🚀

Beam for SvelteKit

Add cookie-free, GDPR-compliant analytics to your SvelteKit app in under 2 minutes. No consent banner required. Uses +layout.svelte with afterNavigate to capture every client-side navigation.

Get started free Explore live demo

No credit card required and setup in minutes

Install Beam on SvelteKit

Replace YOUR_SITE_ID with your Beam site ID from the dashboard.

Add Beam to src/routes/+layout.svelte

<!-- src/routes/+layout.svelte -->
<script>
  import { onMount } from 'svelte'
  import { afterNavigate } from '$app/navigation'

  onMount(() => {
    // Inject the Beam script once on first browser load
    const script = document.createElement('script')
    script.defer = true
    script.src = 'https://beam-privacy.com/js/beam.js'
    script.setAttribute('data-site-id', 'YOUR_SITE_ID')
    document.head.appendChild(script)
  })

  // Fire a pageview on every SvelteKit client-side navigation
  afterNavigate(({ to }) => {
    window.beam?.track?.('pageview', { path: to?.url.pathname ?? window.location.pathname })
  })
</script>

<slot />

onMount runs once on initial browser load. afterNavigate fires after every SvelteKit client-side route transition — keeping Top Pages accurate across your entire app without a full page reload.

Alternative: add script to app.html (simpler, no SPA route tracking)

<!-- src/app.html -->
<!DOCTYPE html>
<html lang="%sveltekit.lang%">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    %sveltekit.head%
    <script
      defer
      src="https://beam-privacy.com/js/beam.js"
      data-site-id="YOUR_SITE_ID">
    </script>
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

This approach works for mostly server-rendered SvelteKit sites where full-page navigations are the norm. For SPAs with client-side routing, use the +layout.svelte approach above to capture route changes.

Verify your SvelteKit integration

Use this checklist after publish so you know tracking is actually live in production.

Why Beam works well for SvelteKit

🍪

No cookie banner required

Beam is cookie-free by design — no consent UI to build, no GDPR overhead for your SvelteKit app.

Sub-2KB, SSR-safe

onMount keeps Beam out of server-side rendering entirely, so your SvelteKit SSR output is never affected.

🧭

Full SPA route tracking

afterNavigate captures every client-side navigation in SvelteKit so your Top Pages report is complete and accurate.

🔒

Privacy by default

No PII, no fingerprinting, no ad-tech — just page-level metrics your team can act on without a consent banner.

Ready to add Beam to SvelteKit?

Start free, validate your first pageview, and then scale when traffic grows.

Related setup guides