Skip to content

Instantly share code, notes, and snippets.

@thomasheyenbrock
Created July 4, 2018 15:01
Show Gist options
  • Save thomasheyenbrock/6803fa1b1e82662687b5c0dc1c263c48 to your computer and use it in GitHub Desktop.
Save thomasheyenbrock/6803fa1b1e82662687b5c0dc1c263c48 to your computer and use it in GitHub Desktop.
What the google analytics tracking snippet really does...
// 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