Frameworks
React analytics with CrispStats
A React app built with Vite needs one CrispStats script tag in the head of index.html, then a deploy. Route changes from React Router and TanStack Router are counted automatically, because both use the History API, so there is no package to install and no router hook to write.
Updated
Set up CrispStats on React
- 01
Step 1: Copy the tag with your key
In the dashboard, open your site’s Settings, then Tracking code, and choose the React / Vite tab. It shows the tag below with your public key (it starts with
cs_pk_) and your domain filled in. The key is safe to publish. - 02
Step 2: Paste it into the head of index.html
In a Vite project,
index.htmlsits at the project root, because Vite treats it as the entry point of the app. Add the tag inside<head>. Keep it a classic script withdefer: the script reads its options from its owndata-attributes, sotype="module"does not work for it.Replace the highlighted values with your own.
index.html<!doctype html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width= device-width, initial-scale=1.0" /><title>My app</title><scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"src= "https:/ / in. crispstats. com/ d. js"></script></head><body><div id="root"></div><script type="module" src= "/ src/ main. tsx"></ script></body></html> - 03
Step 3: Add data-hash if you use a hash router
With
BrowserRouter,createBrowserRouteror TanStack Router, skip this step. A HashRouter keeps the route after#, which the script cuts from every URL unless the tag hasdata-hash. Without it, every route counts as the same page.index.html<scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-hashsrc="https:/ / in. crispstats. com/ d. js"></script> - 04
Step 4: Send a custom event (optional)
Call the global from any component. The optional call (
?.) keeps the button working when an ad blocker stops the script. In TypeScript, savecrispstats.d.tsinsrc/and import the queue stub as the first line ofsrc/main.tsx; both are on the TypeScript page. For clicks you can also usedata-cs-eventattributes with no JavaScript.src/UpgradeButton.tsxexport function UpgradeButton() {return (<button onClick={() => window.crispstats? . ( "upgrade clicked", { from: "settings" })}>Upgrade</button>);} - 05
Step 5: Deploy and open the live site
Visits to the Vite dev server on
localhostare not counted. Deploy as you normally do, then open your production URL in a normal browser window without an ad blocker.
Check that it works
Open your deployed app with the browser’s developer tools on the Network tab. You should see d.js load and a POST to https://in.crispstats.com/e that answers 202, then one more POST for each route you click to. In the dashboard, Settings, Tracking code shows Receiving events. and the visit appears on Realtime within seconds. To check the key alone, open https://in.crispstats.com/check?k=cs_pk_your_site_key with your own key: "site": "known" means it is right. To watch events locally without sending them, use debug mode.
CrispStats on React
What you get
- A pageview for every route change made through
<Link>,navigate()or the back button, read from the History API and, in browsers that have it, the Navigation API. - Exit pages per route, so you see which screen of a client-rendered app people leave from, next to the source that brought them.
- A core script of about 2.2 KB gzipped, loaded with
deferand outside your bundle, so it adds nothing to your build. - Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute on the same tag.
- Revenue by source once you pass
crispstats.id()to your checkout: see Stripe revenue attribution.
- A pageview for every route change made through
Notes and limits
- There is no React component or npm package to install: the tag in
index.htmlis the whole install, andwindow.crispstatsis what components call. - React strict mode does not double your pageviews. The script is loaded once by the tag, outside React, and the same URL twice in a row is one pageview.
- Query strings are cut before a URL is compared or sent, keeping only UTM and a few other parameters, so
/products?sort=priceand/products?sort=nameare one page. Keep more withdata-keep-params. - A router the script cannot see needs
data-manualand apageview()call after each route change. See single-page apps. - This guide is for client-rendered React served from
index.html. A Next.js app or a React Router framework app has its own guide: Next.js or Remix and React Router. - An ad blocker can stop the collector host. Serving the script from your own domain through your host fixes most of that: see proxy by host.
- There is no React component or npm package to install: the tag in
The docs page covers the optional steps this guide leaves out.
Setup questions. Straight answers.
What people ask before they connect it.
No. CrispStats is one script tag in index.html, with nothing to install from npm. Components call the global window.crispstats for custom events, and route changes are counted without code.
No. The script watches the History API, which React Router and TanStack Router use, so each new URL is counted as a pageview. Only hash routers need the data-hash attribute.
The script ignores localhost and automated browsers on purpose, so development and test runs stay out of your numbers. Deploy and test on the live URL, or turn on debug mode to see each event in the console without sending it.
Call await window.crispstats?.id() in the click handler that starts checkout, and pass the id to your payment provider in the checkout metadata. Each payment is then credited to the first known source of that visitor.
No. The script loads once from the tag, outside React, so effects that run twice in development send nothing extra, and each route change is one event. 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.
Not answered here? Write to hello@crispstats.com
Go further
Related
- Web analytics for developers
A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.
- Funnels and goals
Find the step where people leave, and count what matters.
- Custom events, defined
A custom event is an action you record yourself beyond pageviews, such as a signup, a button click, a download or a video play.
- Next.js analytics
Add CrispStats to a Next.js app with one next/script element in the root layout.
- Gatsby analytics
Add CrispStats to a Gatsby site with onRenderBody in gatsby-ssr.js.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
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.