viakite
Dashboard

SDKs/Frameworks/Svelte and SvelteKit

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

terminal
1npm install @viakite/sdk
terminal
1pnpm add @viakite/sdk
terminal
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:

src/routes/+layout.svelte
1<script>
2 import Analytics from "@viakite/sdk/svelte";
3 let { children } = $props();
4</script>
5
6{@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:

src/App.svelte
1<script>
2 import Analytics from "@viakite/sdk/svelte";
3</script>
4
5<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:

src/lib/SignupButton.svelte
1<script>
2 import { track } from "@viakite/sdk";
3</script>
4
5<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:

src/routes/+layout.svelte
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.

Next steps