jQuery 在新的 Google Analytics (analytics.js) 设置中未跟踪事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15744042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Events not being tracked in new Google Analytics (analytics.js) setup
提问by squeezemylime
I have a website that I am using the new Universal Analytics (analytics.js) to track. Everything is setup and working (pageviews, referrals, etc.) using the following code snippet:
我有一个网站,我正在使用新的 Universal Analytics (analytics.js) 进行跟踪。一切都使用以下代码片段进行设置和工作(综合浏览量、推荐等):
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-39570713-1', 'site.com');
ga('send', 'pageview');
</script>
That is located before the </head>
tag.
它位于</head>
标签之前。
I am using JQuery to fire off an event. I tested the JQuery with an alert message and it is getting called, so that isn't the problem. Here is the snippet that fires when a button is clicked:
我正在使用 JQuery 来触发一个事件。我用一条警告消息测试了 JQuery,它被调用了,所以这不是问题。这是单击按钮时触发的代码段:
$('#submitButton').on('click', function() {
ga('send', 'event', 'button', 'click', 'contact form');
});
Nothing is appearing in the Events section of Analytics. I keep clicking the button, even from different computers just to make sure it isn't excluding my IP address. Because the Analytics docthat Google provides does not provide a whole lot of explanation I'm at a loss here.
Analytics 的“事件”部分未显示任何内容。我一直点击按钮,即使是在不同的计算机上,只是为了确保它不排除我的 IP 地址。因为谷歌提供的分析文档没有提供很多解释,我在这里不知所措。
采纳答案by squeezemylime
The only way I solved the problem was rolling back to the previous version of Analytics (non-beta):
我解决问题的唯一方法是回滚到以前版本的 Analytics(非测试版):
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-39570713-2']);
_gaq.push(['_setDomainName', 'optimino.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
回答by Sanjay Vamja
If you are using Google Tag Managerand also want to trigger some events via code, ga('send'...)
does not appear to be enough. You need to first fetch the appropriate analytics object:
如果您正在使用Google Tag Manager并且还想通过代码触发一些事件,ga('send'...)
这似乎还不够。您需要首先获取适当的分析对象:
if ("ga" in window) {
tracker = ga.getAll()[0];
if (tracker)
tracker.send("event", "Test", "Test GA");
}
Note that this assumes you're only using a single Google Analytics Tracking code on your site. If you happen to be using multiple, you may need to fetch the appropriate one by name or index.
请注意,这假设您仅在您的网站上使用一个 Google Analytics 跟踪代码。如果您碰巧使用多个,则可能需要按名称或索引获取适当的一个。
回答by afelaho
New version of analytics has a new syntax. Replace the line below;
新版本的分析具有新的语法。替换下面的行;
ga('send', 'event', 'button', 'click', 'contact form');
with this;
有了这个;
gtag('event', 'click', {'event_category' : 'button',
'event_label' : 'contact form'});
Reference; https://developers.google.com/analytics/devguides/collection/gtagjs/events
参考; https://developers.google.com/analytics/devguides/collection/gtagjs/events
回答by Dom
For testing purposes you could also use the hitCallback method:
出于测试目的,您还可以使用 hitCallback 方法:
ga('send', {
'hitType': 'event',
'eventCategory': 'button',
'eventAction': 'click',
'eventLabel': 'contact form',
'hitCallback' : function () {
alert("Event received");
}
});
Update: comma was missing.
更新:缺少逗号。
回答by extrabacon
I had the exact same problem. I had to create a new property and select "Universal Analytics" instead of "Classic Analytics" (it is labeled as "beta"). Now events are captured properly.
我有同样的问题。我必须创建一个新属性并选择“Universal Analytics”而不是“Classic Analytics”(它被标记为“beta”)。现在事件被正确捕获。
回答by user1139733
In my case the problem was uBlock Origin that was blocking the analytics script from loading.
就我而言,问题是 uBlock Origin 阻止了分析脚本的加载。
回答by coredumperror
I had this same problem, and I think I've found the solution, but it really leaves a bad taste in my mouth about Universal Analytics.
我遇到了同样的问题,我想我已经找到了解决方案,但它确实让我对 Universal Analytics 产生了不好的印象。
What I had to do was explicitly use the synchronousanalytics API. So instead of including the usual snippet in your <head>
tag, use the following code:
我必须做的是明确使用同步分析 API。因此,不要在您的<head>
标签中包含通常的代码段,而是使用以下代码:
<script src="//www.google-analytics.com/analytics.js"></script>
<script>
tracker = ga.create('UA-XXXXXXX-1', 'example.com');
tracker.send('pageview');
</script>
Then you call the event tracking code like this:
然后像这样调用事件跟踪代码:
tracker.send('event', 'Category', 'Action', 'Label');
This will ensure that the tracking beacon is sent to Google and acknowledgedbefore the page the user navigated to starts loading.
这将确保跟踪信标被发送到谷歌并在用户导航到的页面开始加载之前得到确认。
This suggests that Universal Analytics requires some kind of additional acknowledgment beyond what the old ga.js
analytics code required. So when you attach an event to a click that brings the user to another page, that acknowledgement can't be sent because the browser has left the page and dropped the current javascript execution stack.
这表明 Universal Analytics 需要某种超出旧ga.js
分析代码所需的额外确认。因此,当您将事件附加到将用户带到另一个页面的点击时,无法发送该确认,因为浏览器已离开页面并丢弃当前的 javascript 执行堆栈。
Maybe this problem is specific to certain execution environments (I'm using Chrome 34 on OSX Mountain Lion), which might explain why more developers aren't noticing this problem.
也许这个问题是特定于某些执行环境的(我在 OSX Mountain Lion 上使用 Chrome 34),这可能解释了为什么更多的开发人员没有注意到这个问题。
回答by Alejandro Cuenca Estrada
today I needed to setup analythics for the first time and I found myself in the same trouble.
今天我第一次需要设置分析,我发现自己遇到了同样的麻烦。
I found that the bast way to deal with the multiple trackers to avoid the getAll()
, is this:
我发现处理多个跟踪器以避免getAll()
, 的最佳方法是:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxx-y', 'auto', 'tracker');
ga('tracker.send', 'pageview');
ga('tracker.send', 'event', 'test', 'click', 'automaticEvent')
Note that you have to pass a "name" to the create method, and then you send an event to that tracker with ga([trackerName].send, ...)
请注意,您必须将“名称”传递给 create 方法,然后使用以下命令将事件发送到该跟踪器 ga([trackerName].send, ...)
Reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers
参考:https: //developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers
回答by Albedo
For GA for this moment... Sending a new page in SPA looks finally like this for me:
对于这一刻的 GA ......在 SPA 中发送一个新页面对我来说最终看起来像这样:
if (window.ga){
window.ga.getAll()[0].set('page', location);
window.ga.getAll()[0].send('pageview')
}
This shows exactly what wanted on GA reports like a new page is hit and the title and all are correct.
这准确地显示了 GA 报告中想要的内容,例如点击了新页面,标题和所有内容都是正确的。
回答by Petr Havlik
I cannot see anything wrong with the code itself. Have you tried using the alternative event tracking?
我看不出代码本身有什么问题。您是否尝试过使用替代事件跟踪?
ga('send', {
'hitType': 'event', // Required.
'eventCategory': 'button', // Required.
'eventAction': 'click', // Required.
'eventLabel': 'contact form'
});
I would also suggest testing the website with GA Debug Chrome addon, which allows you to see the tracking beacon was sent or not.
我还建议使用 GA Debug Chrome 插件测试网站,它允许您查看是否发送了跟踪信标。
"Official" debugging documentation for Universal Analytics is still missing as of now, but hopefully it will be added soon as ga_debug.js provides lot of useful ways how to find out what's wrong with Analytics implementation...
到目前为止,Universal Analytics 的“官方”调试文档仍然缺失,但希望它会尽快添加,因为 ga_debug.js 提供了许多有用的方法来找出 Analytics 实现的问题......
回答by Yuriy Silvestrov
I have the same problem, and it looks like events are tracked, but GA dashboard doesn't allow to browse them. This is the only way how I could interprete the "Visits with events: 1071" but "Total events: 0" that GA dashboard shows me.
我有同样的问题,看起来事件被跟踪,但 GA 仪表板不允许浏览它们。这是我如何解释 GA 仪表板向我显示的“事件访问次数:1071”但“事件总数:0”的唯一方法。
UPD: With GA Chrome debug, have found a problem; 1st method is not working (sends the event without any data attached), but the 2nd one is OK.
UPD:用GA Chrome调试,发现问题;第一种方法不起作用(发送没有附加任何数据的事件),但第二种方法正常。