> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Add CrispStats to an Angular app

> Install CrispStats in an Angular app with one script tag in src/index.html. Router navigation is tracked for you; hash location needs data-hash.

To add CrispStats to an Angular app, paste the script tag inside `<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**

```html src/index.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>My app</title>
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>
```

Replace the key and the domain with yours. Keep it a tag in `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](https://docs.crispstats.com/install/typescript.md) 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

| Your router | What to do |
| - | - |
| Default routing (URLs such as `/settings/billing`) | Nothing. The router uses the History API. |
| Hash location (`withHashLocation()` or `HashLocationStrategy`, URLs such as `/#/settings`) | Add `data-hash` to the tag. Without it the part after `#` is never sent, and every route counts as the same page. |
| Server-side rendering or prerendering | Check that the rendered HTML still carries the tag (view the page source). The script only runs in the browser. |

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](https://docs.crispstats.com/tracking/single-page-apps.md).

## Check that it works

* In your browser's developer tools, the **Network** tab shows `d.js` loading from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` on the first load and on each route change. The collector answers `202`.
* In CrispStats, **Settings**, **Tracking code** shows **Receiving events.**, and the visit appears in **Realtime** within seconds.

To see events on `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](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

## Send custom events

Call the global from a component. The optional call (`?.`) keeps the app working when an ad blocker stops the script:

```ts src/app/upgrade-button.component.ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { Component } from "@angular/core";

@Component({
  selector: "app-upgrade-button",
  template: `<button (click)="upgrade()">Upgrade</button>`,
})
export class UpgradeButtonComponent {
  upgrade() {
    window.crispstats?.("upgrade clicked", { from: "settings" });
  }
}
```

For clicks you can also put `data-cs-event="upgrade clicked"` on the element in the template, with no TypeScript. See [declarative events](https://docs.crispstats.com/track/declarative-events.md).

## FAQ

> [accordion] 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.

> [accordion] 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.

> [accordion] 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](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md).

## Next steps

> [card] [Single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md)
>
> How route changes, hash routing and engagement are tracked.

> [card] [Identify signed-in users](https://docs.crispstats.com/track/identify-users.md)
>
> Tie visits to your own user id.

> [card] [TypeScript](https://docs.crispstats.com/install/typescript.md)
>
> Types for the global and the queue stub.

> [!NOTE]
>
> 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](https://angular.dev/reference/configs/file-structure) and [https://angular.dev/api/router/withHashLocation](https://angular.dev/api/router/withHashLocation) on 2026-11-23.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.