> ## 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 Astro site

> Install CrispStats in an Astro site with one is:inline script tag in your layout. Static pages and view transitions are both counted as pageviews.

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>`:

```astro src/layouts/Layout.astro theme={"theme":{"light":"github-light","dark":"github-dark"}}
---
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_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <slot />
  </body>
</html>
```

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:

```astro src/layouts/Layout.astro theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  is:inline
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-outbound
  data-downloads
  data-vitals
  src="https://in.crispstats.com/d.js"
></script>
```

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](https://docs.crispstats.com/tracking/script-attributes.md) 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](https://docs.crispstats.com/tracking/localhost-and-testing.md).

## 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:

```astro src/components/SignupButton.astro theme={"theme":{"light":"github-light","dark":"github-dark"}}
<a href="/signup" data-cs-event="signup clicked" data-cs-prop-from="hero">Sign up</a>
```

Each `data-cs-prop-*` attribute becomes an event property, here `from: "hero"`. See [declarative events](https://docs.crispstats.com/track/declarative-events.md).

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:

```astro src/components/PlanPicker.astro theme={"theme":{"light":"github-light","dark":"github-dark"}}
<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>
```

Without view transitions, drop the `astro:page-load` wrapper and attach the listener directly. In a TypeScript project, save `crispstats.d.ts` from [TypeScript](https://docs.crispstats.com/install/typescript.md) in `src/` so the call type-checks.

## Next steps

> [card] [Declarative events](https://docs.crispstats.com/track/declarative-events.md)
>
> Track clicks with `data-cs-event` attributes and no JavaScript.

> [card] [404 pages](https://docs.crispstats.com/track/404-pages.md)
>
> Report visits to not-found pages from your 404 layout.

> [card] [Single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md)
>
> How client navigation and engagement are tracked.


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