We love open source and we invest in continuous learning. We give back our knowledge to the community.

José Costa

Google Analytics With AJAX


It’s been a while since us all have been working in fully AJAX enabled sites (as is the case of Indieoma(Indieoma), one of our projects). Time comes when you’d like to know a little bit about who is accessing the site, when, which page, why .. and a few more questions Google Analytics can answer for you (not particularly the last one) in a very simple manner most of you already know about.

You may encounter some issue when trying to check your stats for the AJAX requests people have made. As you can imagine, the code you place in your web pages won’t execute itself on each AJAX request, but more like on each entire page load. But Google doesn’t let us down and give us a tiny explanation( How do I track AJAX applications?) on how to pull this off.

At the heart of http://indieoma.com we use Prototype(Prototype) So Google’s tip may become more something like the code below:

  onComplete: function(request) {
    // Assuming the ga.js code was loaded first

Using the Ajax.Responders(Prototype Ajax.Responders), we register a function that will fire the tracker after each AJAX request has been completed.

The _trackPageview(request.url) allows us to name the tracked page exactly as the requested URL. Note that you can name this page in any way you like. We chose to do so with the requested URL in this case.

So, that’s it and happy analytics!