# Remix and React Router analytics with CrispStats

Updated 2026-11-23

Remix apps, and React Router apps in framework mode, get analytics from one CrispStats script tag rendered inside the head in the Layout export of app/root.tsx. The layout wraps every route, and navigation goes through the History API, so every page is counted with no useLocation effect and no package.

## Steps

### 1. Copy the element with your key

In the dashboard, open your site's **Settings**, then **Tracking code**, and choose the **Remix / React Router** tab. It shows the element below with your public key (it starts with `cs_pk_`) and your domain filled in.

### 2. Add the script to the Layout export

Open `app/root.tsx` and add the `<script>` element after `<Links />`. The root route's [Layout export](https://reactrouter.com/api/framework-conventions/root.tsx) wraps the app, its error boundary and its hydrate fallback, so the tag is on every page, error pages included. If the file already has a `Layout`, add only the element. In Remix v2 the components come from `@remix-run/react`; the code is otherwise the same.

`app/root.tsx`

```tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";
// Remix v2: import the same components from "@remix-run/react".

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

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

### 3. Turn on more tracking (optional)

In JSX an attribute needs a value. Give each option an empty string, which switches it on; a bare attribute renders as `"true"`, which the script reads as a value. The switches in **Settings**, **Tracking code** add `data-outbound`, `data-downloads`, `data-vitals`, `data-errors` and `data-404`. Do not add `data-hash`: both frameworks route with real paths.

`app/root.tsx`

```tsx
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-outbound=""
  data-vitals=""
  data-errors=""
  src="https://in.crispstats.com/d.js"
/>
```

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

Call the global from a component. The optional call (`?.`) keeps the button working when an ad blocker stops the script. For types and calls made before the script loads, add `crispstats.d.ts` and the queue stub from the [TypeScript page](/docs/install/typescript), and run the stub from `app/entry.client.tsx`.

`app/components/upgrade-button.tsx`

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

### 5. Deploy and open the live site

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

## Check that it works

In your browser's developer tools, the **Network** tab shows a `POST` to `https://in.crispstats.com/e` on the first load and on each navigation, and the collector answers `202`. In the dashboard, the site's **Tracking code** 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: each event is logged in the console and nothing is sent. Remove both before you ship. See [localhost and testing](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for the first load and every `<Link>` or `navigate()` to another route, with the previous page as referrer. Fetchers, forms that stay on the page and revalidation do not change the URL, so they add no pageview.
- Sources and campaigns per route, with UTM tags kept from the landing URL while other query parameters are cut before anything is sent.
- A core script of about 2.2 KB gzipped, loaded with `defer`, outside your route bundles.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages.
- Revenue by source once you pass `crispstats.id()` to your checkout: see [Stripe revenue attribution](/integrations/stripe).

## Notes for Remix

- `navigate(…, { replace: true })` and redirects call `history.replaceState`: a new URL counts once, the same URL does not.
- Query strings are cut to UTM and a few other parameters before a URL is compared, so moving from `/products?sort=price` to `/products?sort=name` is not a new pageview. Keep more with `data-keep-params`.
- This guide covers React Router in framework mode. A client-only app with `createBrowserRouter` and an `index.html` follows the [React guide](/integrations/react) instead.
- To count visitors whose ad blocker stops the collector host, add a resource route or a rule on your host that forwards one path to the collector. See [proxy by host](/docs/tracking/proxy).
- There is no Remix package to install. The element in `root.tsx` is the whole install, whatever adapter you deploy with, because the script runs in the browser.

Full reference: [Remix in the docs](https://docs.crispstats.com/install/remix)

## Frequently asked questions

### 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. An effect that called pageview() after each change would find the URL already counted and send nothing.

### 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 the string true, which the script reads as a value.

### Does it work after upgrading from Remix to React Router v7?

Yes. The install is the same element in the Layout export of app/root.tsx. Only the import path of Links, Meta and the other components changes, from @remix-run/react to react-router.

### Do fetchers and form submissions count as pageviews?

No. Fetchers, forms that stay on the same page and revalidation leave the URL alone, so they add no pageview. To count a submission, call window.crispstats with an event name once it succeeds.

### What does it cost for a side project?

There is no free plan. 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. Paid plans never pause: extra events are billed and nothing is dropped.

## Related

- [React analytics](/integrations/react)
- [Revenue attribution: which traffic pays](/features/revenue-attribution)
- [Web analytics for SaaS products](/for/saas)
- [First touch vs last touch attribution](/blog/revenue-attribution-first-touch-vs-last-touch)
