Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Turbolinks #29

Open
mebibou opened this issue Nov 9, 2015 · 5 comments
Open

Support Turbolinks #29

mebibou opened this issue Nov 9, 2015 · 5 comments

Comments

@mebibou
Copy link

mebibou commented Nov 9, 2015

Do you have any plans to support Turbolinks? like described here:

http://reed.github.io/turbolinks-compatibility/google_analytics.html

@spodlecki
Copy link
Collaborator

I feel like that is something that's better left to the app's specific requirements. What you could do, is still use the init tag, but pass skip_pageview: true as an option.

Then using the coffeescript in your ticket, you can run the ga commands.

Would just need to make sure that turbolinks doesn't reload the GA scripts, which I believe you can do by placing the init tag in the head

@asecondwill
Copy link

Think it needs to work with Turbo Links as that's standard for rails.

@spodlecki
Copy link
Collaborator

spodlecki commented Jan 5, 2017

To accomplish what you're looking for, here is a snippet of code that'll get you started. I welcome any PR that would like to include a JavaScript class for this style, but I still feel its best left to the application.

application.html.erb

<html>
  <head>
  <%= analytics_init(skip_pageview: true) %>
  </head>
  ....
</html>

Javascript in app

(function() {
  document.addEventListener("turbolinks:load", function() {
    ga('send', 'pageview', window.location.pathname);
  });
})();

Turbo Links will execute any javascripts you place in the body as the different pages load too, so there is always that option as well.

<body>
  <div>Hello world</div>
  <%= GA::Events::TrackPageview.new %>
</body>

*all untested

@SimonVillage
Copy link

We had to change the javascript part:

(function() {
  document.addEventListener("turbolinks:load", function() {
    ga('send', 'pageview', window.location.pathname);
  });
})();

@spodlecki
Copy link
Collaborator

Updated above snippet to use the pathname in the pageview event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants