Advertisement
Guest User

Untitled

a guest
May 25th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. class @GoogleAnalytics
  2.  
  3. @load: ->
  4. # Load the analytics code
  5. window['GoogleAnalyticsObject'] = 'ga'
  6. window['ga'] = window['ga'] || ->
  7. (window['ga'].q = window['ga'].q || []).push arguments
  8. window['ga'].l = 1 * new Date()
  9.  
  10. # Add the script
  11. googleScript = document.createElement("script")
  12. googleScript.async = 1
  13. googleScript.src = '//www.google-analytics.com/analytics.js'
  14.  
  15. firstScript = document.getElementsByTagName("script")[0]
  16. firstScript.parentNode.insertBefore googleScript, firstScript
  17.  
  18. # Create the analytics
  19. ga 'create', GoogleAnalytics.analyticsId(), 'auto'
  20.  
  21. # You can enable additional modules like so
  22. # ga 'require', 'displayfeatures'
  23.  
  24. if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
  25. document.addEventListener "page:change", GoogleAnalytics.trackPageview, true
  26. else
  27. GoogleAnalytics.trackPageview()
  28.  
  29. @trackPageview: (url) ->
  30. unless GoogleAnalytics.isLocalRequest()
  31. if url
  32. ga 'send', 'pageview', url
  33. else
  34. ga 'send', 'pageview'
  35.  
  36. @isLocalRequest: ->
  37. document.domain.indexOf('dev') isnt -1
  38.  
  39. @analyticsId: ->
  40. '[change-this-to-analytics-id]'
  41.  
  42. GoogleAnalytics.load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement