Using WP Statistics with Barba.js (or other JS Page Transitions)

If your WordPress site uses a JavaScript-based page-transition library (for example Barba.js) which updates content without full page reloads, then the normal tracking of WP Statistics may only run on the initial load. This guide shows how to ensure WP Statistics continues to track page-views correctly after JS page transitions.

Why this matters

When using Barba.js (or similar), the browser may not trigger a full document load on each “page” navigation. As a result:

Step-by-step solution

1. Ensure the tracker script is loaded and remains active

• Confirm WP Statistics’ tracker.js is injected in a way that persists across transitions (or re-injected after each transition).
• If your theme/minifier/optimizer caches or defers JS, exclude tracker.js from being modified.
• (Optional) See our doc “How to Exclude WP Statistics tracker.js from Caching & Minification”.

2. Hook into Barba.js lifecycle

In your Barba.js configuration, use the afterEnter (or equivalent) event to manually trigger WP Statistics tracking of the “new page”.

barba.init({
  transitions: [{
    name: 'default-transition',
    afterEnter({ next }) {
      if (typeof wpStatisticsTracker === 'object') {
        wpStatisticsTracker.trackPageView(next.url);
      }
    }
  }]
});

Replace wpStatisticsTracker.trackPageView with the actual function name your setup uses.
If WP Statistics provides a JS API or custom event (check your version) use that instead.

3. Disable duplicate tracking on initial load (if necessary)

To avoid counting the same initial load twice (once by default, once via your custom trigger), you might disable the default auto-track on document.ready and rely only on your custom trigger.
Alternatively, check the URL change (via Barba.js) before firing the custom event.

4. Clear caching and test

After implementing the script, clear any server/plugin caching, disable/minify or exclude tracker.js, then test the transitions and inspect WP Statistics → Visitors or Hits report to confirm each transition is recorded.
Use browser DevTools (Network tab + Console) to verify the tracking request fires on each transition.

Troubleshooting

Version compatibility & notes

• This guide assumes WP Statistics version 14.x (with client-side tracking) or above.
• Works with Barba.js version 2+ (or equivalent) using afterEnter.
• If you use any other JS page-transition library (e.g., PJAX, Turbolinks) adapt the lifecycle hook accordingly (e.g., onContentReplacedpjax:end, etc).

Summary

By integrating WP Statistics’ tracking into your JS-based page transition lifecycle, you ensure accurate analytics even when full page reloads don’t happen. Follow the steps above so every “virtual page” view is counted correctly.

Let’s get started
Take your business to next level

Become part of our growing family of +600,000 users and get the tools you need to make smart choices for your website. Simple, powerful insights are just a click away.