Skip to content

Instantly share code, notes, and snippets.

@walkergv
Last active February 21, 2023 07:55
Show Gist options
  • Save walkergv/1ba5021572964197f395 to your computer and use it in GitHub Desktop.
Save walkergv/1ba5021572964197f395 to your computer and use it in GitHub Desktop.
Adding GTM (Google Tag Manager) Container Code using Tampermonkey
// ==UserScript==
//@name Name of your script
//@namespace http://use.i.E.your.homepage/
//@version 0.1
//@description Inject GTM bootstrap on my website
//@include http://www.domain.com/* [wildcard url where GTM should inject the code]
// ==/UserScript==
// Google Tag Manager
setTimeout("(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.src="+
"'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);"+
"})(window,document,'script','dataLayer','YOUR_CONATINER_ID_GOES_HERE');"+
"console.info('GTM injected');",500);
// End Google Tag Manager
@mannbarry2
Copy link

This works extremely well, I have contemplated forking it for other tag managers (Adobe DTM , Tealium IQ and Adobe Launch eventually) if anyone would like to assist me.

Alternative approaches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment