# React analytics with CrispStats

Updated 2026-11-23

A React app built with Vite needs one CrispStats script tag in the head of index.html, then a deploy. Route changes from React Router and TanStack Router are counted automatically, because both use the History API, so there is no package to install and no router hook to write.

## Steps

### 1. Copy the tag with your key

In the dashboard, open your site's **Settings**, then **Tracking code**, and choose the **React / Vite** tab. It shows the tag below with your public key (it starts with `cs_pk_`) and your domain filled in. The key is safe to publish.

### 2. Paste it into the head of index.html

In a Vite project, `index.html` sits at the project root, because [Vite treats it as the entry point](https://vite.dev/guide/) of the app. Add the tag inside `<head>`. Keep it a classic script with `defer`: the script reads its options from its own `data-` attributes, so `type="module"` does not work for it.

`index.html`

```html
<!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_your_site_key"
      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>
```

### 3. Add data-hash if you use a hash router

With `BrowserRouter`, `createBrowserRouter` or TanStack Router, skip this step. A [HashRouter](https://reactrouter.com/api/declarative-routers/HashRouter) keeps the route after `#`, which the script cuts from every URL unless the tag has `data-hash`. Without it, every route counts as the same page.

`index.html`

```html
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-hash
  src="https://in.crispstats.com/d.js"
></script>
```

### 4. Send a custom event (optional)

Call the global from any component. The optional call (`?.`) keeps the button working when an ad blocker stops the script. In TypeScript, save `crispstats.d.ts` in `src/` and import the queue stub as the first line of `src/main.tsx`; both are on the [TypeScript page](/docs/install/typescript). For clicks you can also use `data-cs-event` attributes with no JavaScript.

`src/UpgradeButton.tsx`

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

### 5. Deploy and open the live site

Visits to the Vite dev server on `localhost` are not counted. Deploy as you normally do, then open your production URL in a normal browser window without an ad blocker.

## Check that it works

Open your deployed app with the browser's developer tools on the **Network** tab. You should see `d.js` load and a `POST` to `https://in.crispstats.com/e` that answers `202`, then one more `POST` for each route you click to. In the dashboard, **Settings**, **Tracking code** shows **Receiving events.** and the visit appears on **Realtime** within seconds. To check the key alone, open `https://in.crispstats.com/check?k=cs_pk_your_site_key` with your own key: `"site": "known"` means it is right. To watch events locally without sending them, use [debug mode](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for every route change made through `<Link>`, `navigate()` or the back button, read from the History API and, in browsers that have it, the Navigation API.
- Exit pages per route, so you see which screen of a client-rendered app people leave from, next to the source that brought them.
- A core script of about 2.2 KB gzipped, loaded with `defer` and outside your bundle, so it adds nothing to your build.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute on the same tag.
- Revenue by source once you pass `crispstats.id()` to your checkout: see [Stripe revenue attribution](/integrations/stripe).

## Notes for React

- There is no React component or npm package to install: the tag in `index.html` is the whole install, and `window.crispstats` is what components call.
- React strict mode does not double your pageviews. The script is loaded once by the tag, outside React, and the same URL twice in a row is one pageview.
- Query strings are cut before a URL is compared or sent, keeping only UTM and a few other parameters, so `/products?sort=price` and `/products?sort=name` are one page. Keep more with `data-keep-params`.
- A router the script cannot see needs `data-manual` and a `pageview()` call after each route change. See [single-page apps](/docs/tracking/single-page-apps).
- This guide is for client-rendered React served from `index.html`. A Next.js app or a React Router framework app has its own guide: [Next.js](/integrations/nextjs) or [Remix and React Router](/integrations/remix).
- An [ad blocker](/glossary/ad-blocker) can stop the collector host. Serving the script from your own domain through your host fixes most of that: see [proxy by host](/docs/tracking/proxy).

Full reference: [React in the docs](https://docs.crispstats.com/install/react-vite)

## Frequently asked questions

### Is there a React analytics package for this?

No. CrispStats is one script tag in index.html, with nothing to install from npm. Components call the global window.crispstats for custom events, and route changes are counted without code.

### Do I need a useEffect on location changes to track routes?

No. The script watches the History API, which React Router and TanStack Router use, so each new URL is counted as a pageview. Only hash routers need the data-hash attribute.

### Why do I see no visits from npm run dev?

The script ignores localhost and automated browsers on purpose, so development and test runs stay out of your numbers. Deploy and test on the live URL, or turn on debug mode to see each event in the console without sending it.

### How do I attribute payments to visits in a React app?

Call await window.crispstats?.id() in the click handler that starts checkout, and pass the id to your payment provider in the checkout metadata. Each payment is then credited to the first known source of that visitor.

### Does strict mode use up extra events?

No. The script loads once from the tag, outside React, so effects that run twice in development send nothing extra, and each route change is one event. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Web analytics for developers](/for/developers)
- [Funnels and goals](/features/funnels)
- [Custom events, defined](/glossary/custom-event)
- [Next.js analytics](/integrations/nextjs)
- [Gatsby analytics](/integrations/gatsby)
