在单页应用程序更改客户端路由时,我是否应手动跟踪 GA4 页面浏览次数?

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

Should I manually track GA4 page views when the client router of a Single Page Application changes?

问题

GA4声称具有新的改进测量功能,可以自动监测页面浏览量,但它是否也能检测客户端历史路由更改?(也称为SPA)

如果它不能自动检测,我该如何实施它?

英文:

GA4 claims to have a new improved measurement that can monitor page views automatically, but can it also detect client-side history router changes? (also known as SPA)

How can I implement it if it doesn't auto-detect?

答案1

得分: 1

是的,历史更改会自动检测到,并且每次历史更改时都会发送一个页面查看(状态更改)。

  1. 如果您在GA4属性中启用了增强测量,并在页面查看设置中选择了基于浏览器历史事件的页面更改(请注意,如果您没有对GA4数据流进行任何更改,这是默认设置)

    gtag('config', 'TAG_ID', <parameters>); 这个配置代码会自动检测SPA的历史更改,并在每次历史更改时将page_view发送到GA4。

  2. 如果您禁用了上述选项1,则需要使用自定义JS解决方案根据历史更改手动发送page_view事件。

如果无法自动检测,如何实施?

您需要手动发送它。通常情况下,将以下代码嵌入到路由配置中,以便在历史状态更改时立即触发:

gtag('event', 'page_view', {
  page_title: '<Page Title>',
  page_location: '<Page Location>'
});

重要提示:如果您手动发送page_view事件,请确保正确配置增强测量,以避免在历史状态更改时重复计数页面查看。通常,这意味着在页面查看部分的高级设置中禁用基于浏览器历史事件的页面更改。

建议:如果您仍然依赖于gtag.js,我建议您使用Google标签管理器。

GA4中测量页面查看的官方Google文档

英文:

Yes history changes are detected automatically and a pageview is send for each history push (change in state)

  1. If you have enabled the Enhanced measurement in your GA4 property and selected Page changes based on browser history events under Page Views settings (note this is the default setting if you haven't done any changes to your GA4 data stream)

    gtag(&#39;config&#39;, &#39;TAG_ID&#39;, &lt;parameters&gt;); The config code auto-detects history changes for SPA and sends the page_view to GA4 on each history change.

  2. If you have disabled option 1 above, then you need to manually send the page_view event based on history changes using a custom JS solution


How can I implement it if it doesn't auto-detect?

You need to send it manually. Generally, embed the following code in your router config to trigger as soon as the history state changes

gtag(&#39;event&#39;, &#39;page_view&#39;, {
  page_title: &#39;&lt;Page Title&gt;&#39;,
  page_location: &#39;&lt;Page Location&gt;&#39;
});

Important: If you manually send page_view events, make sure enhanced measurement is configured correctly to avoid double counting pageviews on history state changes. Typically, this means disabling Page changes based on browser history events under the advanced settings of the Page views section.


Recommendation: I would recommend you to use Google Tag Manager if you are still relying on gtag.js


Official Google Documentation on measuring page views in GA4

huangapple
  • 本文由 发表于 2023年7月3日 12:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601837.html
匿名

发表评论

匿名网友

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

确定