Tracking Ajax Calls in Google Analytics

JAN
31

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.


Leave a Reply

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

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box

Your email address will not be published.