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: 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).
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.