Ask A Question


You’re not receiving notifications from this thread.

How to use Google Analytics gtag.js with Turbolinks & Webpacker Discussion

Are you using GA for the public parts of GoRails or are you also using it for the authenticated parts of GoRails too?


Thank you for yet another amazing and informative tutorial!
I found that in Rails 6.1.3 with Turbo (rather than Turbolinks), I needed to replace "page_location:" with "page_location: event.detail.url". (data -> detail).


I've also done the upgrade from Turbolinks to Turbo and had to make this change. It was the only detail that I could find to store (ie those other two options had to be removed (title and pagename or something).


I noticed that my Google Analytics Pageviews get counted twice on turbo navigations. This is because Google Analytics has something called "Enhanced measurement" which is on by default. With "enhanced measurement" the Goole analytics script triggers a page view each time the page loads or the browser history state is changed by the active site. Since turbo changes the browser history state, a pageview will be triggered automatically. So, you can either implement Google Analytics by calling a turbo event as Chris did above and disabling enhanced measurement, or you can just rely on enhanced measurement alone. Implementing both will count pageviews twice though. Just posting this in case someone has this problem in the future. It took me a long time before I could figure this out. Here are the google docs about Enhanced measurement:

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 84,111+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.