Skip to content

Instantly share code, notes, and snippets.

@mlkirkpatrick
Created December 14, 2020 18:42
Show Gist options
  • Save mlkirkpatrick/469885e67b8397f73a95f53d379e1ce9 to your computer and use it in GitHub Desktop.
Save mlkirkpatrick/469885e67b8397f73a95f53d379e1ce9 to your computer and use it in GitHub Desktop.
(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+ '&gtm_auth=${gtmAuth}&gtm_preview=${gtmEnvId}&gtm_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