Skip to content

Instantly share code, notes, and snippets.

@andrewagain
Last active March 28, 2023 13:18
Show Gist options
  • Save andrewagain/8999b424aebaadd998f3c3fa0b51dbf0 to your computer and use it in GitHub Desktop.
Save andrewagain/8999b424aebaadd998f3c3fa0b51dbf0 to your computer and use it in GitHub Desktop.
Google Analytics Snippet Unminified
// Source
// https://developers.google.com/analytics/devguides/collection/analyticsjs/
// Original
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
// Unminified
//
// I removed most of the function arguments because the code is easier to understand without them.
// The function arguments onlyn exist to replace frequently used terms with one-word variables, for
// instance i[r] instead of window.ga.
//
(function(win, doc) {
win.GoogleAnalyticsObject = 'ga';
win.ga = win.ga || function() {
(win.ga.q = win.ga.q || []).push(arguments)
},
win.ga.l = 1 * new Date();
var gaScriptElem = doc.createElement('script');
gaScriptElem.async = 1;
gaScriptElem.src = 'https://www.google-analytics.com/analytics.js';
// Insert the GA script element before the first existing script tag. We know there is at least
// one on the page already because this code is within a script tag.
var firstExistingScriptElem = doc.getElementsByTagName('script')[0];
firstExistingScriptElem.parentNode.insertBefore(gaScriptElem, firstExistingScriptElem);
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment