简体   繁体   English

如何为 Google Analytics 4 Gtag 设置心跳

[英]How to setup heartbeat for Google Analytics 4 Gtag

Recently google analytics announced that it gonna end support for old google analytics and want webmasters to move to google analytics 4 (GA4)最近谷歌分析宣布它将结束对旧谷歌分析的支持,并希望网站管理员转移到谷歌分析 4 (GA4)

So they provide basic google analytics code to track website visitors, but Average User Engagement is not even 1 Minute for my website because it is an online chat room, once the chat loads in iframe the events are not tracked by google analytics.因此,他们提供了基本的谷歌分析代码来跟踪网站访问者,但我的网站的平均用户参与度甚至不到 1 分钟,因为它是一个在线聊天室,一旦聊天加载到 iframe,谷歌分析就不会跟踪事件。

在此处输入图像描述

This is my old google analytics code with heart beat to send signal to google analytics that the user is still active on the site.这是我的旧谷歌分析代码,心跳向谷歌分析发送信号,表明用户在网站上仍然活跃。

<script>(function(d,e,j,h,f,c,b){d.GoogleAnalyticsObject=f;d[f]=d[f]||function(){(d[f].q=d[f].q||[]).push(arguments)},d[f].l=1*new Date();c=e.createElement(j),b=e.getElementsByTagName(j)[0];c.async=1;c.src=h;b.parentNode.insertBefore(c,b)})(window,document,"script","https://www.google-analytics.com/analytics.js","ga");ga("create","UA-Google Analytics Code Goes Here-1","auto");ga("send","pageview");function ga_heartbeat(){console.log("Firing heartbeat");ga("send","event","heartbeat");setTimeout(ga_heartbeat,5*60*1000)}ga_heartbeat();</script>

But in new GA4 it is not possible, so i want to add heart beat for GA4 analytic code, if someone help me it might be useful.但是在新的 GA4 中这是不可能的,所以我想为 GA4 分析代码添加心跳,如果有人帮助我,它可能会有用。 Thanks谢谢

You can achieve the same with GA4:您可以使用 GA4 实现同样的效果:

<script>
(function ga4_heartbeat(){console.log("Firing heartbeat");gtag("event","heartbeat");setTimeout(ga4_heartbeat,5*60*1000)}());
</script>

given that you use the "Global Site tag (gtag.js)" code from Admin... Data Streams... Tagging Instructions...假设您使用来自Admin 的“Global Site tag (gtag.js)”代码...数据流...标记说明...

Complete Example (replace G-XXX with your own Measurement ID):完整示例(用您自己的测量 ID 替换 G-XXX):

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXX');
  (function ga4_heartbeat(){console.log("Firing heartbeat");gtag("event","heartbeat");setTimeout(ga4_heartbeat,5*60*1000)}());
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM