Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Track AJAX Link Clicks Using Google Analytics

11 Responses »

About a year back I created an advanced form for a customer that took in numerous user-given values and calculated some totals using JavaScript. No page refresh, just straight JavaScript and AJAX functionality. Obviously since there's no page refresh, Google Analytics doesn't track the form's usage. I changed all that a few days ago, using some simple MooTools code to control Google Analytics.

The MooTools 1.2

window.addEvent('load', function() {
	if(pageTracker) {
		$('submitButton').addEvent('click',function() {
			pageTracker._trackPageview('/calculator-clicks/my-calulator');
		});
	}
});

Once someone clicks the button, I instruct the Google Analytics page tracker to add the click. If I go to the "Content Overview" page, I see "/calculator-clicks/my-calulator" in the URL list. Awesome!

Do any of you use this type of tracking?

Discussion

  1. christof haemmerle
    April 6, 2009 @ 12:12 am

    you want to change want to check on the parent object(here window) if it has a function called pageTracker otherwise it will break your app.

    if(window.pageTracker)

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!