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.
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.
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:Then you call the event tracking code like this:
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.
For testing purposes you could also use the hitCallback method:
Update: comma was missing.
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.
In my case the problem was uBlock Origin that was blocking the analytics script from loading.
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: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