Skip to content

Frameworks

Angular analytics with CrispStats

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.

Updated

Set up CrispStats on Angular

  1. 01

    Step 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. 02

    Step 2: Paste it into src/index.html

    src/index.html is the page the app is served from. 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".

    Replace the highlighted values with your own.

    src/index.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" /><scriptdeferdata-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. 03

    Step 3: Add data-hash if you use hash location

    With default routing (URLs such as /settings/billing) skip this step. `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
    <scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-hashsrc="https://in.crispstats.com/d.js"></script>
  4. 04

    Step 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 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
    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. 05

    Step 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.

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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.
  • Notes and limits

    • 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.
    • To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see proxy by host.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

  • 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.

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

  • 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.

  • 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.

  • 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.

Not answered here? Write to hello@crispstats.com

Go further

Related

  • Web analytics for developers

    A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.

  • Funnels and goals

    Find the step where people leave, and count what matters.

  • Error tracking in your analytics

    JavaScript errors grouped with the page and browser they hit.

  • React analytics

    Add CrispStats to a React or Vite app with one script tag in index.html.

  • Vue analytics

    Add CrispStats to a Vue app built with Vite by pasting one script tag into index.html.

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.