Adam Howitt's Blog

Nov 22
2005

Using Google Analytics to Monitor AJAX Applications

In this post, I'm going to explain how  to use Google Analytics to help you monitor AJAX and DHTML activities on your web applications.

Just over a week ago Google launched Google Analytics as a free, tag based web analytics package for one and all to use.  The product is based on Urchin which Google purchased for $30 million at the end of March 2005. 

This means you can create a free account, copy the basic snippet of code

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">

</script>
<script type="text/javascript">
_uacct = "UA-xxxxx-x";
urchinTracker();
</script>

and within 6-12 hours be looking at some great stats.  My personal favorite is that it is goal centered.  It makes you think about what the goal is for your website (readership, whitepaper downloads, sales, etc.) and focus on tracking those behaviors.

With traditional log based analytics packages you only get to see requests for files but with tag based analytics packages, like Google Analytics, you can monitor each and every JavaScript action, each click away from your site and tweak your application based on what your visitors are doing. 

I have an AJAX searWalkJogRun.net search boxch box on WalkJogRun.net which up until last week I had no idea how it was used by visitors.  I mean, sure, it said search but people should know what that means, right?  Entering a search term dynamically filters the list of routes in the panel beneath the search box without refreshing the page.

Based on the results of tracking this for a week, it now reads "Find a route by name or zip code" because I was able to see that the most common behavior was to enter a zip code as a search term which, until I realized this, was a dead end.  The search was only looking for keywords so a zip search would find no results here.  A simple change and now it recognizes zip codes and handles your request differently.

Okay, but how do you track events?

Once you have your Google Analtics account and have copied the snippet into the head of your web page you are one line of code away from the answer.  Any time you want to log an event or activity to Google Analytics, add a call to the urchinTracker function which expects the name of a page (or for the purpose of this example, a virtual page since the search activity isn't a real page...  You can do this anywhere you can put JavaScript in a page including your own methods or, say, the onClick property of an href.  Since my application is already calling a JavaScript function to handle the AJAX call, I added the following line to my file:

urchinTracker('/search_routes/'+term);
which logs the event under a virtual directory called /search_routes/ and then logs the actual search term used under that.  To access this information in the Google Analytics manager when you are looking at your reports, click on "All Reports" > "Content Optimization" > "Content Performance" > "Content Drilldown" and you should see a pie chart and something like this:

 

Google Analytics snapshot of Content Drilldown You can see the /search_routes folder here and that 119 visitors have used the search tool 184 times between them.  Click on the folder icon next to the label "search_routes" and you see the next layer down:

Google Analytics Drilldown of search terms used The % Exit column reflects the damage that was done by providing a search box with no results when you search by zipcode! 

What else can it do?

Since every href can have an onClick event, you can log each click away from your site under a virtual folder called /outgoing_links/ to monitor which links your visitors found the most interesting.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. Have you had any experience with the new tracker? I'm guessing there's a similar technique you could use with it, but I haven't dug into the guts of ga.js

  2. Hi Jason, The event tracker is still in beta at this point (unless I'm mistaken) and I wasn't granted access to it. I have tagged the app ready for when it goes live but will have to wait for now. I can tell you once it becomes available but the integration was pretty straightforward.

    Adam

  3. PAY for information that's freely available elsewhere? You got to be kidding me!

  4. If you had left your email address and name blaahblaah you would have a PDF in your hands now. I'm not unreasonable and if you were honestly saying this isn't worth $5 I would appreciate your honesty and send you a copy.

    I figure that if one of my blog posts saves you half an hour of Googling or reading the free documentation to workout how to this it's worth $5. You can read the comments to see if the answer is relevant or helpful and make the choice.

[Add Comment]