Add analytics to SvelteKit
A cookieless tracker for SvelteKit, installed once in the app template and left alone.
The install
SvelteKit has one HTML file that wraps every route. Put the snippet in the
<head> of src/app.html:
<!-- src/app.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
%sveltekit.head%
<script defer data-site="YOUR_SITE_CODE" src="https://www.staats.ai/s.js"></script>
</head>
<body data-sveltekit-preload-data="hover">
<div>%sveltekit.body%</div>
</body>
</html>
Put it in app.html rather than a +layout.svelte. The app template is
rendered once per document, so the tag is emitted as plain HTML and never re-evaluated. A tag placed
in a root layout can re-run on client navigation, which is where duplicate pageviews come from.
Client-side navigation is already covered
SvelteKit intercepts link clicks and navigates on the client, so most page changes never trigger
a document load. You do not need to subscribe to afterNavigate or watch
$page.url to record them. The tracker hooks pushState,
replaceState and popstate, which is what the SvelteKit router uses, so
soft navigations are counted the same as hard ones.
Adding an afterNavigate hook on top will double-count every in-app navigation.
Preloading does not inflate your numbers
The default data-sveltekit-preload-data="hover" fetches a route's data when a visitor
hovers a link, before any click. That is a data load, not a navigation: no history entry is pushed
and no pageview is recorded. Hovering every link in your nav will not show up as traffic.
Tracking a click
Add a data-staats attribute to any clickable element in a .svelte file.
The tracker uses one delegated listener, so this works on elements rendered conditionally or inside
an {#each} block:
<button data-staats="signup_click">Start free</button>
For form submissions, or anything that is not a click, call window.staats from your
handler:
<script>
function onSubmit() {
window.staats('contact_submitted')
}
</script>
No cookies
The script is ~1.8KB compressed, sets no cookies, and stores nothing on the device. Visitors are counted with a pseudonymous hash that rotates daily, and country comes from the browser timezone instead of an IP lookup.
Local development
Leave the snippet in app.html while you work. localhost is never
recorded, so vite dev stays out of your production numbers without an environment
check.
Worth knowing: src/routes/+error.svelte renders inside the same
app.html, so 404s are recorded as pageviews for paths that do not exist. If mistyped
URLs and crawler probes cluttering your top pages bothers you, filter those paths when you ask, or
serve your error page from a template without the snippet.
Reading the data
You read it by asking your coding agent. Staats is a Model Context Protocol server, so traffic, referrers, journeys and funnels come into the conversation you are already having about the code.
> did the new pricing page help?
Name the commit or the day it shipped and the agent compares up to a week before and after, and says when it’s too early to tell. See MCP Server and Agent Workflows.
Get a site code
Add https://api.staats.ai/mcp to your agent and ask it to add analytics. It signs you in
with GitHub or Google, creates the site and adds the tag above for you. To do it by hand instead,
sign in, add the site and copy its tag. Free up to 10,000
events a month, no card.