Insert Google tag manager to embedded site

15 hours ago 4
ARTICLE AD BOX

insertBefore expects both parameters to be DOM nodes. Your gtmScript is just a string value.

Create a script element, assign the script code as its text content, and then insert that element.

const head = document.getElementsByTagName("head")[0]; const gtmScript = document.createElement('script'); gtmScript.text = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-PPK5GJD2');`; head.insertBefore(gtmScript, head.firstChild);

C3roe's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article