Skip to content

Instantly share code, notes, and snippets.

@ChunAllen
Forked from mupkoo/google_analytics.coffee
Last active August 29, 2015 14:12
Show Gist options
  • Save ChunAllen/74eb772fd43aa7657abf to your computer and use it in GitHub Desktop.
Save ChunAllen/74eb772fd43aa7657abf to your computer and use it in GitHub Desktop.
class @GoogleAnalytics
@load: ->
# Load the analytics code
window['GoogleAnalyticsObject'] = 'ga'
window['ga'] = window['ga'] || ->
(window['ga'].q = window['ga'].q || []).push arguments
window['ga'].l = 1 * new Date()
# Add the script
googleScript = document.createElement("script")
googleScript.async = 1
googleScript.src = '//www.google-analytics.com/analytics.js'
firstScript = document.getElementsByTagName("script")[0]
firstScript.parentNode.insertBefore googleScript, firstScript
# Create the analytics
ga 'create', GoogleAnalytics.analyticsId(), 'auto'
# You can enable additional modules like so
# ga 'require', 'displayfeatures'
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
document.addEventListener "page:change", GoogleAnalytics.trackPageview, true
else
GoogleAnalytics.trackPageview()
@trackPageview: (url) ->
unless GoogleAnalytics.isLocalRequest()
if url
ga 'send', 'pageview', url
else
ga 'send', 'pageview'
@isLocalRequest: ->
document.domain.indexOf('dev') isnt -1
@analyticsId: ->
'TRACKING-ID'
GoogleAnalytics.load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment