Skip to content

Frameworks

SvelteKit analytics with CrispStats

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.

Updated

Set up CrispStats on SvelteKit

  1. 01

    Step 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. 02

    Step 2: Paste it into src/app.html

    src/app.html is the page template 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.

    Replace the highlighted values with your own.

    src/app.html
    <!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><scriptdeferdata-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. 03

    Step 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
    <scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-outbounddata-vitalsdata-errorssrc="https://in.crispstats.com/d.js"></script>
  4. 04

    Step 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 in src/, and run the queue stub from src/hooks.client.ts so early calls are not lost.

    src/lib/UpgradeButton.svelte
    <script lang="ts">function upgrade() {window.crispstats?.("upgrade clicked", { from: "settings" });}</script><button onclick={upgrade}>Upgrade</button>
  5. 05

    Step 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.

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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 and limits

    • 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.
    • To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see proxy by host.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

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

  • 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.

  • 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.

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

  • 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.

Not answered here? Write to hello@crispstats.com

Go further

Related

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.