Skip to content

Instantly share code, notes, and snippets.

@Startouf
Created August 20, 2016 22:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Startouf/aa04fe6616d01f794e1b26336b790f3c to your computer and use it in GitHub Desktop.
Save Startouf/aa04fe6616d01f794e1b26336b790f3c to your computer and use it in GitHub Desktop.
Google Analytics for Turbolinks 5 (& Rails 5)
// Google Analytics code to work with Turbolniks 5
this.GoogleAnalytics = (function() {
function GoogleAnalytics() {}
GoogleAnalytics.load = function() {
var firstScript, ga;
window._gaq = [];
window._gaq.push(["_setAccount", GoogleAnalytics.analyticsId()]);
window._gaq.push(['_setAllowAnchor', true]);
ga = document.createElement("script");
ga.type = "text/javascript";
ga.async = true;
ga.src = ("https:" === document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
firstScript = document.getElementsByTagName("script")[0];
firstScript.parentNode.insertBefore(ga, firstScript);
if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) {
return document.addEventListener("turbolinks:load", (function() {
return GoogleAnalytics.trackPageview();
}), true);
} else {
return GoogleAnalytics.trackPageview();
}
};
GoogleAnalytics.trackPageview = function(url) {
if (!GoogleAnalytics.isLocalRequest()) {
if (url) {
window._gaq.push(["_trackPageview", url]);
} else {
window._gaq.push(["_trackPageview"]);
}
return window._gaq.push(["_trackPageLoadTime"]);
}
};
GoogleAnalytics.isLocalRequest = function() {
return GoogleAnalytics.documentDomainIncludes("local");
};
GoogleAnalytics.documentDomainIncludes = function(str) {
return document.domain.indexOf(str) !== -1;
};
GoogleAnalytics.analyticsId = function() { return 'UA-xxxxxxxx-y' };
return GoogleAnalytics;
})();
GoogleAnalytics.load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment