# Astro analytics with CrispStats

Updated 2026-11-23

An Astro site gets analytics from one CrispStats script tag with the is:inline directive, placed inside the head of your shared layout, usually src/layouts/Layout.astro. Every page that uses the layout is tracked, and with view transitions each client-side navigation counts as a pageview too.

## Steps

### 1. Copy the tag with your key

In the dashboard, open your site's **Settings** and select **Tracking code**. The tag there has your public key (it starts with `cs_pk_`) and your domain filled in. The key is safe to publish.

### 2. Add the tag to your layout with is:inline

Add the tag inside `<head>` and keep `is:inline`. That directive [tells Astro to leave the tag as written](https://docs.astro.build/en/reference/directives-reference/) instead of processing and bundling it. The script reads its options from its own `data-` attributes, so it has to stay a classic tag pointing at the hosted file. If your pages use more than one layout, add the tag to each, or to a shared head component they all include.

`src/layouts/Layout.astro`

```astro
---
interface Props {
  title: string;
}

const { title } = Astro.props;
---

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <title>{title}</title>
    <script
      defer
      is:inline
      data-site="cs_pk_your_site_key"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <slot />
  </body>
</html>
```

### 3. Turn on more tracking (optional)

Add attributes to the same tag. The switches in **Settings**, **Tracking code** add `data-outbound`, `data-downloads`, `data-vitals`, `data-errors` and `data-404`. Leave **Hash routing** off: Astro routes with real paths.

`src/layouts/Layout.astro`

```astro
<script
  defer
  is:inline
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-outbound
  data-downloads
  data-vitals
  src="https://in.crispstats.com/d.js"
></script>
```

### 4. Track clicks with attributes

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. Each `data-cs-prop-*` attribute becomes a property, here `from: "hero"`.

`src/components/SignupButton.astro`

```astro
<a href="/signup" data-cs-event="signup clicked" data-cs-prop-from="hero">Sign up</a>
```

### 5. Send other events from a script (optional)

For anything else, call the global 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. Without view transitions, attach the listener directly.

`src/components/PlanPicker.astro`

```astro
<select id="plan">
  <option value="starter">Starter</option>
  <option value="pro">Pro</option>
</select>

<script>
  document.addEventListener("astro:page-load", () => {
    document.getElementById("plan")?.addEventListener("change", (event) => {
      const plan = (event.target as HTMLSelectElement).value;
      window.crispstats?.("plan selected", { plan });
    });
  });
</script>
```

### 6. Build, deploy and open the live site

Pages on `localhost`, `127.0.0.1` and `file://` are ignored, so `astro dev` and `astro preview` are not counted. Deploy, then open the production site in a browser without an ad blocker.

## Check that it works

In your browser's developer tools, the **Network** tab shows a `POST` to `https://in.crispstats.com/e` on each page load, and the collector answers `202`. In the dashboard, the site's **Tracking code** tab shows **Receiving events.** with the time of the last event, and the visit appears on **Realtime**. To see events locally, add `data-debug` to the tag and `localhost` to `data-domains` while you develop: each event is logged in the console and nothing is sent. Remove both before you ship. See [localhost and testing](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for every static page load, and for every view-transition navigation made through the client router, with the previous page as referrer.
- Sources and channels, including search, newsletters tagged with [UTM parameters](/glossary/utm-parameters) and AI assistants such as ChatGPT and Perplexity.
- A core script of about 2.2 KB gzipped, loaded with `defer`, so a fast static site stays fast.
- Time on page and scroll depth for each page, useful on docs and blogs where reading is the goal.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.

## Notes for Astro

- Without `is:inline`, Astro processes the tag and the script cannot read its options. Keep the directive.
- With view transitions (`<ClientRouter />`, called `<ViewTransitions />` in older versions), the content is swapped and the URL updated with `history.pushState`, which the script watches. If the tag runs again after a navigation, the second copy finds the tracker running and does nothing, so no visit is counted twice.
- Not-found pages can be reported from your 404 layout with `data-404`. See [404 pages](/docs/track/404-pages).
- There is no Astro integration or npm package to install. The tag in the layout is the whole install, for static output and for server output on any adapter.
- Leave out private paths with `data-exclude`, and your own visits with the ignore flag in your browser. See [exclude traffic](/docs/tracking/exclude-traffic).
- 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: [Astro in the docs](https://docs.crispstats.com/install/astro)

## Frequently asked questions

### Why does the tag need is:inline in Astro?

Astro processes and bundles script tags by default. is:inline leaves the tag exactly as written, so it keeps its src and data attributes, which is where the script reads its options from.

### Are view transitions counted as pageviews?

Yes. The client router updates the URL with history.pushState, which the script watches, so each navigation is a pageview and the back and forward buttons are counted too.

### Does it work on a fully static Astro site?

Yes. The tag is in the built HTML of every page that uses the layout, and the script runs in the visitor's browser. No server code is needed.

### Does a static Astro site need a cookie banner for this?

CrispStats sets no cookies and writes nothing to the visitor's device by default, and the IP address is discarded once the daily visitor hash is computed. A static Astro build sets no cookies of its own, so what is left to check is anything else your pages embed. This is not legal advice.

### Do view transitions use more events?

No more than full page loads would. Each view-transition navigation is one pageview, the same count a site without the client router gets. 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

- [Privacy-friendly analytics for bloggers](/for/bloggers)
- [Core Web Vitals in plain English](/blog/core-web-vitals-plain-english)
- [Free UTM builder](/tools/utm-builder)
- [SvelteKit analytics](/integrations/sveltekit)
