> ## 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 a SvelteKit app

> Install CrispStats in a SvelteKit app with one script tag in src/app.html. Client-side routing is tracked automatically, with no hooks or afterNavigate code.

To add CrispStats to a SvelteKit app, paste the script tag into `src/app.html`, inside `<head>` and above `%sveltekit.head%`. That template wraps every page, so every route is tracked. SvelteKit's client-side navigation goes through the History API, which the script watches, so you do not need an `afterNavigate` callback.

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 src/app.html**

```html src/app.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>
```

Replace `cs_pk_xxxxxxxx` with your public key. Keep it a classic script with `defer`: `type="module"` does not work, because the script reads its options from its own tag.

**2. Turn on more tracking (optional)**

Add attributes to the same tag:

```html src/app.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-outbound
  data-vitals
  data-errors
  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 `vite 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 the first load and on each client-side navigation. 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).

## How client routing is counted

The script tracks a pageview on the first load, on every `history.pushState` and `replaceState`, and on `popstate` (the back and forward buttons). The same URL twice in a row is one pageview, so `goto()` to the current page, or a `replaceState` that keeps the URL, does not add a view. On a client-side navigation the referrer is the previous page.

Preloading with `data-sveltekit-preload-data` fetches data without changing the URL, so it is not counted.

## Send custom events

Call the global function from a component:

```svelte src/lib/UpgradeButton.svelte theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script lang="ts">
  function upgrade() {
    window.crispstats?.("upgrade clicked", { from: "settings" });
  }
</script>

<button onclick={upgrade}>Upgrade</button>
```

In a TypeScript project, save `crispstats.d.ts` from [TypeScript](https://docs.crispstats.com/install/typescript.md) in `src/`. It types every method of the global, and the queue stub there (in `src/hooks.client.ts`) keeps early calls from being lost.

The optional call (`?.`) keeps the button working when an ad blocker stops the script from loading. For clicks you can also use `data-cs-event` attributes and no JavaScript, see [declarative events](https://docs.crispstats.com/track/declarative-events.md).

## Next steps

> [card] [Custom events](https://docs.crispstats.com/track/custom-events.md)
>
> Track signups, clicks and other actions with properties.

> [card] [Exclude traffic](https://docs.crispstats.com/tracking/exclude-traffic.md)
>
> Skip admin paths, staging hosts and your own browser.

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


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