Stickybeak Docs Dashboard Get early access

Frameworks

Svelte

Svelte analytics with Stickybeak starts by adding its script to your rendered pages; this guide shows you where it belongs and how to track page views, goals and purchase revenue. It is a lighter alternative to Google Analytics for Svelte, with live visitors, goals and revenue attribution in one script.

Using SvelteKit?

SvelteKit has its own page template, so follow the SvelteKit guide.

Add the script

  1. Copy your snippet

    In Stickybeak, open Settings, pick your site and copy the HTML snippet.

  2. Paste it into index.html

    It's at the project root. Paste it before </head>.

    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My app</title>
        <script
          defer
          data-site="YOUR_SITE_ID"
          data-domain="example.com"
          src="https://app.usestickybeak.com/_cfw/s.js"
        ></script>
      </head>
      <body>
        <div id="app"></div>
        <script type="module" src="/src/main.ts"></script>
      </body>
    </html>

    Keep it a plain <script>, without type="module".

  3. Build and deploy

    Visits appear once the new build is live. Local development on localhost isn't counted.

To keep the ID out of the file, put VITE_STICKYBEAK_SITE_ID=YOUR_SITE_ID in .env.production and write data-site="%VITE_STICKYBEAK_SITE_ID%". Vite fills it in at build time.

Check it's working

Test the deployed site, then verify your install.

Track goals

Add data-goal to rendered links or buttons. For other events, use the typed queued helper below. See Custom goals.

<button data-goal="initiate_checkout" data-goal-plan="pro" onclick={openCheckout}>Upgrade</button>
type GoalProps = Record<string, string | number | boolean>;
type Stickybeak = ((goal: string, props?: GoalProps) => void) & { q?: unknown[][] };

declare global {
  interface Window {
    stickybeak?: Stickybeak;
  }
}

export function track(goal: string, props?: GoalProps): void {
  if (typeof window === "undefined") return; // on the server: nothing to do
  if (!window.stickybeak) {
    // Hold calls made before the script loads; it sends them when it arrives.
    const queue: Stickybeak = (...args) => {
      (queue.q ??= []).push(args);
    };
    window.stickybeak = queue;
  }
  window.stickybeak(goal, props);
}
<script lang="ts">
  import { track } from "./stickybeak";

  let { url }: { url: string } = $props();

  async function share() {
    await navigator.clipboard.writeText(url);
    track("share_link_copied");
  }
</script>

<button type="button" onclick={share}>Copy link</button>

Track purchases

After confirming an order in the browser, send:

window.stickybeak?.("purchase", { revenue: 49, currency: "USD", order_id: "ORDER_ID" });

Use real values; see Revenue.

Page changes

History routers such as svelte-routing count new paths. Hash routers such as svelte-spa-router appear as one page. See Single-page apps.

If it isn't working

Check the production site ID and domain, then see Troubleshooting.