<head> in src/index.html, the page the app is served from. Angular’s router changes the URL through the History API, which the script watches, so every route change is counted with no router code.
You need your site’s public key, which starts with cs_pk_. Find it in your site’s settings under Tracking code, on the HTML tab. The key is safe to publish. There is no npm package to install.
Add the script
1
Add the tag to src/index.html
src/index.html
index.html rather than an entry in the scripts option of angular.json: the script reads its options from the data- attributes of its own <script> tag. Keep defer and do not add type="module".2
Add types (optional)
Save
crispstats.d.ts from TypeScript in src/ so window.crispstats is typed. Check that the include list of tsconfig.app.json covers it.3
Build, deploy and open your app
ng serve runs on localhost, which the script ignores. Run ng build, deploy, then open your live app in a normal browser window.Router setups
The same URL twice in a row is one pageview, so a redirect that replaces the URL with itself does not add a view. See single-page apps.
Check that it works
- In your browser’s developer tools, the Network tab shows
d.jsloading fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/eon the first load and on each route change. The collector answers202. - In CrispStats, Settings, Tracking code shows Receiving events., and the visit appears in Realtime within seconds.
ng serve, add data-debug and localhost in data-domains while you develop. Every event is logged in the console and nothing is sent. See localhost and testing.
Send custom events
Call the global from a component. The optional call (?.) keeps the app working when an ad blocker stops the script:
src/app/upgrade-button.component.ts
data-cs-event="upgrade clicked" on the element in the template, with no TypeScript. See declarative events.
FAQ
Do I need an Angular module or service?
Do I need an Angular module or service?
No. The script tag is the whole install, and route changes are seen without router hooks. Call
window.crispstats directly where you need custom events.My routes use a hash. Why does every page show as /?
My routes use a hash. Why does every page show as /?
Hash location keeps the route after
#, which the script drops unless the tag has data-hash. Add it and redeploy.How do I pass the visitor id to my checkout?
How do I pass the visitor id to my checkout?
Call
await window.crispstats?.id() in the click handler that starts checkout and send it with the request. See visitor id at checkout.Next steps
Single-page apps
How route changes, hash routing and engagement are tracked.
Identify signed-in users
Tie visits to your own user id.
TypeScript
Types for the global and the queue stub.
Angular details (
src/index.html as the page the app is served from, and withHashLocation() using the URL fragment instead of the History API) checked against https://angular.dev/reference/configs/file-structure and https://angular.dev/api/router/withHashLocation on 2026-11-23.