Skip to content

Instantly share code, notes, and snippets.

@DimaSamodurov
Created September 13, 2018 10:06
Show Gist options
  • Save DimaSamodurov/127c39244d0b411bfb474a8102a83497 to your computer and use it in GitHub Desktop.
Save DimaSamodurov/127c39244d0b411bfb474a8102a83497 to your computer and use it in GitHub Desktop.
Using gtag with Turbolinks
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
document.addEventListener('turbolinks:load', function (event) {
if (typeof gtag === 'function') {
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID', {
'page_location': event.data.url
});
}
});
</script>
@overdrivemachines
Copy link

Works great. I just put this in the head

@sehongpark
Copy link

thanks!

@MichalRemis
Copy link

I had problem with this implementation and AdWords. Paid traffic in Google Analytics had too many sessions/user and very high bounce rate, because Analytics was somehow reseting session of paid traffic. Finally I found code that worked:

          gtag('js', new Date());
          gtag('config', 'GA_TRACKING_ID', {
             'page_path': location.pathname + location.search
          });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment