简体   繁体   English

如何使用新的 Google Analytics(分析)4 (gtag.js) 跟踪和发送自定义事件?

[英]How can I track and send custom events with the new Google Analytics 4 (gtag.js)?

I have an old website that's using the old Google Analytics code from last decade, and I need help using the new GA4.我有一个旧网站,该网站使用的是过去十年的旧 Google Analytics(分析)代码,我需要有关使用新 GA4 的帮助。

Old Code (2016)旧代码 (2016)

To send custom events and pageviews to Google, I would use the global ga() function from their <script> snippet:要向 Google 发送自定义事件和综合浏览量,我会使用其<script>代码段中的全局ga() function:

// Event
ga('send', 'event', {
    eventAction: 'click',
    eventCategory: 'social',
    eventLabel: 'twitter'
});

// Pageview
ga('send', {
    hitType: 'pageview',
    page: 'Video page',
    title: 'Intro video'
});

New Code (2022)新法典 (2022)

Google Analytics says that all old properties will stop working on July 1, 2023, so we need to switch to the new Google Analytics 4 property, the <script> snippet in the header has changed a bit, now it creates gtag() : Google Analytics 表示所有旧属性将在 2023 年 7 月 1 日停止工作,因此我们需要切换到新的 Google Analytics 4 属性,header 中的<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>

But upon trying to use gtag("send") like I used to, it looks like nothing gets transmitted to Google anymore.但是,当我尝试像以前一样使用gtag("send")时,似乎没有任何内容会再传输到 Google。 I've tried looking it up, but all tutorials/articles still demonstrate the old approach.我已经尝试查找它,但所有教程/文章仍然展示了旧方法。 How can I send custom events using the new GA4?如何使用新的 GA4 发送自定义事件?

Instead of using "send" , the recommended name is "event" see here for official docs .不使用"send" ,推荐的名称是"event" ,参见此处获取官方文档 Also, the recommended parameters should use underscore, like event_category (although you could use your own custom ones):此外,推荐的参数应使用下划线,例如event_category (尽管您可以使用自己的自定义参数):

// Send event
gtag("event", "click", {
    "event_category": "social",
    "event_label": "twitter"
});

// Pageview
gtag('event', 'page_view', {
    "page_title": "landing-page",
});

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> :如果您手动发送自定义综合浏览量,请确保在登陆时禁用初始综合浏览量以避免将其计数两次,如“手动综合浏览量”部分所述,将send_page_view: false添加到 header <script>中的初始配置调用:

gtag('config', 'X-XXX123456', {send_page_view: false});

You can check that something is being sent locally by opening your Network Tab and looking at the payload of each collect .network request:您可以通过打开“网络”选项卡并查看每个collect .network 请求的有效负载来检查是否正在本地发送某些内容:

在此处输入图像描述

Sources:资料来源:

This is the syntax to send an event with GA4:这是使用 GA4 发送事件的语法:

gtag('event', <action>, {
  'event_category': <category>,
  'event_label': <label>,
  'value': <value>
});

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.或者,如果您想测试命中以及事件是否与哪些数据一起发送,您可以使用Google Tag Assistant来查看。

声明:本站的技术帖子网页,遵循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)跟踪ID吗? - Can I use the same Google Analytics (gtag.js) tracking ID for multiple subdomains? 如何在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 Analytics content grouping not working using gtag.js 我是否需要包括这一行:<!-- 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? 如何在GA(gtag.js)中跟踪Facebook销售流量? - How to track Facebook sale traffic in GA (gtag.js)? 使用gtag.js跟踪虚拟网页浏览量 - Use gtag.js to track virtual pageviews Google Analytics 脚本片段 analytics.js 和 gtag.js 之间的区别 - Difference between Google Analytics script snippets analytics.js and gtag.js 结合Google Analytics(gtag.js)和Google跟踪代码管理器(gtm.js) - Combining Google Analytics (gtag.js) and Google Tag Manager (gtm.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM