当 Google Analytics 由 Google Tag Manager 管理时如何跟踪自定义事件? [英] How to track custom events when Google Analytics is managed by Google Tag Manager?

查看:20
本文介绍了当 Google Analytics 由 Google Tag Manager 管理时如何跟踪自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最终不得不为我的网站使用 Google 标签管理器,因此 Google Analytics 现在是其中的一部分.一切都设置正确并且工作正常.但是,我曾经能够非常轻松地跟踪我网站上的自定义事件,使用ga() 函数:

I ended up having to use Google Tag Manager for my site, so Google Analytics is now a part of that. It's all set up correctly and working fine. However, I used to be able to track custom events on my site very easily, with the ga() function:

ga('send', 'event', 'Videos', 'play', 'Fall Campaign');

然而,现在ga()不再被定义;我在控制台中收到一个错误,ReferenceError: ga is not defined.然后我尝试了 gtag() 方法,但它也不起作用(相同错误信息):

However, now ga() is no longer defined; I get an error in the console, ReferenceError: ga is not defined. I then tried the gtag() method and it also doesn't work (same error message):

gtag('event', 'aaa', {
  'event_category' : 'bbb',
  'event_label' : 'ccc'
});

如何使用 Javascript 代码跟踪事件?

How can I track events with Javascript code?

明确地说,我不想对 Google 标签管理器大惊小怪.在那里完成任何事情需要一百万次点击.;-) 我只想直接调用 Javascript,就像我以前一样.

To be clear, I do NOT want to fuss with the Google Tag Manager. It's a million clicks to get anything done in there. ;-) I just want to call the Javascript directly, like I always could before.

推荐答案

我在标签管理器中创建了一个新的自定义 html"标签.然后我添加了下面的 analytics.js 代码并将标签设置为在页面加载时触发.这使我能够使用代码中现有的 ga 调用来触发自定义事件,而不是像其他答案建议的那样重写我的所有事件以通过 dataLayer 与 Universal Analytics 一起使用.

I created a new "custom html" tag in Tag Manager. Then I added the analytics.js code below and set the tag to fire on page load. This allowed me to use the existing ga calls in my code to fire custom events rather than re-writing all of my events to work with Universal Analytics through the dataLayer as the other answer suggested.

<!-- Google Analytics -->
<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-XXXXXXXXX-Y', 'auto');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

这篇关于当 Google Analytics 由 Google Tag Manager 管理时如何跟踪自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆