新Google Analytics(analytics.js)设置中未跟踪事件

时间:2022-01-13 15:21:30

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:

我有一个网站,我正在使用新的通用分析(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.

它位于 标记之前。

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 doc that Google provides does not provide a whole lot of explanation I'm at a loss here.

Google Analytics的“事件”部分中没有任何内容。我一直点击按钮,即使是从不同的计算机,只是为了确保它不排除我的IP地址。由于Google提供的Google Analytics(分析)文档并未提供大量解释,因此我不知所措。

14 个解决方案

#1


54  

If you are using Google Tag Manager and 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跟踪代码管理器并且还希望通过代码触发某些事件,则ga('发送'...)似乎不够。您需要先获取适当的分析对象:

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跟踪代码。如果您碰巧使用多个,则可能需要按名称或索引获取适当的一个。

#2


4  

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.

更新:逗号丢失。

#3


3  

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”)。现在可以正确捕获事件。

#4


2  

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 synchronous analytics API. So instead of including the usual snippet in your <head> tag, use the following code:

我必须做的是明确使用同步分析API。因此,不要在标记中包含常用的代码段,请使用以下代码:

<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 acknowledged before the page the user navigated to starts loading.

这将确保跟踪信标发送给Google并在用户导航到的页面开始加载之前得到确认。

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),这可能解释了为什么更多的开发人员没有注意到这个问题。

#5


2  

In my case the problem was uBlock Origin that was blocking the analytics script from loading.

在我的情况下,问题是阻止分析脚本加载的uBlock Origin。

#6


1  

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提供了很多有用的方法来找出Google Analytics实施的错误...

#7


0  

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调试,发现了一个问题;第一种方法不起作用(发送事件没有附加任何数据),但第二种方法是OK。

#8


0  

You should also consider that it is likely that the page gets reloaded after the submit event was fired before the ga script was able to execute the 'send' method. To avoid this you could employ the 'hitCallback' mechanism, i.e. prevent the submit, call the ga send-method and submit the form data in the callback.

您还应该考虑在ga脚本能够执行'send'方法之前触发提交事件后页面可能会重新加载。为避免这种情况,您可以使用'hitCallback'机制,即阻止提交,调用ga send-method并在回调中提交表单数据。

#9


0  

I got it working - my example is using the new Universal Analytics.

我得到了它 - 我的例子是使用新的Universal Analytics。

<script type="text/javascript">
    function sliderOnChange() {
    var period = window.convertDays(($("#PeriodSlider").slider("value")));
    var amount_of_credit = $("#AmountOfCreditSlider").slider("value");
    var gaEventInput = "£" + amount_of_credit + " for " + period;
    ga('send', 'event', 'slider', 'sliding', gaEventInput);
}
</script>
  1. Make sure Google Analytics / Google Tag Manager filters are not excluding any traffic from different domain. (Maybe you are testing it to get this working using different domain)
  2. 确保Google Analytics / Google跟踪代码管理器过滤器不排除来自其他域的任何流量。 (也许你正在测试它以使用不同的域工作)

  3. Recheck your GA id and domain in ga('create', 'UA-39570713-1', 'site.com');
  4. 在ga('create','UA-39570713-1','site.com')中重新检查您的GA ID和域名;

  5. Create a new profile in Google Analytics (GA) for testing purposes and debug your html in the same domain you define in GA.
  6. 在Google Analytics(GA)中创建新的配置文件以进行测试,并在您在GA中定义的同一域中调试html。

  7. Change the date to be today in GA - you might also need to wait some time before it appears in GA
  8. 在GA中更改今天的日期 - 您可能还需要等待一段时间才能在GA中出现

#10


0  

In my case it didn't work because I loaded the HTML file directly from the file system.

在我的情况下它没有用,因为我直接从文件系统加载HTML文件。

Loading the page via a web server did the trick.

通过Web服务器加载页面就可以了。

For local development a tool like https://github.com/tj/serve does a great job.

对于本地开发,像https://github.com/tj/serve这样的工具做得很好。

#11


0  

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

#12


0  

I recommend sending GTM event via window.dataLayer.push({ event: 'EVENT_NAME', ...data }) and in GTM creating a trigger to fire a tag which sends event to Google Analytics. You'll have the best debugging experience with GTM preview and you'll be sure that events will be sent from GTM to GA, because GTM takes care of that.

我建议通过window.dataLayer.push({event:'EVENT_NAME',... data})发送GTM事件,并在GTM中创建触发器以触发将事件发送到Google Analytics的标记。您将获得GTM预览的最佳调试体验,并且您将确保将事件从GTM发送到GA,因为GTM负责这一点。

#13


-2  

The only way I solved the problem was rolling back to the previous version of Analytics (non-beta):

解决问题的唯一方法是回滚到之前版本的Analytics(非beta):

<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>

#14


-3  

You can also use a jquery plugin I wrote for the new Universal Analytics: https://github.com/pascalvgemert/jquery-analytics-event-tracking

您还可以使用我为新的Universal Analytics编写的jquery插件:https://github.com/pascalvgemert/jquery-analytics-event-tracking

#1


54  

If you are using Google Tag Manager and 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跟踪代码管理器并且还希望通过代码触发某些事件,则ga('发送'...)似乎不够。您需要先获取适当的分析对象:

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跟踪代码。如果您碰巧使用多个,则可能需要按名称或索引获取适当的一个。

#2


4  

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.

更新:逗号丢失。

#3


3  

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”)。现在可以正确捕获事件。

#4


2  

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 synchronous analytics API. So instead of including the usual snippet in your <head> tag, use the following code:

我必须做的是明确使用同步分析API。因此,不要在标记中包含常用的代码段,请使用以下代码:

<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 acknowledged before the page the user navigated to starts loading.

这将确保跟踪信标发送给Google并在用户导航到的页面开始加载之前得到确认。

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),这可能解释了为什么更多的开发人员没有注意到这个问题。

#5


2  

In my case the problem was uBlock Origin that was blocking the analytics script from loading.

在我的情况下,问题是阻止分析脚本加载的uBlock Origin。

#6


1  

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提供了很多有用的方法来找出Google Analytics实施的错误...

#7


0  

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调试,发现了一个问题;第一种方法不起作用(发送事件没有附加任何数据),但第二种方法是OK。

#8


0  

You should also consider that it is likely that the page gets reloaded after the submit event was fired before the ga script was able to execute the 'send' method. To avoid this you could employ the 'hitCallback' mechanism, i.e. prevent the submit, call the ga send-method and submit the form data in the callback.

您还应该考虑在ga脚本能够执行'send'方法之前触发提交事件后页面可能会重新加载。为避免这种情况,您可以使用'hitCallback'机制,即阻止提交,调用ga send-method并在回调中提交表单数据。

#9


0  

I got it working - my example is using the new Universal Analytics.

我得到了它 - 我的例子是使用新的Universal Analytics。

<script type="text/javascript">
    function sliderOnChange() {
    var period = window.convertDays(($("#PeriodSlider").slider("value")));
    var amount_of_credit = $("#AmountOfCreditSlider").slider("value");
    var gaEventInput = "£" + amount_of_credit + " for " + period;
    ga('send', 'event', 'slider', 'sliding', gaEventInput);
}
</script>
  1. Make sure Google Analytics / Google Tag Manager filters are not excluding any traffic from different domain. (Maybe you are testing it to get this working using different domain)
  2. 确保Google Analytics / Google跟踪代码管理器过滤器不排除来自其他域的任何流量。 (也许你正在测试它以使用不同的域工作)

  3. Recheck your GA id and domain in ga('create', 'UA-39570713-1', 'site.com');
  4. 在ga('create','UA-39570713-1','site.com')中重新检查您的GA ID和域名;

  5. Create a new profile in Google Analytics (GA) for testing purposes and debug your html in the same domain you define in GA.
  6. 在Google Analytics(GA)中创建新的配置文件以进行测试,并在您在GA中定义的同一域中调试html。

  7. Change the date to be today in GA - you might also need to wait some time before it appears in GA
  8. 在GA中更改今天的日期 - 您可能还需要等待一段时间才能在GA中出现

#10


0  

In my case it didn't work because I loaded the HTML file directly from the file system.

在我的情况下它没有用,因为我直接从文件系统加载HTML文件。

Loading the page via a web server did the trick.

通过Web服务器加载页面就可以了。

For local development a tool like https://github.com/tj/serve does a great job.

对于本地开发,像https://github.com/tj/serve这样的工具做得很好。

#11


0  

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

#12


0  

I recommend sending GTM event via window.dataLayer.push({ event: 'EVENT_NAME', ...data }) and in GTM creating a trigger to fire a tag which sends event to Google Analytics. You'll have the best debugging experience with GTM preview and you'll be sure that events will be sent from GTM to GA, because GTM takes care of that.

我建议通过window.dataLayer.push({event:'EVENT_NAME',... data})发送GTM事件,并在GTM中创建触发器以触发将事件发送到Google Analytics的标记。您将获得GTM预览的最佳调试体验,并且您将确保将事件从GTM发送到GA,因为GTM负责这一点。

#13


-2  

The only way I solved the problem was rolling back to the previous version of Analytics (non-beta):

解决问题的唯一方法是回滚到之前版本的Analytics(非beta):

<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>

#14


-3  

You can also use a jquery plugin I wrote for the new Universal Analytics: https://github.com/pascalvgemert/jquery-analytics-event-tracking

您还可以使用我为新的Universal Analytics编写的jquery插件:https://github.com/pascalvgemert/jquery-analytics-event-tracking