Skip to main content
To add CrispStats to an Astro site, put the script tag inside <head> in your shared layout, usually src/layouts/Layout.astro, with the is:inline directive. Every page that uses the layout is tracked. If your site uses view transitions, each client-side navigation is a History API navigation and counts as a pageview. You need your site’s public key, which starts with cs_pk_. Find it in your site’s settings under Tracking code. The key is safe to publish.
1

Add the tag to your layout

Add the tag inside <head>:
src/layouts/Layout.astro
Replace cs_pk_xxxxxxxx with your public key. is:inline tells Astro to leave the tag exactly as written instead of processing and bundling it. The script reads its options from its own data- attributes, so it has to stay a classic script tag pointing at https://in.crispstats.com/d.js.If your pages use more than one layout, add the tag to each one, or to a shared head component they all include.
2

Turn on more tracking (optional)

Add attributes to the same tag:
src/layouts/Layout.astro
The switches in Settings, Tracking code add data-outbound, data-downloads, data-vitals, data-errors and data-404 to the generated snippet. Leave Hash routing off: this framework routes with real paths, and data-hash only reads routes written after #. Script attributes lists every attribute the script reads, including data-exclude and data-domains, and what each one does.
3

Deploy and confirm

Pages on localhost, 127.0.0.1 and file:// are ignored, so a visit to astro dev is not counted. Deploy, then open your production site in a browser without an ad blocker.
  • In your browser’s developer tools, the Network tab shows a POST request to https://in.crispstats.com/e on each page load. The collector answers 202.
  • In CrispStats, your site’s Tracking code settings tab shows Receiving events. with the time of the last event.
To see events locally, add data-debug to the tag and localhost to data-domains while you develop: every event is logged in the console and nothing is sent. Remove both before you ship. See localhost and testing.

View transitions

With Astro’s client router (<ClientRouter />, called <ViewTransitions /> in older versions), navigating between pages swaps the content and updates the URL with history.pushState. The script watches the History API, so each of these navigations is tracked as a pageview, and the back and forward buttons are tracked through popstate. On each navigation the referrer is the previous page. If the tag is executed again after a navigation, the second copy finds the tracker already running and does nothing, so a visit is never counted twice.

Send custom events

For clicks, HTML attributes are the simplest option in Astro. They need no script, and they keep working across view transitions because the script listens for clicks on the whole page:
src/components/SignupButton.astro
Each data-cs-prop-* attribute becomes an event property, here from: "hero". See declarative events. For anything else, call the global function from a <script>. Astro runs a component’s processed script once per full page load, so with view transitions, attach listeners in an astro:page-load handler:
src/components/PlanPicker.astro
Without view transitions, drop the astro:page-load wrapper and attach the listener directly. In a TypeScript project, save crispstats.d.ts from TypeScript in src/ so the call type-checks.

Next steps

Declarative events

Track clicks with data-cs-event attributes and no JavaScript.

404 pages

Report visits to not-found pages from your 404 layout.

Single-page apps

How client navigation and engagement are tracked.
Last modified on November 18, 2026