Last active
March 9, 2019 10:43
-
-
Save dustinrecko/1aab97802642cece60c7d88a78a32420 to your computer and use it in GitHub Desktop.
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
/** | |
* THE STANDARD.. | |
* | |
*/ | |
(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;f.parentNode.insertBefore(j,f); | |
})(window,document,'script','dataLayer','GTM-XXXXXXX'); | |
/** | |
* ..BECOMES ---> | |
* | |
*/ | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.pushStashed = window.dataLayer.push; | |
Object.defineProperty(window.dataLayer, 'push', { | |
set(y) { | |
this.pushStashed = y; | |
Object.defineProperty(this, 'push', { | |
writable: true, | |
value: function() { | |
arguments = [].map.call(arguments, function(event) { | |
if (event && event["gtm.element"]) { | |
event.dataContext = {}, elem = event["gtm.element"]; | |
for (; elem && elem !== document; elem = elem.parentNode) { | |
[].forEach.call(elem.attributes, function(attr) { | |
if (/^data-/.test(attr.name)) { | |
var camelCaseName = attr.name.substr(5).replace(/-(.)/g, function($0, $1) { | |
return $1.toUpperCase(); | |
}); | |
event.dataContext[camelCaseName] = event.dataContext[camelCaseName] || attr.value; | |
} | |
}); | |
} | |
} | |
return event; | |
}); | |
return this.pushStashed.apply(null,arguments); | |
} | |
}); | |
}, | |
get() { | |
return this.pushStashed | |
}, | |
configurable: true | |
}); | |
(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;f.parentNode.insertBefore(j,f); | |
})(window,document,'script','dataLayer','GTM-XXXXXXX'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment