Created
July 4, 2018 15:01
-
-
Save thomasheyenbrock/6803fa1b1e82662687b5c0dc1c263c48 to your computer and use it in GitHub Desktop.
What the google analytics tracking snippet really does...
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
// create a ga function in the window object (if not exists) | |
// which queues all function calls as long as the real script | |
// is not loaded yet | |
window.GoogleAnalyticsObject = 'ga'; | |
window.ga = window.ga || function () { | |
(window.ga.q = window.ga.q || []).push(arguments) | |
}; | |
window.ga.l = 1 * new Date(); | |
// create new script tag to load the "real" ga function | |
newScript = document.createElement('script'); | |
newScript.async = 1; | |
newScript.src = 'https://www.google-analytics.com/analytics.js'; | |
// insert the new script tag into the dom | |
firstScriptInHtml = document.getElementsByTagName('script')[0]; | |
firstScriptInHtml.parentNode.insertBefore(newScript, firstScriptInHtml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment