Svelte and SvelteKit
Svelte 5 and SvelteKit
For Svelte 5 and SvelteKit, add the <Analytics /> component from @viakite/sdk/svelte once, in your root layout. It starts tracking when it mounts in the browser and counts every SvelteKit navigation as a page view.
Install
1npm install @viakite/sdk
1pnpm add @viakite/sdk
1yarn add @viakite/sdk
It needs Svelte 5. The component ships as Svelte source (Analytics.svelte), and your app's own Svelte compiler builds it, so it always matches your Svelte version.
SvelteKit
Add it to the root layout, src/routes/+layout.svelte:
1<script>2import Analytics from "@viakite/sdk/svelte";3let { children } = $props();4</script>56{@render children()}7<Analytics project="pk_live_us_…" />
It renders nothing. In onMount, which runs only in the browser, it calls init() with its props. Server rendering is safe.
Svelte without SvelteKit
Add it to src/App.svelte:
1<script>2import Analytics from "@viakite/sdk/svelte";3</script>45<Analytics project="pk_live_us_…" />6<!-- your app -->
The named import works too: import { Analytics } from "@viakite/sdk/svelte".
How it behaves
- Once per page load. Props that change after it mounts are ignored, and a second component does nothing.
- Navigations are followed through the browser's history, which SvelteKit's router uses: one page view per new path, none when only the query changes. For a hash router, add
hash.
Send events
Import track, identify and reset from @viakite/sdk in any component:
1<script>2import { track } from "@viakite/sdk";3</script>45<button onclick={() => track("Signup", { plan: "pro" })}>Start free</button>
Calls made before <Analytics /> has mounted are kept and sent once it does. In load functions and other server code they do nothing.
Options
Every option of init() is a prop:
1<Analytics project="pk_live_us_…" title params={["plan", "coupon"]} outbound={false} />
To keep the key out of your code, read it from $env/static/public: import { PUBLIC_VIAKITE_KEY } from "$env/static/public". Nothing is sent from localhost; add debug to see events in the console, or localhost to send them. All props: Options.