Google Analytics: Track Page View during Ajax Call

Gary Oosterhuis | August 2, 2014


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.

Add a Comment

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