Skip to content

Frameworks

Remix and React Router analytics with CrispStats

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.

Updated

Set up CrispStats on Remix

  1. 01

    Step 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. 02

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

    Replace the highlighted values with your own.

    app/root.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 /><scriptdeferdata-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. 03

    Step 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
    <scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-outbound=""data-vitals=""data-errors=""src="https://in.crispstats.com/d.js"/>
  4. 04

    Step 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, and run the stub from app/entry.client.tsx.

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

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

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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.
  • Notes and limits

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

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

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

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

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

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

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

Not answered here? Write to hello@crispstats.com

Go further

Related

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.