当Google跟踪代码管理器管理Google Analytics(分析)时,如何跟踪自定义事件? [英] How to track custom events when Google Analytics is managed by Google Tag Manager?

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

问题描述

我最终不得不为自己的网站使用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跟踪代码管理器管理Google Analytics(分析)时,如何跟踪自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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