<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 Replace the key and the domain with yours. If your
<script> element after <Links />: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:The switches in Settings, Tracking code add
app/root.tsx
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 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
POSTrequest tohttps://in.crispstats.com/eon the first load and on each navigation. The collector answers202. - In CrispStats, your site’s Tracking code settings tab shows Receiving events. with the time of the last event.
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.Send custom events
Call the global function from a component. For types and calls made before the script loads, addcrispstats.d.ts and the queue stub from TypeScript (run the stub from app/entry.client.tsx).
app/components/upgrade-button.tsx
?.) 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.
How route changes are counted
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.
FAQ
Do I need a useLocation effect to track routes?
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.
Why do the flags in my tag have empty values?
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.Can I proxy the script through my own domain?
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 does. See proxy by host.
Next steps
Custom events
Track signups, clicks and other actions with properties.
Visitor id at checkout
Pass the visitor id to your payment provider to attribute revenue.
Single-page apps
How client navigation and engagement are tracked.