Last Updated: February 25, 2016
·
1.158K
· msaspence

Track Mixpanel Page Views With jQuery pjax

We've been playing around with the jQuery pjax plugin for one of our latest projects to try and make page loads feel as snappy as possible. There were a few things that we weren't quite happy about the way the default implementation worked.

One of the things we lost was Mixpanel page views because only the initial page request initiated the Mixpanel library triggering the page view event. Luckily the jQuery pjax plugin has some global events to hook into and the Mixpanel JavaScript library can trigger page views manually, making it as simple as:

$(document).on('pjax:end', function (e) {
  mixpanel.track_pageview(window.location.pathname)
});

to get them back again.

Note: pjax:success might be a better event to hook into?