Update to Universal Analytics
Closed this issue · 8 comments
Does this work? I have no way to test it.
class @GoogleAnalytics
@load: ->
# Create a script element and insert it in the DOM
ua = document.createElement("script")
ua.type = "text/javascript"
ua.async = true
ua.src = ((if "https:" is document.location.protocol then "https://ssl" else "http://www")) + ".google-analytics.com/analytics.js"
firstScript = document.getElementsByTagName("script")[0]
firstScript.parentNode.insertBefore ua, firstScript
# Set account
ga 'create', GoogleAnalytics.analyticsId(), 'auto'
# If Turbolinks is supported, set up a callback to track pageviews on page:change.
# If it isn't supported, just track the pageview now.
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: ->
GoogleAnalytics.documentDomainIncludes "local"
@documentDomainIncludes: (str) ->
document.domain.indexOf(str) isnt -1
@analyticsId: ->
# your google analytics ID(s) here...
GoogleAnalytics.load()
Here is our solution for Google Universal Analytics. Pretty straight forward and it works for us. Pull request if you'd like to merge - #46.
I would recommend renaming Google Analytics
to Google Classic Analytics
or something more descriptive as is being deprecated by Google.
Did you try my solution? I'm curious to know if it works.
No, I haven't tried. I saw your solution last night just before pushing mine (I had it for a while, just didn't have time to create the page), as I did a final search for UA to see if there is already an issue.
Personally I'd prefer going with something that is very close with official implementation rather than a complete overhaul. For example, adding support for Display Advertising using your solution is not as trivial as just adding the code snippet as-is ga('require', 'displayfeatures');
I haven't had a chance to try @rmarescu's PR yet but I'd like to see something of that nature added. It's nearly a drop in replacement which is always good.
Have you done extensive testing to make sure it's gathering all the hits it should be tracking?
We've used the implementation suggested on a site that generated more than 400 million page views in November; we also have custom event tracking implemented
@rmarescu Awesome. I imagine you guys have enhanced link attribution working too?
Ex.
<script>
ga('require', 'displayfeatures');
ga('require', 'linkid', 'linkid.js');
ga('set', 'location', location.href.split('#')[0]);
ga('send', 'pageview', { 'title': document.title });
</script>
I wasn't asking if my solution works because I would use it instead of yours. If it works, I was going to include both so that users would have options.
Anyway, I merged your PR. Thanks for submitting it.