Skip to main content
CrispStats is a script tag, not an npm package: there is nothing to install. In a TypeScript app, two copy-paste files make the window.crispstats global typed and safe to call at any time:
  • crispstats.d.ts declares the global, so window.crispstats?.("signup") type-checks.
  • The queue stub installs a placeholder that queues calls made before the script has loaded. The script replays them in order.
Install the script tag first. These files only add types and early calls.

Add the global types

Save this as crispstats.d.ts anywhere your tsconfig.json includes, for example at the project root or in src/:
crispstats.d.ts
The file has no import or export, so its declarations are global. The global is optional (crispstats?) because it is undefined until the stub or the script has run, and stays undefined when an ad blocker stops the script. Call it with ?.:

Queue calls made before the script loads

The script is deferred, so your app’s code can run first. Run this stub as early as possible, before any code calls crispstats:
crispstats-stub.ts
It is valid strict TypeScript: no any, no arguments, and it does nothing on the server. It installs itself only when window.crispstats is not already a function, so it never replaces the script if the script loaded first, and it replaces an element exposed by id="crispstats". Each queued call is an array; a method call carries its name in m. The script replays the queue in order once it starts. Where to run it:

Get the visitor id

crispstats.id() exists only on the loaded script, not on the stub, because it returns a value the stub cannot know. Call it from code that runs after the page has loaded, such as a click handler, and check that it is there:
It resolves to undefined when the script is not tracking, in debug mode, or when the request fails. See visitor id at checkout.

Use it from components

The global works the same in every framework. In React:
Props are flat: strings, numbers, booleans and null. Anything else is sent as its string form. See custom events.

Next steps

Next.js

The script element, a proxy route handler and 404s.

Custom events

Track signups, clicks and other actions with properties.

Script attributes

Every option and method of the global.
Last modified on November 18, 2026