How to track a single-page app without double counting
In a single-page app the browser loads one HTML page and JavaScript swaps the views, so a tracker that only counted page loads would see one page per visit. Linqry's tracker watches the History API instead, so route changes count as page views with no extra code, as long as you avoid a few common mistakes.
Updated · 7 min read
How the tracker spots a navigation
Browsers don't fire any event when an app changes the URL with history.pushState or history.replaceState: popstate fires for history navigation, such as the back and forward buttons, but not for those two calls. So the Linqry tracker wraps both functions and listens for popstate. After each one, it compares the new URL with the last one it counted, looking at the path and the query string, plus the hash when data-hash-routing is set. If they differ, it sends a pageleave event with the time spent on the previous route, then a pageview for the new one.
Two details follow from that design. The first page view of a page load carries the referrer, if the visitor came from another site, but route changes never do, so moving around your own app can't turn into a referral. And because the comparison is on the URL, saving state with replaceState without changing the address doesn't count as anything.
What counts as a page view
| What the app does | Counted as a page view? |
|---|---|
pushState to a new path, such as /settings | Yes |
pushState or replaceState to the URL you're already on | No |
replaceState that only changes the history state object | No |
replaceState that changes the query string, such as ?tab=billing | Yes, every change |
| Back or forward to a URL with a different path or query string | Yes |
A change to the hash only, such as #faq | No, unless data-hash-routing is set |
A change to the hash only, with data-hash-routing set | Yes, including jumps to in-page anchors |
| A full page load | Yes, with the referrer when it came from another site |
Script attributes that matter in an SPA
The tracker reads its settings from attributes on its own <script> tag:
| Attribute | What it does in an SPA |
|---|---|
data-website-id | Required. Your site ID. data-site-id and data-site work too. |
data-domain | Optional. If set, it must be the site's domain or one of its additional hosts, or every event is rejected. Events from pages on other hosts are rejected either way. |
data-hash-routing | Optional, no value. Counts hash changes as navigations, for hash routers. |
data-exclude | Optional. Comma-separated paths to skip, where * matches anything, such as /admin/*,/preview. Page views, custom events and outbound clicks on matching paths aren't sent. |
data-endpoint | Optional. Where events are sent. Defaults to /api/collect on the script's origin, and most apps never set it. |
Two data-exclude details catch people out. Patterns match the whole path and are case-sensitive, so /admin/* matches /admin/users but not /admin itself or /Admin/users; list /admin,/admin/* to cover both. And the query string is ignored, so /preview also matches /preview?id=3.
Hash routers: count them, but know the limits
Hash routers keep the route after a #, as in https://example.com/#/settings. By default the tracker treats a hash change as a jump within the page, so only full page loads are counted. Add data-hash-routing to the script tag and every route change counts:
<script>window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}</script>
<script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" data-hash-routing src="https://linqry.com/t.js"></script>Know the trade-offs before you rely on it:
- Page reports group by path, and the hash isn't part of the path. Linqry files each page view under the URL's path, so every route of
https://example.com/#/…is listed as/in Top pages, entry and exit pages, user flows and visitor timelines. Page view counts are right, but per-route tables aren't possible. - Path goals and funnel steps can't target a hash route. They match the path exactly, which is the same
/for every route. Send a custom event from the screens you care about, such aswindow.linqry('checkout-viewed'), and build goals and funnels from those events. - In-page anchors count too. With the attribute set, a table-of-contents link that changes the hash to
#pricing-tableis a page view. Leave it off on sites that only use the hash for anchors, such as documentation.
If you can, switch the router to history mode. Most routers offer both, and history-mode URLs give you per-route reports with no extra events:
| Router | History routing | Hash routing |
|---|---|---|
| React Router | createBrowserRouter, which updates the path with pushState and replaceState | createHashRouter, which keeps the path in the URL hash |
| Vue Router | createWebHistory(), with URLs like /user/id | createWebHashHistory(), with a # before the route |
| Angular Router | PathLocationStrategy, the default | HashLocationStrategy |
History mode needs your server to answer every app route with the app's HTML, otherwise a deep link returns a 404. Hash mode doesn't, because the hash is never sent to the server, which is why some apps start there. The install guides for React, Vue and Angular show where the snippet goes in each.
Load the tracker once
Put the snippet in the HTML shell every route shares, such as index.html or the root layout, never in a component that mounts per route. If your app adds the script at runtime, guard it so a second call does nothing:
// Safe to call more than once: later calls find the tag and stop.
export function loadLinqry() {
if (typeof window === 'undefined') return; // server rendering
window.linqry = window.linqry || function () {
(window.linqry.q = window.linqry.q || []).push(arguments);
};
if (document.querySelector('script[data-website-id]')) return;
const s = document.createElement('script');
s.src = 'https://linqry.com/t.js';
s.setAttribute('data-website-id', 'YOUR_SITE_ID');
s.setAttribute('data-domain', 'yourdomain.com');
document.head.appendChild(s);
}The queue stub keeps any window.linqry() calls made before the script arrives, and the tracker sends them once it loads. In TypeScript, declare the function once so calls type-check everywhere:
// linqry.d.ts (anywhere your tsconfig includes)
export {};
declare global {
interface Window {
linqry?: (
event: string,
props?: Record<string, string | number | boolean>,
revenueCents?: number,
) => void;
}
}Mistakes that double-count page views
Each of these was reproduced against the tracker in a real browser. All of them inflate page views, and most also lower the bounce rate, because a session with two page views is never a bounce.
- Loading the tracker twice. Two script tags, a hard-coded tag plus a tag manager, or a component that injects the script on every route: each copy wraps the History API and counts every navigation, so every page view arrives twice. In the console,
document.querySelectorAll('script[src*="linqry.com/t.js"]').lengthshould print1. - Sending page views yourself. A router hook or effect that calls
window.linqry('pageview')on each route change adds a second page view to the one the tracker already sent. Delete it; the tracker needs no help. - Redirecting in the browser. A router that sends
/to/en/, or/dashboardto/login, withreplaceStateafter the page loads can produce two page views: one for the URL that loaded and one for where it ended up. Redirect on the server where you can. An HTTP redirect happens before the page loads, so the tracker only ever sees the final URL. - Writing every keystroke to the URL. Search boxes, filters and tabs that mirror their state into the query string create a page view per change. Top pages still shows a single path, but the page view count climbs. Update the URL when the input settles, as below, or on submit.
- Turning on hash routing for anchors.
data-hash-routingon a site whose hashes are in-page anchors makes every table-of-contents click a page view.
// Mirror the search box into ?q= once typing pauses, not on every keystroke.
let timer;
searchInput.addEventListener('input', () => {
clearTimeout(timer);
timer = setTimeout(() => {
const url = new URL(location.href);
url.searchParams.set('q', searchInput.value);
history.replaceState(history.state, '', url);
}, 500);
});Routes that open a modal or drawer, such as /photos/42 over a gallery, are counted as page views as well. That's usually what you want, but remember it when a page's numbers look high.
When page views go missing
- Only the first page is counted: the app routes with the hash and the script tag has no
data-hash-routing. - A step has no URL of its own: multi-step forms and wizards that swap content without changing the URL produce no page views. Send an event per step with its own name, such as
onboarding-step-2, because goals and funnels match event names, not properties. The signup tracking guide shows a full event plan. - A whole section is missing: a
data-excludepattern such as/app/*matches more than you meant. - You're testing on localhost: linqry.com doesn't record events from
localhost. Test on a deployed host instead, as the troubleshooting docs explain. - Some visitors are missing: ad blockers and privacy tools can block analytics scripts in any app, and nothing in your routing changes that.
Custom events in single-page apps
Custom events work as on any site: call window.linqry(name, properties, revenueCents) from your handlers, or put data-linqry-event on a clickable element. An event is recorded with the URL that's current when it's sent, so one fired straight after a route change belongs to the new route, and it's dropped if that path matches data-exclude. The custom event guide covers naming and properties.
Check your setup in five minutes
- Open the deployed site in a normal window
Use a browser where you haven't set the
linqry_ignoreflag, ideally without an ad blocker. Afterwards, exclude your own visits so testing doesn't inflate your numbers. - Click through three routes, then press Back once
That's four navigations, so you should see four page views after the first one.
- Watch Realtime
The live event feed lists each page view within seconds, newest first. One per navigation is right; two per navigation means a second copy of the tracker or a manual page view call.
- Read your timeline
On the Users page, open your visit. The timeline lists every path you viewed, newest first, along with any custom events, so a doubled entry stands out.
- Count the script tags
Run the console check from the list above. It should print 1.
Frequently asked questions
Do I need to call window.linqry on every route change?
No. The tracker detects pushState, replaceState and popstate on its own. Calling window.linqry('pageview') from a router hook adds a second page view for every route.
Does Linqry work with Next.js, React, Vue, Svelte and Angular?
Yes. Any router that changes the URL with the History API is tracked automatically, and hash routers need the data-hash-routing attribute. Each framework's install guide shows where the snippet goes.
Do query strings show up in Top pages?
No. Page reports group by path, so /search?q=a and /search?q=b both appear as /search, even though each change of query string inside the app counts as a separate page view.
Why does my app show more page views than I expect?
Look for a second copy of the tracker, a manual pageview call, redirects done in the browser, or state written to the query string on every change. Each one adds page views without a real navigation.
Sources
- MDN Web Docs: Window popstate event (pushState and replaceState don't fire it) (checked Nov 14, 2026)
- React Router: createBrowserRouter (checked Nov 14, 2026)
- React Router: createHashRouter (checked Nov 14, 2026)
- Vue Router: Different history modes (hash mode, HTML5 mode and server configuration) (checked Nov 14, 2026)
- Angular: Common routing tasks (location strategies) (checked Nov 14, 2026)
Related
Paste the snippet into index.html in a Vite or other React SPA. React Router navigations are tracked as page views.
Add the tracker to app/layout.tsx with next/script. Client-side route changes are counted as page views automatically.
Custom event tracking records the actions that matter on your site, like a button click, a signup or a purchase, in addition to page views. In Linqry you send one with a single HTML attribute or one line of JavaScript, and it becomes something you can count, turn into a goal and use as a funnel step.
Why visits or events don't show up in Linqry, and how to fix each cause, from the snippet to server-side API errors.
Add the snippet to index.html in a Vite-based Vue app. Vue Router navigations count as page views, including hash mode.
See your own funnels and revenue by channel
No card required · 30-day trial · No cookies