index.html. Route changes are picked up from the History API, which React Router and TanStack Router both use, so there is nothing to wire into your router. There is no npm package to install.
You need your site’s public key, which starts with cs_pk_. Settings, Tracking code, React / Vite shows the tag with your key and domain filled in.
1
Add the tag to index.html
In a Vite project, Replace the key and the domain with yours. Keep the CrispStats tag a classic script with
index.html sits at the project root. Add the tag inside <head>:index.html
defer: type="module" does not work for it, because the script reads its options from its own tag.2
Add types and the queue stub (TypeScript)
Save The optional call (
crispstats.d.ts in src/, and import the queue stub as the first line of src/main.tsx. Both files are on the TypeScript page. Then call the global from any component:src/UpgradeButton.tsx
?.) keeps the button working when an ad blocker stops the script. You can also track clicks with data-cs-event attributes and no JavaScript: see declarative events.3
Deploy and check
Visits to the Vite dev server on
localhost are not counted. Deploy, then open your production site in a normal browser window. Your visit appears in Realtime within a few seconds, and Settings, Tracking code shows Receiving events. To see events locally, use debug mode.Routers
The same URL twice in a row counts as one pageview, so a repeated
replaceState or a navigation to the current page does not inflate your numbers. See single-page apps.
Check that it works
- Open your deployed app with the browser’s developer tools on the Network tab. You should see
d.jsload fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/eon the first load. The collector answers202. - Click through a few routes. Each new URL sends another
POST. If only the first load sends one, your router uses hash URLs (adddata-hash) or saveshistory.pushStatein a way the script cannot see (adddata-manual, see the table above). - In CrispStats, Settings, Tracking code shows Receiving events., and each route appears in Realtime within seconds.
- Open
https://in.crispstats.com/check?k=cs_pk_xxxxxxxxwith your key."site": "known"means the key is right.
FAQ
Is there a React component or npm package?
Is there a React component or npm package?
No. The tag in
index.html is the whole install, and the global window.crispstats is what you call from components.Are pageviews counted twice in React strict mode?
Are pageviews counted twice in React strict mode?
No. The script is loaded once by the tag, outside React, so strict mode’s double rendering does not touch it. The same URL twice in a row is also one pageview.
How do I attribute payments to visits?
How do I attribute payments to visits?
Call
await window.crispstats?.id() in the click handler that starts checkout and pass the id to your payment provider. See visitor id at checkout.Next steps
Custom events
Track signups, clicks and other actions with properties.
TypeScript
Types for the global and the queue stub.
Proxy
Serve the script from your own domain.