Skip to main content
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. 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

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

The same URL twice in a row is one pageview. See single-page apps.

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.

Send custom events

src/lib/UpgradeButton.svelte
The optional call (?.) keeps the button working when an ad blocker stops the script. In TypeScript, save crispstats.d.ts from TypeScript in src/. For clicks without code, use declarative events.

FAQ

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 snippet does, but the tag in index.html is simpler and loads earlier.
Your router keeps routes after #. Add data-hash to the tag and redeploy.
Serve the script from your own domain with a forwarding rule on your host. See proxy by host.

Next steps

Single-page apps

How route changes and engagement are tracked.

Custom events

Track signups, clicks and other actions with properties.

SvelteKit

The guide for SvelteKit apps.
Vite details (index.html at the project root as the entry, and the svelte and svelte-ts templates) checked against https://vite.dev/guide/ on 2026-11-23.
Last modified on November 23, 2026