GithubHelp home page GithubHelp logo

Comments (8)

reed avatar reed commented on July 18, 2024

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()

from turbolinks-compatibility.

rmarescu avatar rmarescu commented on July 18, 2024

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.

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024

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

from turbolinks-compatibility.

rmarescu avatar rmarescu commented on July 18, 2024

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

from turbolinks-compatibility.

nickjj avatar nickjj commented on July 18, 2024

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?

from turbolinks-compatibility.

rmarescu avatar rmarescu commented on July 18, 2024

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

from turbolinks-compatibility.

nickjj avatar nickjj commented on July 18, 2024

@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>

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024

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.

from turbolinks-compatibility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.