Google Analytics: How to track pages in a single page application?

Derek 朕會功夫 picture Derek 朕會功夫 · Mar 9, 2012 · Viewed 16k times · Source

Currently in my website, I used HTML5's pushState() and popState in links to increase the speed. However, this doesn't really change the real URL and it looks like it will affect and mess up the Google Analytics's code. (doesn't show a url change) Is there a possible solution for this? Thanks,

Answer

Derek 朕會功夫 picture Derek 朕會功夫 · Mar 9, 2012

If you are using the newer analytics.js API, Google's documentation requires the following code to trigger the event:

ga('send', 'pageview', '/some-page');

If you are using the older ga.js API, David Walsh suggests AJAX websites to use the _gaq.push method:

_gaq.push(['_trackPageview', '/some-page']);