简体   繁体   English

Google Analytic GA4 不会禁用 React 应用程序中的默认 page_view 事件

[英]Google Analytic GA4 does not disable default page_view event in React Application

I'm trying to add Google Analytics GA4 to a React Application,我正在尝试将 Google Analytics GA4 添加到 React 应用程序,

index.html索引.html

   <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'G-xxxxxxxL', {send_page_view: false});
    </script>

Except that this does not disable the default page_view event.除了这不会禁用默认的 page_view 事件。

I would like to disable the default request so I manually send page_view events.我想禁用默认请求,所以我手动发送 page_view 事件。 they are some pages where the title is not set unlike a saga returns, for example: Project: .它们是一些没有设置标题的页面,不像 saga 返回,例如: Project: 。 the default event sets to undefined, which make sense because data is not loaded yet.默认事件设置为未定义,这是有道理的,因为尚未加载数据。

So I how can I achieve this?那么我该如何实现呢?

Try toggling the option to automatically track history events as page cries OFF from the Enhanced Measurement settings in GA4 admin (Web Stream settings).尝试从 GA4 管理员中的增强测量设置(Web Stream 设置)切换选项以在页面关闭时自动跟踪历史事件。

Many SPA sites have an initial pushState/replaceState in place for state management purposes (or for other reasons), which would cause a page_view to be tracked of that Enhanced Measurement option is toggled on.许多 SPA 站点为 state 管理目的(或出于其他原因)设置了初始 pushState/replaceState,这将导致在启用增强测量选项时跟踪 page_view。

Do you disable gtag on every webpage?您是否在每个网页上禁用 gtag?

As the docs say:正如文档所说:

The send_page_view setting does not persist across pages. send_page_view 设置不会跨页面保留。 This setting must be repeated on every page of your website where you want to disable automatic pageviews.必须在您要禁用自动浏览量的网站的每个页面上重复此设置。

I ran into the same problem, my initial idea was that sincs the default index.html loads as a framing for the react app, I can disable page_view events on all pages just by adding the snippet here.我遇到了同样的问题,我最初的想法是默认index.html加载为 react 应用程序的框架,我可以通过在此处添加片段来禁用所有页面上的page_view事件。 But this was not the case for me.但对我来说不是这样。

I ended up programmatically removing the gtag events from dataLayer on the componentDidMount of App.tsx with the ts equivalent of a pseudo snippet like this (I dont have the exact code on me right now):我最终以编程方式从 App.tsx 的componentDidMount上的dataLayer中删除了 gtag 事件,ts 等效于这样的伪代码段(我现在没有确切的代码):

window.dataLayer = window.dataLayer.filter(element => element['event'] && !(element['event'] === 'gtm.load' || element['event'] === 'gtm.dom'));

This is as crazy as it looks: we remove the tags responsible for page hits right from the data buffer.这看起来很疯狂:我们直接从数据缓冲区中删除了负责页面点击的标签。 This seems to work but it not only is a major hack, but looks ugly in my opinion, so any better non-lib way for this is much appreciated on my side!这似乎有效,但它不仅是一个主要的黑客攻击,而且在我看来看起来很丑,所以任何更好的非 lib 方式都非常感谢我!

This is a very complicated issue - I'm sure if you're here you're frustrated.这是一个非常复杂的问题——我敢肯定,如果你在这里,你会感到沮丧。

In addition to making sure you've disabled 'browser-history events' under Enhanced measurement events I also discovered the following:除了确保您在增强测量事件下禁用了“浏览器历史事件”之外,我还发现了以下内容:

  • The preview feature does NOT work the same as the live deployed version.预览功能与实时部署的版本不同。 I have my site open twice one under the GTM 'preview' feature another instance normally.我的网站在 GTM“预览”功能下打开了两次,通常是另一个实例。 With the exact same version deployed I get ONE page_view event under the preview and TWO when I open the site normally.部署完全相同的版本后,我在预览下得到一个 page_view 事件,当我正常打开站点时得到两个。

  • I can't seem to figure out the nuances of why this occurs but to debug where the events are coming from I found the following settings useful:我似乎无法弄清楚为什么会发生这种情况的细微差别,但要调试事件的来源,我发现以下设置很有用:

在此处输入图像描述

The two yellow fields get sent as event parameters which means when I look at the debugging in Chrome console I can see exactly where they came from:两个黄色字段作为事件参数发送,这意味着当我在 Chrome 控制台中查看调试时,我可以确切地看到它们来自哪里:

在此处输入图像描述

In this case you can see they fire on 'Route change' and 'DOM Loaded' - which is exactly how I have them configured:在这种情况下,您可以看到它们在“Route change”和“DOM Loaded”上触发——这正是我配置它们的方式:

在此处输入图像描述

When I load in preview mode it only gets the gtm.dom event and no Route change .当我在预览模式下加载时,它只会获得gtm.dom事件而没有Route change I can't figure out why.我不知道为什么。 It's like the 'GTM Preview' feature is swallowing my history change events.这就像“GTM 预览”功能正在吞噬我的历史更改事件。

  • Another final tip:最后的另一个提示:

Add a Custom HTML tag for the following code and trigger it when Route Change occurs.为以下代码添加Custom HTML标签,并在发生Route Change时触发。 That way you can see in the console exactly when it occurs (or doesn't occur).这样您就可以在控制台中准确地看到它何时发生(或不发生)。

<script>
  console.log('Route Change - GTM Debug - {{Page Hostname}} {{Page URL}}'); 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法禁用 Google Analytics page_view 事件 - Can't disable Google Analytics page_view events Google Analytics 4 不会在虚拟页面视图上发送 `page_view`(`hashchange` 源的`historyChange` 事件) - Google Analytics 4 doesn't send `page_view` on virtual page views (`historyChange` event for `hashchange` source) GA4 Google Analytics 事件未显示为转化 - GA4 Google Analytics event doesn't appear as conversion React-ga未在Google Analytics(分析)仪表板中返回正确的活动页面 - React-ga not returning correct active page in Google Analytics Dashboard GA4 谷歌分析。 gtag.js 发送请求有几秒钟的延迟,而不是立即 - GA4 Google Analytics. gtag.js sends a request with a delay of a few seconds, not instantly 带有 react-ga 库的 Google Analytics - Google Analytics with react-ga library react-ga4 多次发送视图 - react-ga4 sends view multiple times 谷歌分析使用 react-ga 推荐 react-router 实现使用上一条路线的页面标题 - Google Analytics using react-ga recommended react-router implementation uses page title from previous route 反应路由器如何禁用标签的默认行为? - how does react router disable default behavior of a tag? React Router v4 Google Analytics(分析)react-ga - React Router v4 Google Analytics react-ga
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM