GithubHelp home page GithubHelp logo

Comments (6)

jonwolfe avatar jonwolfe commented on August 17, 2024

After implementing Google Analytics support in my own app, I believe the strategy mentioned here is superior: https://coderwall.com/p/ypzfdw

If you simply put the standard google analytics script in the document head (NOT the body tag, as you currently recommend), you will get two pageviews recorded on a non-turbolinks page load: one pageview is recorded by the standard Google Analytics script, and another is recorded by the page:change event handler, which is called on that initial page load.

The best approach is this:

  1. Put a modified version of the google analytics initialization code in your document head -- modify it to not actually track the pageview.

  2. In your document body, put this script tag: <script>_gaq.push(['_trackPageview']);</script>

  3. Since the script tag in the body won't be executed when a page is reloaded from the turbolinks cache, add a page:restore handler:

$(document).on 'page:restore', ->
if window._gaq?
_gaq.push ['_trackPageview']
else if window.pageTracker?
pageTracker._trackPageview()

from turbolinks-compatibility.

reed avatar reed commented on August 17, 2024

I actually didn't come up with the GA solution that's posted. @shukydvir did, and it appears that since then he has put together a gem for it. Take a look at the code and let me know if it works.

shukydvir/google-analytics-turbolinks

from turbolinks-compatibility.

jonwolfe avatar jonwolfe commented on August 17, 2024

Hi Nick. That gem works for tracking pageviews on turbolinks pages, but
with a couple of problems:

  1. You need to initialize Google Analytics in the document head -- the gem
    doesn't include that. If you use the standard Google Analytics
    initialization code, you'll get two pageviews on the first page loaded --
    one from the Google Analytics code, and one from the page:change callback
    in the gem.

  2. If you modify the Google Analytics initialization code to not record a
    pageview, then pageviews will only be recorded on turbolinks-capable
    browsers. This is a problem for people like me who are using Turbolinks but
    need it to gracefully degrade on older browsers like IE8/9.

Would you like me to submit a pull request for that page that explains the
alternative approach I used, and also the tradeoffs of using that gem? I'd
be happy to do it.

Regardless, the first line on the page is wrong -- you want to move your
javascript from the body to the head with Turbolinks, not the other way
around.

Best,
Jon

On Tue, Dec 3, 2013 at 9:35 PM, Nick Reed [email protected] wrote:

I actually didn't come up with the GA solution that's posted. @shukydvirhttps://github.com/shukydvirdid, and it appears that since then he has put together a gem for it. Take
a look at the code and let me know if it works.

shukydvir/google-analytics-turbolinkshttps://github.com/shukydvir/google-analytics-turbolinks/blob/master/lib/assets/javascripts/google-analytics-turbolinks.js.coffee


Reply to this email directly or view it on GitHubhttps://github.com//issues/19#issuecomment-29780872
.

from turbolinks-compatibility.

reed avatar reed commented on August 17, 2024

If you use Turbolinks 2.0.0, which we just released today, page:change will fire on DOMContentLoaded, so it will gracefully degrade on unsupported browsers. So the correct solution could be:

  1. Put a modified version of the google analytics initialization code in your document head -- modify it to not actually track the pageview.
  2. Add this snippet inside the head:
trackPageView = ->
  if window.ga?
    ga 'set', 'location', document.location.href.split('#')[0]
    ga 'send', 'pageview'
  else if window._gaq?
    _gaq.push ['_trackPageview']
  else if window.pageTracker?
    pageTracker._trackPageview()

# For Turbolinks 2.0.0
$(document).on 'page:change', trackPageView

# For Turbolinks < 2.0.0
$ ->
  trackPageView()
  $(document).on 'page:change', trackPageView

How does that look? If you agree and would like to submit a PR for it, be my guest.

from turbolinks-compatibility.

jonwolfe avatar jonwolfe commented on August 17, 2024

This is the full solution I came up with. https://gist.github.com/jonwolfe/7897610

The key thing was to test if Turbolinks is initialized and supported before installing the page:change callback. If TL is not supported or is undefined, just track the pageview immediately.

from turbolinks-compatibility.

reed avatar reed commented on August 17, 2024

Cool. Are you still wanting to submit a pull request to update the site with your solution?

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.