Events not being tracked in new Google Analytics (

2019-02-08 00:10发布

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:

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

$('#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.

14条回答
We Are One
2楼-- · 2019-02-08 00:24

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

Loading the page via a web server did the trick.

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

查看更多
放我归山
3楼-- · 2019-02-08 00:26

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.

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:

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

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.

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.

查看更多
何必那么认真
4楼-- · 2019-02-08 00:28

For testing purposes you could also use the hitCallback method:

ga('send', {
  'hitType': 'event',         
  'eventCategory': 'button', 
  'eventAction': 'click',     
  'eventLabel': 'contact form',
  'hitCallback' : function () {
      alert("Event received");
   }
});

Update: comma was missing.

查看更多
\"骚年 ilove
5楼-- · 2019-02-08 00:28

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.

查看更多
何必那么认真
6楼-- · 2019-02-08 00:29

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

查看更多
劫难
7楼-- · 2019-02-08 00:30

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:

<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, ...)

Reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers

查看更多
登录 后发表回答