To load the snippet in React or Next.js, follow Install on React and Next.js. This page explains what happens after it's loaded.
How route changes are detected
The SDK watches the browser's history. It reacts when your router:
- calls
history.pushState()orhistory.replaceState()— how React Router, Next.js, Vue Router and most others navigate; - moves back or forward;
- changes the
#hash.
It compares the new full URL with the last one it tracked. Only a real change counts, so a router that calls replaceState with the same URL doesn't create a page view.
What happens on a route change
Feature | On the new route |
|---|---|
Page views | A Page View is sent with the new URL and page title. |
Campaigns | Widgets from the old route are removed, and the ones whose page rules match the new URL are placed. |
A/B tests | The old route's changes are cleared and the tests for the new URL run. A visitor keeps their variant. |
Popups , bars , quizzes | Checked again against the new URL. |
Session replay | Keeps recording the same visit, and stops if the new URL is on your list of pages never to record. |
Page titles
The page view takes document.title at the moment the URL changes. If your app sets the title after it renders the new route, the page view may carry the previous title. The URL is always correct.
Send page views yourself
If you'd rather decide when a page view is sent, turn automatic page views off and send your own event:
entailInsights.init('YOUR_SITE_TOKEN', {
disable_decide: false,
capture_pageview: false,
});
// After your route has rendered:
entailInsights.trackEvent('virtual-page-view', { path: location.pathname, title: document.title });Your own event isn't a Page View. Reports and goals built on Page View don't see it. Build those goals on your own event instead.
Tracking an action on a route
import { useEffect } from 'react';
export function CheckoutSuccess({ order }) {
useEffect(() => {
window.entailInsights?.trackEvent('purchase', { value: order.total, currency: order.currency, orderId: order.id });
}, [order.id]);
return <h1>Thanks for your order</h1>;
}- Install on React and Next.js: Load the snippet in a single-page app.
- Track custom events: Send your own events from your code.
- Automatically tracked events: Everything recorded without code.
Frequently asked questions
Do I need to send page views myself in React or Next.js?
No. The SDK detects route changes made through the browser's history (pushState, replaceState, back and forward) and hash changes, and sends a page view for each new URL.
Why do I see two page views for one navigation?
A page view is sent whenever the full URL changes, including the query string and the hash. A router that updates the URL twice — for example adding a query parameter right after navigating — sends two.
