[英]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-router
和react-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.