Skip to content

Instantly share code, notes, and snippets.

@walkergv
Last active February 21, 2023 07:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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
@walkergv
Copy link
Author

Adding GTM using Tampermonkey

Script originally created by Stéphane Hamel of Cardinal Path and provided here with additional instructions and screen shots.

We can add GTM to the website using tamper monkey. We do this so we do not have to install the script before it's all configured. Tampermonkey injects the script in your browser without installing the GTM container script site wide before it's ready.

To get inject the script you need to get the tampermonkey chrome extension here.


Add the script to tamper monkey


Paste in the script provided above to you and change the @include (1) line to the appropriate site if required, Change the container ID (2) if required and hit save (3).


Load up the site and check that the script loaded. Check that the Console in the Chrome Dev tools (Ctrl+Shift+i) shows two lines "Tampermonkey Started" and "GTM injected"


When working GTM should load and behave exactly as id it was installed in site source code

@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