Wednesday, October 11th, 2006

Tracking Ajax Requests in Analytics

Category: Google

People often complain about tracking Ajax requests in web stats programs. If you are crunching server logs it is a non-issue since all of those requests are seen exactly the same as a ‘normal’ web request.

It comes into play with JavaScript based tracking.

Google Analytics has a solution:

With a typical HTML page, you can use the URL to differentiate between
multiple pageviews. But in an AJAX application, a request to the
server is made without changing the URL of the page, making it
difficult to track.

By calling Analytics’ urchinTracker function, however, you can assign
a page filename to any AJAX event. Typically, this is done as part of
the onreadystatechange function, after the data has been returned and all updates to the page have been made. The
example below shows one way of doing this:

http_request.onreadystatechange = sendAlert;‘GET’, url, true);


function sendAlert() {

if (http_request.readyState == 4) {

if (http_request.status == 200) {

urchinTracker(“/pagefilename1” );
} else {



The urchinTracker variable (/pagefilename1 in the example above) can be any value, which will be displayed as a page filename in your Analytics reports.

Important: if your pages include a call to urchinTracker(), utmLinker(), utmSetTrans(), or utmLinkPost(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.

Posted by Dion Almaer at 8:00 am

3.7 rating from 23 votes


Comments feed TrackBack URI

I’ve been using this technique and it’s works great.

Comment by Michael — October 11, 2006

Hmm.. I ment:
<a href=”link to external site” onclick=”return urchinTracker(this.href)”>External link<a>

Comment by CiPHER — October 11, 2006

This also means that when Google’s servers are down, your website get very slow. The Analytics scripts in this case, have to be loaded before your page starts displaying, or am I wrong?

Comment by Tinus — October 11, 2006

This begs an interesting question of why not do JS based analytics yourself? Not meaning write it yourself but at least host it yourself. Certainly we have had the tools to do so particularly with Ajax and now we need to insert the appropriate calls anyway. If you did your own you avoid the load issue concerns.

I think the main reason these outsource analytics firms exist is because marcom people can’t get reasonable data because they are limited access to servers or their IT people are too busy. It is just too easy for them to jam a .js file call in their page and be on their way, but in many ways from privacy to delivery it just isn’t the right way to do it. Cost certainly isn’t the issue either folks because until things like Google Analytics came along lots of these stats services like Omniture or HBX cost huge money ($1000s per month) and many still do. So purchase or implement in some cases might even be cheaper and maybe the rise of Ajax and the analytics implications from it will drive a new trend?

Comment by Matt Lowell — October 11, 2006

[…] [::[ Wed 11 Oct 2006 ]::[ General ]::[ ]::] I just read a post over at on how to use Google Analytics online, another to track Ajax Pages. Google Analytics is a really good tool for Web 1.0 fashioned pages but is terribly wrong for Web 2.0, first of all a pageview cannot be translated to anything in an Ajax Page. Is a refresh of a part of the page, let’s say the user list currentlypageview or is only the change of the main content a new pageview? Google Analytics themselfs suggest that you call the urchinTracker upon every successful XMLHttpRequest, but I believe that this is really dangerous. By triggering too many pageviews we first of all obscure the data we are actually interested in (how do you distinguish a single content reload among thousands of minor updates of some small widget?) and you significantly reduce the speed of your application by generating additional overhead. So my advise: although it might be tempting to use things like Prototype’s Ajax.Responder to trigger the urchinTracker function, don’t! It generates a huge overhead and actually obscures your statistics.Technorati Tags: Ajax, Google, Google Analytics, Statistics […]

Pingback by Fading Roses & Raging Viruses » — October 11, 2006

[…] 很酷的图像切换效果 Tracking Ajax Requests in Analytics […]

Pingback by IT快餐——–发现,分享,关注互联网 » 10-13 — October 13, 2006

I’m working on a new analytics platform explicitly designed for Ajax applications – – and I’d love to hear what you have to say about it.

Comment by Alex — April 8, 2007

Leave a comment

You must be logged in to post a comment.