A website template with lots of features, built with ruby on rails.

google_analytics.js.coffee 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class @GoogleAnalytics
  2. @load: ->
  3. # Google Analytics depends on a global _gaq array. window is the global scope.
  4. window._gaq = []
  5. window._gaq.push ["_setAccount", GoogleAnalytics.analyticsId()]
  6. # Create a script element and insert it in the DOM
  7. ga = document.createElement("script")
  8. ga.type = "text/javascript"
  9. ga.async = true
  10. ga.src = ((if "https:" is document.location.protocol then "https://ssl" else "http://www")) + ".google-analytics.com/ga.js"
  11. firstScript = document.getElementsByTagName("script")[0]
  12. firstScript.parentNode.insertBefore ga, firstScript
  13. # If Turbolinks is supported, set up a callback to track pageviews on page:change.
  14. # If it isn't supported, just track the pageview now.
  15. if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
  16. document.addEventListener "page:change", (->
  17. GoogleAnalytics.trackPageview()
  18. ), true
  19. else
  20. GoogleAnalytics.trackPageview()
  21. @trackPageview: (url) ->
  22. unless GoogleAnalytics.isLocalRequest()
  23. if url
  24. window._gaq.push ["_trackPageview", url]
  25. else
  26. window._gaq.push ["_trackPageview"]
  27. window._gaq.push ["_trackPageLoadTime"]
  28. @isLocalRequest: ->
  29. GoogleAnalytics.documentDomainIncludes "local"
  30. @documentDomainIncludes: (str) ->
  31. document.domain.indexOf(str) isnt -1
  32. @analyticsId: ->
  33. # your google analytics ID(s) here...
  34. 'UA-58359004-1'
  35. GoogleAnalytics.load()