Integrations

Svelte (Vite, client-side)

Add Databuddy once and every page and route change in your Svelte app is recorded. Using SvelteKit? Follow the SvelteKit guide.

Add Databuddy

npm install @databuddy/sdk
src/main.js
import { init } from "@databuddy/sdk";
import { mount } from "svelte";
import App from "./App.svelte";

init({
  clientId: import.meta.env.VITE_DATABUDDY_CLIENT_ID,
  autocapture: { webVitals: true, errors: true },
});

mount(App, { target: document.getElementById("app") });

Your Client ID is in the dashboard under your website's Settings → General. Navigation through the History API is tracked automatically, so svelte-routing needs no extra code.

Track hash routes

Routers that use #/ URLs, such as svelte-spa-router, need hash changes turned on:

init({ clientId, autocapture: { hashChanges: true } });

Track an event

src/lib/Pricing.svelte
<script>
  import { track } from "@databuddy/sdk";
</script>

<button onclick={() => track("plan_selected", { plan: "pro" })}>
  Choose Pro
</button>

track() works with either install. Calls made before the tracker loads wait for it.

Track clicks without code

Turn on attributes: true in autocapture, or data-track-attributes on the script tag, then name elements:

<a href="/pricing" data-track="pricing_link_click" data-location="header">View pricing</a>

Each click sends a pricing_link_click event with location: "header".

How is this guide?