如何使用新的Google Analytics(GTAG.JS)跟踪和发送自定义事件?

发布于 2025-01-28 19:23:16 字数 1161 浏览 7 评论 0 原文

我有一个旧的网站,该网站正在使用旧的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发送自定义事件?

I have an old website that's using the old Google Analytics code from last decade, and I need help using the new GA4.

Old Code (2016)

To send custom events and pageviews to Google, I would use the global ga() function from their <script> snippet:

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

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

New Code (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():

<!-- 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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2025-02-04 19:23:16

推荐名称是“ event” “发送” /事件“ rel =“ nofollow noreferrer”>官方文档。另外,推荐参数应使用下划线,例如 event_category

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

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

如果您正在手动发送自定义页面浏览量,请确保在着陆时禁用初始页面浏览量以避免对其进行两次计数,如在标题&lt; script>&gt; 中添加 send_page_view:false

gtag('config', 'TAG_ID', {
    send_page_view: false
});

您可以通过打开网络选项卡并查看有效载荷来检查是否在本地发送某些内容每个收集网络请求:

”在此处输入图像描述“

sources:

Instead of using "send", the recommended name is "event" official docs. Also, the recommended parameters should use an underscore, like event_category:

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

// Pageview event
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>:

gtag('config', 'TAG_ID', {
    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:

enter image description here

Sources:

你的心境我的脸 2025-02-04 19:23:16

这是发送ga4的语法:

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

在这里您可以看到事件的文档。

另外,如果要测试命中率,并且如果发送了这些事件,则可以使用 google tag assisth < /a>对此进行审查。

This is the syntax to send an event with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文