@crispstats/tracker is the same tracker as the script tag, as functions you import. It counts pageviews (single-page navigation included), sends custom events and revenue, and gives you the visitor id for checkout metadata. It sets no cookies, stores nothing on the device by default, has no dependencies and ships its own TypeScript types.
Use the package or the script tag on a page, not both: each would count the visit.
You need your site’s public key, which starts with cs_pk_. Settings, Tracking code, npm shows the code below with your key and domains filled in.
1
Install the package
2
Start it with your site key
Call Importing the package touches no browser API, so it is safe in server rendering. Every function does nothing on the server and never throws.
init once, in code that runs in the browser, for example your app’s entry file:src/main.ts
3
Deploy and check
Visits on
localhost are not counted. Deploy, then open your site in a normal browser window: your visit appears in Realtime within a few seconds. To see events locally, pass debug: true, which logs each event in the console and sends nothing.Track events and revenue
Calls made before
init wait for it, up to 50 of them, so the order does not matter.
Options
init takes the same options as the tag’s script attributes, as camel-case names:
Modules
Each module is its own export, so your bundler drops the ones you do not pass:outbound() tracks clicks to other sites, downloads({ extensions? }) file downloads, vitals() web vitals for the Performance page, errors() JavaScript errors for the Errors page, notFound({ selector? }) 404 pages, and checkouts() adds the visitor id to hosted checkout links (Stripe Payment Links, Lemon Squeezy, Polar, Dodo Payments).
React and Next.js
<CrispStats /> takes the init options as props, starts tracking once after hydration and renders nothing. It is a client component, so a Next.js root layout can stay a server component:
app/layout.tsx
<Link>, router.push and the back button is tracked with no extra code, in Next.js, React Router and TanStack Router alike. In client components, useCrispStats() returns { track, pageview, identify, revenue, getVisitorId }:
components/upgrade-button.tsx
From your server
@crispstats/tracker/server uses the global fetch only: Node 18 and later, Cloudflare Workers, Vercel Edge, Deno and Bun. Both helpers resolve to whether the collector accepted the request and never throw.
trackServer also takes userId, url and revenue: { amount, currency }. Create the secret key under API keys.
reportBotHit(request, { site }) reports AI crawler visits from middleware or an edge function, because crawlers never run JavaScript. It sends only the User-Agent, the path and the status. In Next.js, in proxy.ts:
proxy.ts
Privacy
The package keeps the script’s promises:- No cookies and no fingerprinting. Nothing is written to the device unless you turn on
persist, or the site owner opts their own browser out with the opt-out link in the site’s settings. - Each event carries the site key, the page URL with its query string cut to campaign parameters, the referrer, the screen width, the browser language, the timezone name and a timestamp.
- The IP address and User-Agent arrive with the request like with any request, and are not stored.
Next steps
Custom events
Names, properties and the rules they follow.
Visitor id at checkout
Link payments to the visit that brought them.
Proxy
Send events through your own domain.