Forked from Startouf/ google_analytics_turbolinks5.js
Created
November 9, 2017 12:24
-
-
Save wesley6j/95615b34dd1ee30c43ce378fd20ffeef to your computer and use it in GitHub Desktop.
Google Analytics for Turbolinks 5 (& Rails 5)
This file contains hidden or 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
// 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