如何使用新的Google Analytics(GTAG.JS)跟踪和发送自定义事件?
我有一个旧的网站,该网站正在使用旧的Google Analytics(分析代码),我需要使用新GA4的帮助。
旧代码(2016)
要向Google发送自定义事件和页面浏览量,我将从其< script>
sminippet中使用全局 ga()
函数:
// Event
ga('send', 'event', {
eventAction: 'click',
eventCategory: 'social',
eventLabel: 'twitter'
});
// Pageview
ga('send', {
hitType: 'pageview',
page: 'Video page',
title: 'Intro video'
});
新代码(2022)
Google Analytics(Google Analytics)表示,所有旧属性都将在2023年7月1日停止工作,因此我们需要切换到新的Google Analytics 4属性,标题中的< script>
摘要已更改,现在,它创建 gtag()
:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=X-XXX123456"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'X-XXX123456');
</script>
但是尝试使用 gtag(“ send”)
,就像我以前一样,看起来再也没有传输到Google了。我已经尝试查找它,但是所有教程/文章仍然展示了旧方法。如何使用新的GA4发送自定义事件?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
推荐名称是
“ event”
“发送” /事件“ rel =“ nofollow noreferrer”>官方文档。另外,推荐参数应使用下划线,例如event_category
:如果您正在手动发送自定义页面浏览量,请确保在着陆时禁用初始页面浏览量以避免对其进行两次计数,如在标题
&lt; script>&gt;
中添加send_page_view:false
:您可以通过打开网络选项卡并查看有效载荷来检查是否在本地发送某些内容每个
收集
网络请求:sources:
Instead of using
"send"
, the recommended name is"event"
official docs. Also, the recommended parameters should use an underscore, likeevent_category
:If you're manually sending custom pageviews, make sure you disable the initial pageview upon landing to avoid counting it twice, as explained in the "Manual Pageviews" section by adding
send_page_view: false
to your initial config call in the header<script>
:You can check that something is being sent locally by opening your Network Tab and looking at the payload of each
collect
network request:Sources:
这是发送ga4的语法:
在这里您可以看到事件的文档。
另外,如果要测试命中率,并且如果发送了这些事件,则可以使用 google tag assisth < /a>对此进行审查。
This is the syntax to send an event with GA4:
Here you can see the documentation for the events.
Alternatively, if you want to test the hits and if the events are being sent with which data, you can use the Google Tag Assistant to review that.