> ## 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 Remix or React Router

> Install CrispStats in a Remix or React Router framework app by adding the script tag to the Layout export in app/root.tsx. Route changes are tracked for you.

To add CrispStats to a Remix app or a React Router app in framework mode, render the script tag inside `<head>` in the `Layout` export of `app/root.tsx`. The layout wraps every route, so every page is tracked. Navigation between routes goes through the History API, which the script watches, so you need no `useLocation` effect.

You need your site's public key, which starts with `cs_pk_`. Find it in your site's settings under **Tracking code**. The key is safe to publish.

**1. Add the tag to the Layout export**

Add the `<script>` element after `<Links />`:

```tsx React Router theme={"theme":{"light":"github-light","dark":"github-dark"}}
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
        <script
          defer
          data-site="cs_pk_xxxxxxxx"
          data-domains="example.com"
          src="https://in.crispstats.com/d.js"
        />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}
```

```tsx Remix theme={"theme":{"light":"github-light","dark":"github-dark"}}
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "@remix-run/react";

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
        <script
          defer
          data-site="cs_pk_xxxxxxxx"
          data-domains="example.com"
          src="https://in.crispstats.com/d.js"
        />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}
```

Replace the key and the domain with yours. If your `root.tsx` already has a `Layout`, add only the `<script>` element. **Settings**, **Tracking code**, **Remix / React Router** shows it with your key and domain filled in.

**2. Turn on more tracking (optional)**

In JSX an attribute needs a value. Give each option an empty string, which switches it on:

```tsx app/root.tsx theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-outbound=""
  data-vitals=""
  data-errors=""
  src="https://in.crispstats.com/d.js"
/>
```

The switches in **Settings**, **Tracking code** add `data-outbound`, `data-downloads`, `data-vitals`, `data-errors` and `data-404` to the generated snippet. Do not add `data-hash`: Remix and React Router route with real paths. [Script attributes](https://docs.crispstats.com/tracking/script-attributes.md) lists every attribute the script reads, including `data-exclude` and `data-domains`, and what each one does.

**3. Deploy and confirm**

Pages on `localhost`, `127.0.0.1` and `file://` are ignored, and so are automated browsers, so a visit to the dev server is not counted. Deploy, then open your production site in a browser without an ad blocker.

* In your browser's developer tools, the **Network** tab shows a `POST` request to `https://in.crispstats.com/e` on the first load and on each navigation. The collector answers `202`.
* In CrispStats, your site's **Tracking code** settings tab shows **Receiving events.** with the time of the last event.

To see events locally, add `data-debug=""` and `localhost` in `data-domains` while you develop: every event is logged in the console and nothing is sent. Remove it before you ship. See [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md).

## Send custom events

Call the global function from a component. For types and calls made before the script loads, add `crispstats.d.ts` and the queue stub from [TypeScript](https://docs.crispstats.com/install/typescript.md) (run the stub from `app/entry.client.tsx`).

```tsx app/components/upgrade-button.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 from loading. For clicks you can also use `data-cs-event` attributes and no JavaScript: see [declarative events](https://docs.crispstats.com/track/declarative-events.md).

## How route changes are counted

| Navigation | What the script sees |
| - | - |
| The first load | One pageview. |
| A `<Link>` or `navigate()` to another route | The router calls `history.pushState`; one pageview, with the previous page as referrer. |
| `navigate(…, { replace: true })` or a redirect to a new URL | `history.replaceState`; a new URL counts once, the same URL does not. |
| A fetcher, a form that stays on the page, or data revalidation | The URL does not change, so no pageview. |
| Back and forward buttons | `popstate`; the URL you land on counts. |

Search parameters are part of the URL, but the script cuts the query string before it compares and sends URLs, keeping only UTM parameters and a few others. So moving from `/products?sort=price` to `/products?sort=name` is not a new pageview: both are `/products`. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md).

## FAQ

> [accordion] Do I need a useLocation effect to track routes?
>
> No. The script watches the History API, so every route change is seen without code in your app.

> [accordion] Why do the flags in my tag have empty values?
>
> In JSX an attribute needs a value. An empty string switches the option on. A bare attribute renders as `"true"`, which the script reads as a value.

> [accordion] Can I proxy the script through my own domain?
>
> Yes. Add a resource route or a rule on your host that forwards one path to the collector, the same way the [Next.js route handler](https://docs.crispstats.com/tracking/proxy/nextjs.md) does. See [proxy by host](https://docs.crispstats.com/tracking/proxy.md).

## Next steps

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

> [card] [Visitor id at checkout](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md)
>
> Pass the visitor id to your payment provider to attribute revenue.

> [card] [Single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md)
>
> How client navigation and engagement are tracked.


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