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
- 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. - 02
Step 2: Paste it into src/app.html
src/app.htmlis 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 withdefer: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> - 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-errorsanddata-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> - 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, savecrispstats.d.tsfrom the TypeScript page insrc/, and run the queue stub fromsrc/hooks.client.tsso 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> - 05
Step 5: Deploy and open the live site
Pages on
localhost,127.0.0.1andfile://are ignored, so a visit tovite devis 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.
CrispStats on SvelteKit
What you get
- A pageview on the first load, on every
pushStateandreplaceStateSvelteKit 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.
- A pageview on the first load, on every
Notes and limits
- Link preloading with
data-sveltekit-preload-datafetches 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 areplaceStatethat keeps the URL, adds no view. - There is no Svelte package to install and no hook to write. The tag in
app.htmlis 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=priceand?sort=nameare the same page unless you keepsortwithdata-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.
- Link preloading with
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
- Web analytics for developers
A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.
- Error tracking in your analytics
JavaScript errors grouped with the page and browser they hit.
- What counts as a pageview
A pageview is one load of a page by a visitor, the most basic count in web analytics.
- Astro analytics
Add CrispStats to an Astro site with one is:inline script tag in your layout.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
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.