# Angular analytics with CrispStats

Updated 2026-11-23

Angular analytics takes one CrispStats script tag inside the head of src/index.html: paste it, then build and deploy. Angular's router changes the URL through the History API, which the script watches, so every route change is counted with no service, module or router event subscription.

## Steps

### 1. Copy the tag with your key

In the dashboard, open your site's **Settings**, then **Tracking code**, and choose the **HTML** tab. It shows the tag with your public key (it starts with `cs_pk_`) and your domain filled in. The key is safe to publish.

### 2. Paste it into src/index.html

`src/index.html` is [the page the app is served from](https://angular.dev/reference/configs/file-structure). Add the tag inside `<head>`. 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"`.

`src/index.html`

```html
<!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_your_site_key"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>
```

### 3. Add data-hash if you use hash location

With default routing (URLs such as `/settings/billing`) skip this step. [`withHashLocation()`](https://angular.dev/api/router/withHashLocation) and `HashLocationStrategy` keep the route in the URL fragment (`/#/settings`), which the script cuts from every URL unless the tag has `data-hash`. Without it, every route counts as the same page.

`src/index.html`

```html
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-hash
  src="https://in.crispstats.com/d.js"
></script>
```

### 4. Send a custom event (optional)

Call the global from a component. The optional call (`?.`) keeps the app working when an ad blocker stops the script. Save `crispstats.d.ts` from the [TypeScript page](/docs/install/typescript) in `src/` so `window.crispstats` is typed, and check that the `include` list of `tsconfig.app.json` covers it. For clicks you can also put `data-cs-event="upgrade clicked"` on the element in the template, with no TypeScript.

`src/app/upgrade-button.component.ts`

```ts
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" });
  }
}
```

### 5. Build, deploy and open the live app

`ng serve` runs on `localhost`, which the script ignores. Run `ng build`, deploy, then open your live app in a normal browser window without an ad blocker.

## Check that it works

In your browser's developer tools, the **Network** tab shows `d.js` loading and a `POST` to `https://in.crispstats.com/e` on the first load and on each route change, and the collector answers `202`. In the dashboard, **Settings**, **Tracking code** shows **Receiving events.** and the visit appears on **Realtime** within seconds. To see events on `ng serve`, add `data-debug` and `localhost` in `data-domains` while you develop: each event is logged in the console and nothing is sent. See [debug mode](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for the first load and every router navigation, including `routerLink`, `router.navigate()` and the back and forward buttons.
- The referrer of the first load kept for the whole visit, so a session that starts from search or an AI assistant such as ChatGPT keeps that source across later route changes.
- A core script of about 2.2 KB gzipped that loads outside your bundles, so your build budgets do not change.
- Time on page and scroll depth per route, sent before each navigation.
- Revenue by source once you pass `await window.crispstats?.id()` with the request that starts checkout: see [Stripe revenue attribution](/integrations/stripe).

## Notes for Angular

- There is no Angular module, service or npm package to install. The tag is the whole install, and components call `window.crispstats` directly.
- With server-side rendering or prerendering, view the page source to check the rendered HTML still carries the tag. 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.
- Query strings are cut to UTM and a few other parameters before a URL is compared, so a change of query parameters alone is not a new pageview. Keep more with `data-keep-params`.
- To tie visits to signed-in users, call `identify` with your own user id after login. See [identify users](/docs/track/identify-users).
- To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see [proxy by host](/docs/tracking/proxy).

Full reference: [Angular in the docs](https://docs.crispstats.com/install/angular)

## Frequently asked questions

### Do I need an Angular module or service for analytics?

No. The script tag in src/index.html is the whole install, and route changes are seen without subscribing to router events. Call window.crispstats directly where you need custom events.

### My routes use a hash. Why does every page show as the home page?

Hash location keeps the route after the hash, which the script drops unless the tag has data-hash. Add it and redeploy.

### Why not add the script to the scripts array in angular.json?

Entries there are bundled into your build, so the script loses its own tag. It reads its key and options from the data attributes of that tag, so it has to stay a script tag in index.html.

### Does it work with Angular server-side rendering?

Yes. With server rendering or prerendering, the tag in src/index.html ends up in the HTML each route is served with, and the script starts only once the page reaches the browser, so nothing runs on the server. View the source of a rendered route to confirm the tag is there.

### How many events does an Angular app use?

One per pageview, and every router navigation to a new URL is a pageview, plus one per custom event you send. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Web analytics for developers](/for/developers)
- [Funnels and goals](/features/funnels)
- [Error tracking in your analytics](/features/error-tracking)
- [React analytics](/integrations/react)
- [Vue analytics](/integrations/vue)
