简体   繁体   English

GA4 谷歌分析。 gtag.js 发送请求有几秒钟的延迟,而不是立即

[英]GA4 Google Analytics. gtag.js sends a request with a delay of a few seconds, not instantly

The problem is that a request to Google https://www.google-analytics.com/g/collect?问题是向谷歌请求https://www.google-analytics.com/g/collect? is not sent immediately, but after 3-4 seconds.不是立即发送,而是在 3-4 秒后发送。 I looked for a solution in the official documentation from Google, but did not find it.我在谷歌的官方文档中寻找解决方案,但没有找到。 I want this request to be sent immediately.我希望立即发送此请求。

I added a script to the head tag as stated in the documentation from Google.如 Google 文档中所述,我在 head 标签中添加了一个脚本。

<!-- Global site tag (gtag.js) - Google Analytics -->
<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=MY_KEY"></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())
  gtag('config', 'MY_KEY', {send_page_view: false})
</script>

My app is built on React version ^18.2.0 and using react-router and react-router-dom .我的应用程序基于 React 版本^18.2.0并使用react-routerreact-router-dom构建。 I use useEffect to send event.我使用useEffect发送事件。

const location = useLocation()

useEffect(() => {
  window.gtag('event', 'page_view', {
    page_location: window.location.href,
    page_path: location.pathname + location.search + location.hash,
    page_search: location.search,
    page_hash: location.hash,
  })
}, [location.pathname])

What you're experiencing is the new feature of gtag.js.您正在体验的是 gtag.js 的新功能。 The GA4 tracking library. GA4 跟踪库。

Here's more about it from here :这里有更多关于它的信息:

Event grouping Most of the events that users trigger on your website or app are not sent one at a time.事件分组 用户在您的网站或应用程序上触发的大多数事件并不是一次发送一个。 Instead, most events are grouped together (or batched).相反,大多数事件被组合在一起(或批处理)。

However, events are not batched in the following circumstances:但是,以下情况不会对事件进行批处理:

Conversion events are transmitted immediately, although they may be part of a batch Containers loaded in debug mode never batch events to provide you with realtime data Events that are temporarily stored by your device are sent when a user leaves a page In browser environments that don't support the sendBeacon API, all events are sent immediately转换事件会立即传输,尽管它们可能是批处理的一部分 在调试模式下加载的容器从不为您提供实时数据的批处理事件 当用户离开页面时,您的设备临时存储的事件将被发送t 支持sendBeacon API,所有事件立即发送

I'm trying to find a way to stop the library from batching events, but I don't see anything documented as of yet.我正在尝试找到一种方法来阻止库对事件进行批处理,但到目前为止我还没有看到任何记录。 Will update this if I find a way that wouldn't involve rewriting the library.如果我找到一种不涉及重写库的方法,将更新它。

In vast majority of cases, nobody cares about the few seconds delay in when the event is sent, given that the batched events are meant to have timestamps of when they were triggered rather than when the batch was sent.在绝大多数情况下,没有人关心事件发送时的几秒延迟,因为批处理事件应该具有触发时间的时间戳,而不是发送批处理时间的时间戳。

Note that batching only works when the sendBeacon api is available.请注意,批处理仅在 sendBeacon api 可用时才有效。 This is mostly to avoid the request/data loss on window unload.这主要是为了避免 window 卸载时请求/数据丢失。 You can read more about it here .您可以在此处阅读更多相关信息。

暂无
暂无

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

相关问题 如何在javascript中动态加载gtag.js(谷歌分析) - How to load gtag.js (Google analytics) dynamically in javascript Google Analytics 内容分组无法使用 gtag.js - Google Analytics content grouping not working using gtag.js Google Analytics 脚本片段 analytics.js 和 gtag.js 之间的区别 - Difference between Google Analytics script snippets analytics.js and gtag.js 如何在Google Analytics(分析)(gtag.js,analytics.js)过滤器中设置“用户定义”字段? - How to set User Defined field in Google Analytics (gtag.js, analytics.js) filter? 结合Google Analytics(gtag.js)和Google跟踪代码管理器(gtm.js) - Combining Google Analytics (gtag.js) and Google Tag Manager (gtm.js) 如何在GA(gtag.js)中跟踪Facebook销售流量? - How to track Facebook sale traffic in GA (gtag.js)? 我可以为多个子域使用相同的Google Analytics(gtag.js)跟踪ID吗? - Can I use the same Google Analytics (gtag.js) tracking ID for multiple subdomains? 我是否需要包括这一行:<!-- Global site tag (gtag.js) - Google Analytics --> - Do I need to include this line: <!-- Global site tag (gtag.js) - Google Analytics --> 如何使用gtag.js使Google Analytics(分析)在SPA上显示正确的活动页面? - How to get google analytics to show correct active page on SPA using gtag.js? Google Analytics(分析)(gtag.js):产品列表名称未显示,但其他字段显示 - Google Analytics (gtag.js): Product List Name doesn't appear but other fields do
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM