reed / turbolinks-compatibility

A collection of tutorials for how to get your favorite javascript libraries, plugins, and snippets working with Turbolinks.
reed.github.io/turbolinks-compatibility
MIT License
147 stars 35 forks source link

Update to Universal Analytics #41

Closed sunnyrjuneja closed 9 years ago

sunnyrjuneja commented 9 years ago

https://support.google.com/analytics/answer/2790010?hl=en

reed commented 9 years ago

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()
rmarescu commented 9 years ago

Here is our solution for Google Universal Analytics. Pretty straight forward and it works for us. Pull request if you'd like to merge - https://github.com/reed/turbolinks-compatibility/pull/46.

I would recommend renaming Google Analytics to Google Classic Analytics or something more descriptive as is being deprecated by Google.

reed commented 9 years ago

Did you try my solution? I'm curious to know if it works.

rmarescu commented 9 years ago

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');

nickjj commented 9 years ago

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?

rmarescu commented 9 years ago

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

nickjj commented 9 years ago

@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>
reed commented 9 years ago

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.