diggersworld diggersworld - 2 months ago 11x
jQuery Question

Using history.js with Google Analytics

Looking at the options listed on this page: https://github.com/browserstate/history.js

It would appear there is no way to set the plugin to sent page view data to Google Analytics.

This makes it hard to deduce where users are going when viewing AJAX powered pages using the


I know that other plugins have had this implemented such as
jQuery Address
: http://www.asual.com/jquery/address/ . I would have expected the history plugin to have this too.

Is there a way to enable Google Analytics with


A similar question has already been answered here: http://stackoverflow.com/a/5307532/1854499

History.js is more of a wrapper for the HTML5 History API, so the answer is the same.

After you've determined which page you'll show when the history state changes (in the statechange callback), push another _trackPageview call to the Google Analytics stack like so:

 _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
 _gaq.push(['_trackPageview', '/new/content']);

Don't forget to add the new URL as the second argument.

You can find the documentation on these so called 'virtual pageviews' on this page (scroll to the bottom) on Google Developers.

Good luck!