Last active
February 21, 2023 07:55
-
-
Save walkergv/1ba5021572964197f395 to your computer and use it in GitHub Desktop.
Adding GTM (Google Tag Manager) Container Code using Tampermonkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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 |
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:
- Use man-in-middle like Charles or Fiddler
- Use Requestly chrome plugin (https://chrome.google.com/webstore/detail/requestly-redirect-url-mo/mdnleldcmiljblolnjhpnblkcekpdkpa?hl=en) - works with DTM easily / my favorte approach
- Try to use "Dynamically Insert DTM" option in the Adobe Experience Cliud debugger (does not work for me / unsupported by Adobe)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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