Skip to content
Get started
All articles

Tracking engineering

SPA Pageview Tracking in React, Next.js, and Vue

Prevent missing or duplicate pageviews in single-page applications by tracking client-side route changes and testing navigation edge cases.

Updated

A left-to-right pipeline connects browser capture, collector acceptance, queue processing and report visibility, with a note that a server key stays on the server.

Quick answer

A single-page application can change routes without a full browser reload. Pageview tracking must observe those transitions, avoid duplicates on initial hydration, and decide how to handle virtual or transient URLs.

Key takeaways

  • Choose automatic navigation tracking or deliberate manual pageviews.
  • Initialize in the browser and clean up the owning handle.
  • Manual pageview calls are not automatically deduplicated.

Understand the route-change problem

Traditional analytics snippets often watch the initial page load. React, Next.js, and Vue routers may update history while the document remains loaded. Without explicit or SDK-supported route tracking, subsequent pageviews can disappear from reports.

Conversely, tracking both automatic navigation and a manual pageview call can double count the same route.

Define a pageview contract

Count a view when navigation reaches a meaningful, visible route. Decide whether hash changes, query-only updates, modal overlays, or redirects count. Normalize URLs to avoid exploding reports with user IDs or sensitive search parameters.

For server-side rendering, initialize browser-dependent tracking only where window and document exist.

Test the awkward navigation cases

Load a page directly, navigate forward, use Back and Forward, follow a redirect, refresh, and move through nested routes. Inspect event sequence and exact URL values. If tracking runs after asynchronous hydration, verify it does not count the initial route twice.

Keep consent choices and tracking opt-out behavior consistent across route changes.

Compare against an expected path

For a test journey of homepage → features → pricing, expect exactly three pageview events under a simple one-view-per-route policy. If the collector reports two or four, fix instrumentation before interpreting exit rates or funnels.

Choose one pageview owner in Revfaro

The npm SDK tracks the initial page and History API navigation automatically. If that behavior fits your application, initialize one shared tracker in the browser and do not also call pageview() on every router callback. The SDK guide includes React and Next.js lifecycle examples. Vue integrations should follow the same ownership rule and release the tracker when their owning integration unmounts.

For a router that needs explicit pageview control, use the documented manual mode:

import { init } from 'revfaro';

const tracker = init({
  siteId: 'YOUR_SITE_ID',
  endpoint: 'https://revfaro.com/api/event',
  autoTrack: false,
});

// Call once after the intended route is visible.
tracker.pageview();

This snippet belongs in a browser lifecycle, not a server render. Keep the same instance for future routes and goals, and call destroy() when its owner unmounts. Each manual call records a view, so your router must suppress duplicate notifications itself. Test initial hydration, client navigation, Back, Forward and private-only query changes before declaring the integration complete.

Related reading

Frequently asked questions

Does every SPA need manual pageview tracking?

Not necessarily. Some SDKs automatically observe router changes, but their behavior should be tested for your framework and configuration.

Should changing a filter query parameter count as a new pageview?

Only if your product's reporting definition treats that state as a meaningful new view.