Forked from mlkirkpatrick/primo-googleTagManager.js
Last active
December 16, 2020 21:05
-
-
Save stanrb/cf3adbe902623e230eb1d0f258d1d3b1 to your computer and use it in GitHub Desktop.
Injecting GTM in Primo's customization package
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
(function(){ | |
'use strict'; | |
let app = angular.module('googleTagManager', ['angularLoad']); | |
const gtmId = 'GTM-AAAAAAA' | |
const gtmAuth = 'abcd1234' | |
const gtmEnvId = 'env-1' | |
function addGTM(doc) { | |
const newScript = doc.createElement('script') | |
const scriptText = `(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+ '>m_auth=${gtmAuth}>m_preview=${gtmEnvId}>m_cookies_win=x';f.parentNode.insertBefore(j,f); | |
})(window,document,'script','dataLayer','${gtmId}');` | |
newScript.innerHTML = scriptText | |
doc.head.append(newScript) | |
const noscript = doc.createElement('noscript') | |
const noscriptText = `<iframe src="https://www.googletagmanager.com/ns.html?id=${gtmId}" height="0" width="0" style="display:none;visibility:hidden"></iframe>` | |
noscript.innerHTML = noscriptText | |
doc.body.insertBefore(noscript, doc.body.firstChild) | |
} | |
addGTM(document) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment