Using Google Analytics - New Snippet Code for AJAX

I was wondering why I couldn't track AJAX through analytics, because let's face it, the world we used to know as "1.0" and static HTML is going to slowly disappear into the abyss. One of the frustrations of AJAX, however, is knowing what your users are doing. You're not refreshing pages (that's so 2002), it's just live HTTP requests.

The first Google search for "urchin ajax" has a dead link. The article here now points to nothing. Also, the #1 result for "analytics ajax" talks about an outdated method using the old version of the Analytics code.

So I'll post an update.

1) You need to read the Google page on how to track AJAX applications.

2) You need to make sure you're using the NEWER urchin/analytics snippet code. The easy way to do this is to check the page that shows the two versions of the Google tracking code (you're most likely running the older stuff).

3) You need to start calling the pageTracker function every time you run an AJAX call, or at least the AJAX calls you want to track.

I'll give an example of what my code looks like. In my <head> tags, I'll call to my external JS file that holds all my AJAX calls, functions, etc. It's important to note that you won't load Google Urchin/Analytics until AFTER all of the <head> JS has been loaded.

Don't try to create DOM elements on the fly and do any immediate tracking. You can't access the pageTracker function until the DOM has fully loaded, including the <body> where your Analytics snippet is embedded.

Inside of every AJAX call I want to track, I'll put this at the very end of the code block (assuming the AJAX call was successful):


thisView:"what_ajax_call_or_view" });

initUrchin is simply a function that looks like this:


//initialize urchin stats for ajax calls

function initUrchin(parameters) {

var thisView = parameters['thisView'];

pageTracker._trackPageview(thisView); }