> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Track single-page apps and hash routing

> How CrispStats sees route changes in single-page apps: the History API, the Navigation API, routers that saved pushState early, hash routing, crispstats.pageview() and data-manual.

CrispStats tracks single-page apps without extra code. The script watches the History API and, where the browser has it, the Navigation API, so route changes in Next.js, React Router, TanStack Router, Vue Router, SvelteKit and other routers count as pageviews. Routers that keep the route after `#` need `data-hash`. For a router the script cannot see, call `crispstats.pageview()` yourself.

## How page changes are seen

The script counts a pageview when the URL has changed since the last one it counted:

| Trigger | When it fires |
| - | - |
| Page load | Once, when the script starts. |
| `history.pushState` and `history.replaceState` | After each call. The script wraps both methods when it starts. |
| The Navigation API's `currententrychange` | After every URL change in the page, in browsers that have the Navigation API, such as Chrome and Edge. |
| `popstate` | When the visitor uses the back or forward button. |
| `pageshow` from the back/forward cache | A page restored from the bfcache counts as a new view, even at the same URL. |
| `hashchange` | Only with `data-hash`. Without it the fragment is not part of the URL, so a hash change sends nothing. |
| The page leaves prerendering | A prerendered page is counted when it is shown, not while it renders in the background. |

When two of these report the same change, such as the `pushState` wrapper and the Navigation API, the second sees the same URL and sends nothing.

### Same URL, one pageview

The same URL twice in a row is one pageview. A router that calls `replaceState` to update state without changing the address, or calls `pushState` twice for the same route, does not inflate your numbers. The script compares the URL as it would send it: the fragment is left out unless `data-hash` is on, and the query string is [cut to the kept parameters](https://docs.crispstats.com/tracking/script-attributes.md#what-the-script-sends), so `?tab=billing` and `?tab=team` are the same page unless you keep `tab` with `data-keep-params`.

### Referrer on navigation

The first pageview carries the external referrer: the site the visitor came from. On every later navigation in the same page load, the referrer is the previous tracked page on your site. An [excluded page](https://docs.crispstats.com/tracking/exclude-traffic.md) is never used as a referrer.

### Engagement before each navigation

Time on page and scroll depth are reported per pageview. Before each single-page navigation, and whenever the page is hidden or left, the script sends a `ping` with the seconds the page has been visible so far and the furthest scroll depth so far as a percentage. A ping is sent only when the visible time has grown by at least one whole second, so a page left within its first second sends none. Time in a background tab or in the back/forward cache is not counted.

## Routers that saved pushState early

The script wraps `history.pushState` when it starts. A router that saved its own copy of `pushState` before that calls the original, which the wrapper never sees. This happens when the script loads late, for example with `next/script` and `strategy="afterInteractive"`, and with routers such as vinext that keep a reference at startup.

The Navigation API covers this: it reports every URL change, however the router made it. Browsers that have it, such as Chrome and Edge, see these route changes. In a browser without it, such a router's navigations are missed, and only the first pageview of each full page load is counted. To see whether a browser has it, run `"navigation" in window` in its console.

To check your app, open it in a browser where `"navigation" in window` is `false`, with [debug mode](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode) on, click between routes and watch the console. One `crispstats: pageview not sent (debug mode)` line per route change means the script sees your router. If route changes log nothing, use [manual pageviews](#manual-pageviews).

## Manual pageviews

Add `data-manual` to the tag and the script counts no pageview on its own, not even the first one. Your code calls `crispstats.pageview()` after each page change instead. Use it only when the script cannot see your router, or when you want to decide yourself what a page is.

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-manual
  src="https://in.crispstats.com/d.js"
></script>
```

Then call `pageview()` once per page, including the first. In Next.js, a client component in the root layout does it:

```tsx app/crispstats-pageviews.tsx theme={"theme":{"light":"github-light","dark":"github-dark"}}
"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";

// Counts a pageview after each route change. Only with data-manual on the script tag.
export function CrispStatsPageviews() {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  useEffect(() => {
    window.crispstats?.pageview();
  }, [pathname, searchParams]);
  return null;
}
```

Render `<CrispStatsPageviews />` inside a `<Suspense>` boundary in `app/layout.tsx`, because `useSearchParams` needs one. Add the [queue stub](https://docs.crispstats.com/install/typescript.md) and the global types so the call is typed and is not lost before the script loads.

How `pageview()` behaves:

| Call | Under `data-manual` | Without `data-manual` |
| - | - | - |
| `crispstats.pageview()` | Counts the current URL, every time it is called. | Counts the current URL only if it differs from the last one counted, so it never doubles a view the script already counted. |
| `crispstats.pageview("/checkout/step-2")` | Counts that URL, every time. | Counts that URL, every time. |

A URL you pass is resolved against the current page and cut like any other: fragment and unkept query parameters are removed. Calls made before the script loads wait in the [queue stub](https://docs.crispstats.com/tracking/script-attributes.md#call-the-global-before-the-script-loads) and run once it starts.

> [!WARNING]
>
> Under `data-manual`, events sent before the first `pageview()` call are dropped, because they belong to no page. Call `pageview()` before your first custom event. In debug mode, such events log `not sent (no pageview yet)`.

Without `data-manual`, `pageview()` is still there as a safety net: a router hook that calls it after each change costs nothing when the script already saw the change.

## Hash routers

If your app routes with the fragment, such as `https://example.com/#/settings`, add `data-hash` to the tag:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-hash
  src="https://in.crispstats.com/d.js"
></script>
```

With `data-hash`, the script sends the fragment when it is a route, one that starts with `#/` or `#!/`, and a change of route counts as navigation. A query string inside the fragment is cut like any other. Any other fragment, such as `#pricing` or `#access_token=…`, is removed in the browser, so `/#/settings` shows as `/#/settings` in your reports and an in-page anchor never counts as a page.

> [!NOTE]
>
> Leave `data-hash` off on a site with real paths, such as Next.js, Nuxt, SvelteKit, Astro or Remix. It does no harm there, since only `#/` and `#!/` routes are read, but it has nothing to read either.

Without `data-hash`, the fragment is cut from every URL before it is sent.

## Check it works

**1. Turn on debug mode locally**

Use the tag with `data-debug` from [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode). It logs each event in the console and sends nothing.

**2. Navigate between routes**

Each route change logs one `crispstats: pageview not sent (debug mode)` line, with the payload. Its `u` is the new URL. If you stayed on the previous route for a second or more, a `ping` line for that page comes just before it.

**3. Watch the live feed after you deploy**

With the normal tag on your live site, open **Realtime** in the dashboard. Each navigation appears in the **Live feed** within a few seconds.

## Next steps

> [card] [Script attributes](https://docs.crispstats.com/tracking/script-attributes.md)
>
> Every option the script reads.

> [card] [Next.js](https://docs.crispstats.com/install/nextjs.md)
>
> The script, a proxy and 404s in a Next.js app.

> [card] [Localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md)
>
> Debug mode, local builds and tests in CI.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.