# Single-page apps

> Venbit counts route changes in React, Next.js, Vue, SvelteKit and other single-page apps with no extra code. A change to only the query string or an anchor doesn't count.

Source: https://venbit.com/docs/developer-guide/single-page-apps
Part of the Venbit Intelligence docs (Developer Guide). Index of every page: https://venbit.com/docs/llms.txt
Last updated: 2026-11-30

Single-page apps need no extra code. The tracker watches `history.pushState`, `history.replaceState`, the `popstate` event and the `hashchange` event, and records a page view whenever the route changes. Put the one line in the shared `<head>` and React Router, the Next.js router, Vue Router, SvelteKit and similar routers are all covered.

```html
<script defer crossorigin="anonymous" src="https://vnbt.app/m.js?s=KEY" data-site="KEY"></script>
```

`KEY` is your site's key. You'll find it in the dashboard under **Settings**, then **Tracking**.

## What counts as a page view

A page is identified by its path. The hash counts too, but only when it's used as a route (it starts with `#/`).

| Change | New page view |
| --- | --- |
| Full page load | Yes |
| `/services` to `/services/roof-repair` through the router | Yes |
| Back or forward button to a different path | Yes |
| A page restored from the browser's back-forward cache | Yes |
| `/#/pricing` to `/#/contact` (hash routing) | Yes |
| `/services#faq` (an anchor on the same page) | No |
| `/search?q=roof` to `/search?q=gutter` (only the query string changes) | No |
| `replaceState` that leaves the path as it was | No |
| The same path viewed again within about two seconds | No |

**A page is its path, plus the hash when the hash is used as a route**

1. The address changes (Through the router, or back and forward)
2. A different path (New page view); A different hash route (New page view); Query or anchor only (No page view)

Two things follow from that.

The query string isn't part of the page, so `/pricing?plan=pro` and `/pricing` show up as the same page in reports. Campaign tags such as `utm_source` are still read from the landing address.

If your app routes on the query string alone (`?view=pricing`), you'll get one page view per full load. Report those screens yourself with a custom event, shown below.

## Page titles

The page view goes out as soon as the address changes. Most routers set `document.title` a moment later, so the tracker waits briefly and corrects the title if it changes. Set the title during or right after the route change and the page gets the right name in [Top pages](https://venbit.com/docs/md/user-guide/pages.md).

## Calls made before the tracker loads

Because the tag is deferred, your app's code can run before the tracker does. Put this stub ahead of your first `venbit()` call. Early calls then wait in a queue and play back in order once the tracker loads.

```html
<script>
  window.venbit = window.venbit || function () { (venbit.q = venbit.q || []).push(arguments); };
</script>
```

## When to call venbit() yourself

There's no call for sending a page view by hand, and you won't need one. A few cases do need a line of your own.

**A screen that changes without a path change.** Think of a wizard step, a tab driven by the query string, or a modal.

```js
venbit('event', 'quote_step_2');
```

**A "not found" screen your app draws while the server answers 200.** Pages your server answers with a 404 or 410 are reported on their own. A client-side "not found" route isn't, so tell Venbit:

```jsx
import { useEffect } from "react";

export default function NotFound() {
  useEffect(() => {
    window.venbit?.('404');
  }, []);
  return <h1>Page not found</h1>;
}
```

**A form your code sends with `fetch`.** Most get picked up automatically, but reporting the result yourself makes it exact. See [Form tracking](https://venbit.com/docs/md/developer-guide/form-tracking.md).

**A sign-up or a payment.** See [Sign-ups and purchases](https://venbit.com/docs/md/developer-guide/sign-ups-and-purchases.md).

The [JavaScript API](https://venbit.com/docs/md/developer-guide/javascript-api.md) lists every call.

To have a coding agent set this up in your app, paste this and fill in your key:

```prompt
Set up Venbit analytics in this single-page app. Put <script defer crossorigin="anonymous" src="https://vnbt.app/m.js?s=KEY" data-site="KEY"></script> once in the shared head, KEY = [your site key]. Route changes are counted automatically, so don't add page view code. In the client-side not-found route, call window.venbit?.('404') once on mount. Where a screen changes without a path change (wizard step, ?view= tab, modal), call venbit('event', 'step_name'). For TypeScript, declare both interface Window { venbit } and a global var venbit.
```

## Common questions

### Does Venbit track route changes in React and Next.js?

Yes, and you don't have to add any code. The tracker watches `history.pushState`, `history.replaceState`, `popstate` and `hashchange`, and records a page view whenever the path changes. React Router, the Next.js router, Vue Router and SvelteKit are covered.

### Why doesn't Venbit count a page view when only the query string changes?

Venbit treats a page as its path, plus the hash when the hash is used as a route. The query string isn't part of it, so `/pricing?plan=pro` and `/pricing` are the same page. If your app routes on the query string alone, report those screens with `venbit('event', name)`.

### How do I track a 404 page in a single-page app?

Pages your server answers with a 404 or 410 are reported automatically. A "not found" screen your app draws while the server answers 200 isn't, so call `venbit('404')` when that screen mounts.

### Can I send a page view to Venbit by hand?

No. There's no call for it, and single-page route changes are counted automatically anyway. For a screen that changes without a path change, use `venbit('event', name)`.
