> ## 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.

# Add CrispStats to a React or Vite app

> Install CrispStats in a React or Vite app with one tag in index.html. React Router and TanStack Router need no wiring; hash routers need data-hash.

To add CrispStats to a React or Vite app, paste the script tag into `index.html`. Route changes are picked up from the History API, which React Router and TanStack Router both use, so there is nothing to wire into your router. There is no npm package to install.

You need your site's public key, which starts with `cs_pk_`. **Settings**, **Tracking code**, **React / Vite** shows the tag with your key and domain filled in.

**1. Add the tag to index.html**

In a Vite project, `index.html` sits at the project root. Add the tag inside `<head>`:

```html index.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My app</title>
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
```

Replace the key and the domain with yours. Keep the CrispStats tag a classic script with `defer`: `type="module"` does not work for it, because the script reads its options from its own tag.

**2. Add types and the queue stub (TypeScript)**

Save `crispstats.d.ts` in `src/`, and import the queue stub as the first line of `src/main.tsx`. Both files are on the [TypeScript](https://docs.crispstats.com/install/typescript.md) page. Then call the global from any component:

```tsx src/UpgradeButton.tsx theme={"theme":{"light":"github-light","dark":"github-dark"}}
export function UpgradeButton() {
  return (
    <button onClick={() => window.crispstats?.("upgrade clicked", { from: "settings" })}>
      Upgrade
    </button>
  );
}
```

The optional call (`?.`) keeps the button working when an ad blocker stops the script. You can also track clicks with `data-cs-event` attributes and no JavaScript: see [declarative events](https://docs.crispstats.com/track/declarative-events.md).

**3. Deploy and check**

Visits to the Vite dev server on `localhost` are not counted. Deploy, then open your production site in a normal browser window. Your visit appears in **Realtime** within a few seconds, and **Settings**, **Tracking code** shows **Receiving events.** To see events locally, use [debug mode](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

## Routers

| Router | What to do |
| - | - |
| React Router (`BrowserRouter`, `createBrowserRouter`) | Nothing. Navigation goes through the History API. |
| TanStack Router | Nothing. Navigation goes through the History API. |
| Hash routing (`HashRouter`, `createHashRouter`) | Add `data-hash` to the tag. Without it the part after `#` is never sent, so every route is counted as the same page. |
| A router the script does not see | Add `data-manual` and call `window.crispstats?.pageview()` after each route change. See [manual pageviews](https://docs.crispstats.com/tracking/single-page-apps.md#manual-pageviews). |

The same URL twice in a row counts as one pageview, so a repeated `replaceState` or a navigation to the current page does not inflate your numbers. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md).

## Check that it works

* Open your deployed app with the browser's developer tools on the **Network** tab. You should see `d.js` load from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` on the first load. The collector answers `202`.
* Click through a few routes. Each new URL sends another `POST`. If only the first load sends one, your router uses hash URLs (add `data-hash`) or saves `history.pushState` in a way the script cannot see (add `data-manual`, see the table above).
* In CrispStats, **Settings**, **Tracking code** shows **Receiving events.**, and each route appears in **Realtime** within seconds.
* Open `https://in.crispstats.com/check?k=cs_pk_xxxxxxxx` with your key. `"site": "known"` means the key is right.

## FAQ

> [accordion] Is there a React component or npm package?
>
> No. The tag in `index.html` is the whole install, and the global `window.crispstats` is what you call from components.

> [accordion] Are pageviews counted twice in React strict mode?
>
> No. The script is loaded once by the tag, outside React, so strict mode's double rendering does not touch it. The same URL twice in a row is also one pageview.

> [accordion] How do I attribute payments to visits?
>
> Call `await window.crispstats?.id()` in the click handler that starts checkout and pass the id to your payment provider. See [visitor id at checkout](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md).

## Next steps

> [card] [Custom events](https://docs.crispstats.com/track/custom-events.md)
>
> Track signups, clicks and other actions with properties.

> [card] [TypeScript](https://docs.crispstats.com/install/typescript.md)
>
> Types for the global and the queue stub.

> [card] [Proxy](https://docs.crispstats.com/tracking/proxy.md)
>
> Serve the script from your own domain.


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