I am using PJAX https://github.com/defunkt/jquery-pjax and I was wondering since the whole page does not change what would be the best way to track analytics with google analytics?
The accepted answer is no longer valid because as Ruy Diaz commented, this was removed from PJAX in this commit.
Here is my solution. On the pjax:end event, set the GA location, and send a pageview.
The pjax:end event is used because it is triggered both "upon following a pjaxed link" (loading from server) and "on back/forward navigation" (loading from cache). See pjax events documentation
$(document).on('pjax:end', function() {
ga('set', 'location', window.location.href);
ga('send', 'pageview');
});
Or using the old version of GA
$(document).on('pjax:end', function() {
if( window._gaq ) {
_gaq.push(['_trackPageview', window.location.href]);
}
});
I had to manually set the location variable because it wasn't picking up that it changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With