Dwight Watson's blog

Google Analytics with Turbolinks 5

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

The newest version of Turbolinks is really easy to use, especially now that it's framework agnostic. Only a couple of lines of setup and it's good to go - you get the benefit of a site that feels faster and native app wrappers to drop your site into. However, it doesn't quite work out of the box with Google Analytics.

All you need to do though is hook into the turbolinks:load event which is fired whenever a new page is loaded through Turbolinks. You can then confirm that Google Analytics is on the page (checking for ga) and then firing off the new pageview.

document.addEventListener("turbolinks:load", (event) => {
if (typeof ga === "function") {
ga("set", "location", event.data.url);
ga("send", "pageview");
}
});

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.