GA4 Implementation alongside Turbolinks (duplicate page_view)

huangapple go评论73阅读模式
英文:

GA4 Implementation alongside Turbolinks (duplicate page_view)

问题

Running on Rails 5.2.6 - Turbolinks 5.1.1 - 我正在使用以下代码将GA4 Google Tag嵌入到网站中

<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>

在使用Google Tag Assistant站点时,一切都在第一页访问时正常工作 - "page_view"在"CONFIG"中被注册。

但是,在页面更改时,我会收到额外的“page_view”事件,由“History Change”注册 - 用于更改浏览器的后退按钮。

在Universal Analytics中我没有遇到这种行为.. ?

英文:

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

&lt;script async src=&quot;https://www.googletagmanager.com/gtag/js?id=XXXX&quot;&gt;&lt;/script&gt;

&lt;script&gt;
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}

  gtag(&#39;js&#39;, new Date());

  document.addEventListener(&#39;turbolinks:load&#39;, event =&gt; {
    if (typeof gtag === &#39;function&#39;) {
     gtag(&#39;config&#39;, &#39;XXXX&#39;, {
        &#39;page_path&#39;: location.pathname + location.search,
        &#39;page_location&#39;: event.data.url
      });
    }
  });
&lt;/script&gt;

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.. ?

答案1

得分: 0

在GA4管理/数据流中,原来有一个默认设置,即在浏览器历史更改时发送page_view事件。

您可以在高级面板中禁用此设置。

英文:

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

huangapple
  • 本文由 发表于 2023年5月25日 21:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定