Google Analytics: Track Page View during Ajax Call

AUG
02

You’ve probably added google analytics tracking code to the <head> section of every page of your website. This tracking code allows google to track page views and additional information when making requests for new pages, but what track a page view without loading a new page. A perfect example is when requesting data using ajax.

Google analytics allows you to push a page view even when a new page (from the browsers perspective) is not being requested.

The following code will allow you to push a page view to google . It allows you to pass a page url and a title. Both of which will appear within your Google Analytics Tracking.

<script type = "text/javascript">
ga('send', {
'hitType': 'pageview',
'page': '/popups/get-a-quote-form.php',
'title': 'Get a Quote Popup Viewed'
});
</script>

The page url does not need to exist to be able to push it to Google Analytics. You can provide any url.

This code is reliant on the generic Google Analytics tracking code added to the <head> section of the site. Ensure that it exists on all pages of your site.


2 thoughts on “Google Analytics: Track Page View during Ajax Call

  1. There is a tiny typo in the JavaScript.

    ga(‘send’, {
    ‘hitType’: ‘pageview’,
    ‘page’: ‘/popups/get-a-quote-form.php,
    ‘title’: ‘Get a Quote Popup Viewed’
    });

    There should be a quote here (I have place it in):
    v
    ‘page’: ‘/popups/get-a-quote-form.php’,

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.