Tracking Ajax Calls in Google Analytics

Gary Oosterhuis | January 31, 2013


With ajax becoming more popular its important to continue to track all page views. Quite often we find ourselves developing ajax functionality such as a popup form or a multi-step signup form.

Tracking these events are not as complicated as you might think.

Step 1.

Include the usual Google Analytics tracking code in the <head> section of the page containing the ajax call.

Step 2.

Add an onclick event to your submit button:

<input type = "submit" value = "Submit Form" onclick = "_gaq.push(['_trackPageview', '/fake-page.php']);" />

Or, include the tracking code within your form validation function:

<script type="text/javascript">
function form_validation() {
// validate form, then;
_gaq.push(['_trackPageview', '/fake-page.php']);
}
</script>

You can change /fake-page.php to any file name. Make sure that this file doesn’t actually exist on the server. Google will then report this action/event as a page view for the fake file name created.

Add a Comment

Your email address will not be published. Required fields are marked *