开发者

How do I log ajax requests in Google Analytics?

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();

开发者_C百科 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:

_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);

rather than this

var pageTracker = _gat._getTracker();
pageTracker._trackEvent(category, action, item);

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:

  • http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜