Skip to content

Instantly share code, notes, and snippets.

@rmasters
Created January 15, 2018 16:14
Show Gist options
  • Save rmasters/1abcdfa11fa1028ecd152d592ec93ca1 to your computer and use it in GitHub Desktop.
Save rmasters/1abcdfa11fa1028ecd152d592ec93ca1 to your computer and use it in GitHub Desktop.
De-obfuscating the Google Analytics include snippet
window.GoogleAnalyticsObject = 'ga';
// Declare a function that keeps a memory of calls to it, in case the analytics.js loads later on (i.e. after a ga() call)
// Presumably those .q calls get replayed when analytics.js finally loads
window.ga = window.ga || function() {
(window.ga.q = window.ga.q || []).push(arguments)
}, window.ga.l = 1 * new Date();
// e.g. if you called ga('foobar') here, they'd be recorded
// Declare a script tag for the analytics include
var script = document.createElement('script');
script.async = 1;
script.src = 'http://www.google-analytics.com/analytics.js';
// Prepend the analytics include before ga() calls
var firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
// Once loaded, this script replaces window[window.GoogleAnalyticsObject]
/*
(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, // i
document, // s
'script', // o
'//www.google-analytics.com/analytics.js', // g
'ga' // r
// a
// m
);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment