> ## 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 Svelte app

> Install CrispStats in a Svelte app built with Vite by adding the script tag to index.html. Client routers are tracked for you; hash routers need data-hash.

To add CrispStats to a Svelte app built with Vite, paste the script tag inside `<head>` in `index.html` at the project root. Vite serves that file as the entry of your app, so every page gets the tag, and route changes through the History API are counted with no router code.

This page is for a Svelte app without SvelteKit. For SvelteKit, see [SvelteKit](https://docs.crispstats.com/install/sveltekit.md). You need your site's public key, which starts with `cs_pk_`, from **Settings**, **Tracking code**. There is no npm package to install.

## Add the script

**1. Add the tag to index.html**

```html index.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.0" />
    <title>My app</title>
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>
```

Replace the key and the domain with yours. Your app's own entry stays `type="module"`, but the CrispStats tag must be a classic script with `defer`: the script reads its options from its own tag.

**2. Build, deploy and open your app**

`npm run dev` serves on `localhost`, which the script ignores. Run `npm run build`, deploy the `dist/` folder, then open your live app in a normal browser window.

## Routers

| Your routing | What to do |
| - | - |
| No router, one page | Nothing. Each load is one pageview. |
| A router that changes the path with the History API (`/settings`) | Nothing. Every route change is counted. |
| A hash router (URLs such as `/#/settings`) | Add `data-hash` to the tag. Without it every route counts as the same page. |
| A router the script does not see | Add `data-manual` and call `window.crispstats?.pageview()` after each route change. See [manual pageviews](https://docs.crispstats.com/tracking/single-page-apps.md#manual-pageviews). |

The same URL twice in a row is one pageview. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md).

## Check that it works

* In your browser's developer tools, the **Network** tab shows `d.js` loading from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` on the first load and on each route change. The collector answers `202`.
* In CrispStats, **Settings**, **Tracking code** shows **Receiving events.**, and the visit appears in **Realtime** within seconds.

To see events on `npm run dev`, add `data-debug` and `localhost` in `data-domains` while you develop. See [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

## Send custom events

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

The optional call (`?.`) keeps the button working when an ad blocker stops the script. In TypeScript, save `crispstats.d.ts` from [TypeScript](https://docs.crispstats.com/install/typescript.md) in `src/`. For clicks without code, use [declarative events](https://docs.crispstats.com/track/declarative-events.md).

## FAQ

> [accordion] Can I import the script from main.ts instead?
>
> There is no npm package to import. The script reads its options from its own `<script>` tag, so it needs a tag: you could create one from code as the [Google Tag Manager](https://docs.crispstats.com/install/google-tag-manager.md) snippet does, but the tag in `index.html` is simpler and loads earlier.

> [accordion] Every route shows as / in the dashboard.
>
> Your router keeps routes after `#`. Add `data-hash` to the tag and redeploy.

> [accordion] Can I count visitors who use ad blockers?
>
> Serve the script from your own domain with a forwarding rule on your host. See [proxy by host](https://docs.crispstats.com/tracking/proxy.md).

## Next steps

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

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

> [card] [SvelteKit](https://docs.crispstats.com/install/sveltekit.md)
>
> The guide for SvelteKit apps.

> [!NOTE]
>
> Vite details (`index.html` at the project root as the entry, and the `svelte` and `svelte-ts` templates) checked against [https://vite.dev/guide/](https://vite.dev/guide/) on 2026-11-23.


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