Why single-page apps are different
A traditional site loads a new HTML page for every click. A single-page app (React, Next.js, Vue, Svelte and others) loads once and then swaps content in place, changing the address with the browser’s History API — history.pushState() and history.replaceState() — and reacting to the back button through the popstate event. No new page load means no new run of a tag that only runs on load.
Three ways to record route changes
- Listen to the History API. The tag wraps
pushStateandreplaceStateand listens forpopstate, and records a page view whenever the address actually changes. Works with any framework and needs nothing from your code. - Call the tag from your router. Your app tells the tag on each route change. Precise, but it has to be wired into every app.
- Use a tag manager’s history trigger. Google Tag Manager has a History Change trigger that fires on the same events.
How the Namorus tag does it
It uses the first method. Add the one line to your document head and it records each change of address as a page view, once — it ignores calls that don’t change the address, so a framework that calls replaceState repeatedly doesn’t inflate your numbers. Install the tag.
Common mistakes
- Double counting. If you install the tag directly and fire it from a GTM History Change trigger, each route change can be sent twice. Pick one.
- Loading the tag inside a component. If the script tag is rendered by a component that mounts and unmounts, it can load more than once. Put it in the document head, outside the app.
- Hash routes. Apps that route with
#/pathchange the address without the History API in some set-ups. If your app uses hash routing, test it with the tag checker.
What gets recorded on each route change
Each page view a tag sends should carry what a normal page load would: the full address, the page title, the referring page and the time. In a single-page app, two of those need care.
- The title. Many frameworks update
document.titlea moment after the address changes. A tag that reads the title instantly can record the previous page’s title against the new address. The Namorus tag waits until the browser has finished the current task before it reads the address and title, which catches most frameworks; if your titles still lag, set them before you change the route. - The referrer.
document.referrerdoesn’t change during a single-page session: it stays the page that sent the visitor to your app in the first place. That’s correct for working out where a visit came from, but don’t expect it to show the previous route.
How the page view is sent
A route change often happens just as someone clicks away. To make sure the page view still arrives, tags send it in a way the browser finishes even if the page is closing. The Namorus tag uses navigator.sendBeacon, and falls back to fetch with keepalive where a browser doesn’t support it. Neither blocks your app, and neither waits for an answer.
Testing it
- Open your app with the browser’s developer tools on the Network tab, filtered to the tag’s endpoint.
- Click through three or four routes, then use the back button twice.
- You should see one request per change of address, and none when you click a link to the page you’re already on.
- Check that a filter or tab that only changes the query string, such as
?tab=billing, records what you expect. Changing the query changes the address, so the Namorus tag counts it as a new page view.
If you see two requests per click, the tag is installed twice. If you see none after the first, the router may be using hash routes; test with the tag checker.
- 01Open developer toolsThe Network tab, filtered to the tag
- 02Click through routesThree or four, then back twice
- 03Count the requestsOne per change of address
- 04Check query stringsA filter or tab that only changes the query
Server-rendered frameworks
Frameworks such as Next.js and Nuxt render the first page on the server and then behave like a single-page app. Nothing changes for the tag: put it in the document head of the root layout, once, and it records the first load and every client-side route change after it.