I am trying to log JavaScript events such as AJAX and click events using Google Analytics.
I have done some reading on Google Analytics and it seems I need to use _trackPageView and _trackEvent. I have put this is my code as a JavaScript function that I call as needed. Here is the code:
var pageTracker = _gat._getTracker();
pageTracker._trackPageview(url);var pageTracker = _gat._getTracker();
pageTracker._trackEvent(category, action, item);
I have verified using FireBug that the HTTP requests are going out successfully when I call this chunk of JavaScript code and I am not receiving any errors. However, the page views and events that I am logging using this code is not working. I do not see any entries in the analytics reports.
Update Adding my google analytics code in order to help with versioning:
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘XXXXXX’]);
_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);
})();
If you are using the Async tracking code then your trackevent code should look like this:
rather than this
It is confusing because the help documents don’t seem to be fully updated. The top of the help document for this feature still uses the old format but if you keep going down you will see the rest of the samples have been updated correctly: