Search code examples
ruby-on-railsgoogle-analyticsgoogle-analytics-apiturbolinks

GA4 Implementation alongside Turbolinks (duplicate page_view)


Running on Rails 5.2.6 - Turbolinks 5.1.1 - i'm embedding GA4 Google Tag to the site using the following code

<script async src="https://www.googletagmanager.com/gtag/js?id=XXXX"></script>

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());

  document.addEventListener('turbolinks:load', event => {
    if (typeof gtag === 'function') {
     gtag('config', 'XXXX', {
        'page_path': location.pathname + location.search,
        'page_location': event.data.url
      });
    }
  });
</script>

using the google Tag Assistant site - everything works fine on first page visit - "page_view" is registered in the "CONFIG".

However I am getting an additional "page_view" event firing on page change registered by "History Change" - which is used to change the back button for the browser.

I don't get this behaviour in Universal Analytics.. ?


Solution

  • So Turns out in GA4 admin / data streams There is a default setting to send a page_view event on change of browser history

    You can disable in the advanced panel