window.crispstats global typed and safe to call at any time:
crispstats.d.tsdeclares the global, sowindow.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.
Add the global types
Save this ascrispstats.d.ts anywhere your tsconfig.json includes, for example at the project root or in src/:
crispstats.d.ts
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 callscrispstats:
crispstats-stub.ts
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:
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: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.