# SvelteKit analytics with CrispStats

Updated 2026-11-23

SvelteKit analytics is one CrispStats script tag in src/app.html, inside the head and above %sveltekit.head%, followed by a deploy. SvelteKit's client-side navigation goes through the History API, which the script watches, so you write no afterNavigate callback and install no package.

## 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. Paste it into src/app.html

`src/app.html` is [the page template](https://svelte.dev/docs/kit/project-structure) that wraps every route, so one tag covers the whole app. Put it inside `<head>`, above `%sveltekit.head%`. Keep it a classic script with `defer`: `type="module"` does not work, because the script reads its options from its own tag.

`src/app.html`

```html
<!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_your_site_key"
      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>
```

### 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: SvelteKit routes with real paths.

`src/app.html`

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

### 4. Send a custom event (optional)

Call the global from a component. The optional call (`?.`) keeps the button working when an ad blocker stops the script. In TypeScript, save `crispstats.d.ts` from the [TypeScript page](/docs/install/typescript) in `src/`, and run the queue stub from `src/hooks.client.ts` so early calls are not lost.

`src/lib/UpgradeButton.svelte`

```svelte
<script lang="ts">
  function upgrade() {
    window.crispstats?.("upgrade clicked", { from: "settings" });
  }
</script>

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

### 5. Deploy and open the live site

Pages on `localhost`, `127.0.0.1` and `file://` are ignored, so a visit to `vite dev` is not counted. Deploy with your adapter as usual, 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 the first load and on each client-side navigation, and the collector answers `202`. In the dashboard, the site's **Tracking code** 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: 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 on the first load, on every `pushState` and `replaceState` SvelteKit makes, and on back and forward navigation, with the previous page as referrer.
- Countries, devices and browsers per route, next to the channel that brought each visit, AI assistants included.
- A core script of about 2.2 KB gzipped that loads with `defer`, outside your bundle, so it never blocks the page from rendering.
- Time on page and scroll depth per route, sent before each navigation.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.

## Notes for SvelteKit

- Link preloading with `data-sveltekit-preload-data` fetches data without changing the URL, so a hover is never counted as a pageview.
- The same URL twice in a row is one pageview, so `goto()` to the current page, or a `replaceState` that keeps the URL, adds no view.
- There is no Svelte package to install and no hook to write. The tag in `app.html` is the whole install, for every adapter, because the script runs in the browser.
- Query strings are cut to UTM and a few other parameters before a URL is compared, so `?sort=price` and `?sort=name` are the same page unless you keep `sort` with `data-keep-params`.
- Leave out private paths, such as an admin area, with `data-exclude`. 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: [SvelteKit in the docs](https://docs.crispstats.com/install/sveltekit)

## Frequently asked questions

### Do I need afterNavigate to track SvelteKit routes?

No. SvelteKit navigates through the History API, which the script watches, so each new URL is a pageview with no code in your layout.

### Does it work with prerendered and server-rendered pages?

Yes. The tag is part of app.html, so every page SvelteKit renders or prerenders carries it, and the script counts the page when a visitor opens it in the browser.

### Why do I see no visits from vite dev?

The script ignores localhost and automated browsers on purpose, so development and test runs stay out of your numbers. Test on a deployed URL, or use debug mode to log each event without sending it.

### Is a hovered link counted as a pageview?

No. Preloading with data-sveltekit-preload-data fetches data on hover without changing the URL, and only a change of URL is a pageview.

### Does the adapter change what it costs?

No. The script runs in the browser whatever adapter you deploy with, so the price depends only on events. Every account starts with a 14-day free trial with no card. Then Starter is $5 a month for 250,000 events, and Growth is $12 for 1,000,000 with unlimited sites.

## Related

- [Web analytics for developers](/for/developers)
- [Error tracking in your analytics](/features/error-tracking)
- [What counts as a pageview](/glossary/pageview)
- [Astro analytics](/integrations/astro)
