For the complete documentation index, see llms.txt. This page is also available as Markdown.

Svelte / SvelteKit

Svelte 5 / SvelteKit + @millimetric/track.

Install

npm i @millimetric/track

SvelteKit — root layout

<!-- src/routes/+layout.svelte -->
<script lang="ts">
  import { onMount } from "svelte";
  import { init } from "@millimetric/track";
  import { PUBLIC_AOA_KEY, PUBLIC_AOA_HOST } from "$env/static/public";

  let { children } = $props();

  onMount(() => {
    init({
      key: PUBLIC_AOA_KEY,
      host: PUBLIC_AOA_HOST ?? "https://api.millimetric.ai"
    });
  });
</script>

{@render children()}

The SDK patches history.pushState, so SvelteKit's client-side navigations auto-fire $pageview.

Identify on login

Mount once in +layout.svelte next to your other auth-aware components.

(For Svelte 4, replace the $effect rune with a reactive statement: $: if ($user) identify($user.id);)

Track on intent

Server-side events — +server.ts

Use @millimetric/track-node with an sk_* key. The variable goes in the private env namespace so it never ships to the client.

Form actions

Same idea — call track() inside the default action, flush() before returning:

SSR safety

Don't import @millimetric/track in code that runs server-side — it touches localStorage and navigator. SvelteKit's +page.server.ts and +layout.server.ts files run on the server only; reach for @millimetric/track-node there instead.

onMount and $effect only fire on the client, so calling browser-SDK functions inside them is always safe.

Plain Svelte (no SvelteKit)

Same flow, simpler init:

Last updated

Was this helpful?