Created
August 5, 2017 20:05
-
-
Save typpo/5e2e4403c60314e04e8b6b257555f6de to your computer and use it in GitHub Desktop.
MIT License
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 queue to push events and stub all methods | |
window.analytics || (window.analytics = {}); | |
window.analytics_queue || (window.analytics_queue = []); | |
(function() { | |
var methods = ['identify', 'track', 'trackLink', 'trackForm', 'trackClick', 'trackSubmit', 'page', 'pageview', 'ab', 'alias', 'ready', 'group', 'on', 'once', 'off']; | |
var factory = function(method) { | |
return function () { | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(method); | |
analytics_queue.push(args); | |
return window.analytics; | |
}; | |
}; | |
for (var i = 0; i < methods.length; i++) { | |
var method = methods[i]; | |
window.analytics[method] = factory(method); | |
} | |
})(); | |
// Load analytics.js after everything else | |
analytics.load = function(callback) { | |
var script = document.createElement('script'); | |
script.async = true; | |
script.type = 'text/javascript'; | |
script.src = '/assets/js/analytics.min.js'; // <--- your url here | |
if (script.addEventListener) { | |
script.addEventListener('load', function(e) { | |
if (typeof callback === 'function') { | |
callback(e); | |
} | |
}, false); | |
} else { // IE8 | |
script.onreadystatechange = function () { | |
if (this.readyState == 'complete' || this.readyState == 'loaded') { | |
callback(window.event); | |
} | |
}; | |
} | |
var firstScript = document.getElementsByTagName('script')[0]; | |
firstScript.parentNode.insertBefore(script, firstScript); | |
}; | |
analytics.load(function() { | |
analytics.initialize({ | |
'Google Analytics': { | |
trackingId: 'UA-XXX-1' | |
}, | |
'Mixpanel': { | |
token: 'XXX' | |
} | |
}); | |
// Loop through the interim analytics queue and reapply the calls to their | |
// proper analytics.js method. | |
while (window.analytics_queue.length > 0) { | |
var item = window.analytics_queue.shift(); | |
var method = item.shift(); | |
if (analytics[method]) analytics[method].apply(analytics, item); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Gatsby Users: just published the related plugin https://github.com/thebarty/gatsby-plugin-analytics-without-segment