All threads / How to use Google Analytics gtag.js with Turbolinks & Webpacker Discussion
Ask A Question

Notifications

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?

Reply

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: event.data.url" 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).

Reply

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: https://support.google.com/analytics/answer/9216061.

Reply
Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.