OnyxMetric
Integration GuidesSvelte

SvelteKit

Add OnyxMetric analytics to your SvelteKit app

SvelteKit serves every page through one HTML shell, src/app.html. Add the tag there and every route is covered.

Copy your tracking snippet

In OnyxMetric, open your site's Site Settings and copy the tracking script. It looks like this:

<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>

YOUR_SITE_ID is the numeric ID of your site.

Add the snippet to SvelteKit

Open src/app.html and add the tag inside <head>, near %sveltekit.head%:

src/app.html
<head>
  <meta charset="utf-8" />
  <link rel="icon" href="%sveltekit.assets%/favicon.png" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>
  %sveltekit.head%
</head>

Client-side navigation is recorded automatically, so no router hook is needed.

Check that it works

Load your live site and click through a few pages. Pageviews show up in the OnyxMetric dashboard within seconds.

If nothing arrives:

  • View the page source and search for script.js?siteId= to confirm the tag is present.
  • In the browser's Network tab, check that script.js returns 200 and that requests go to /api/track.
  • Turn off ad blockers, or proxy the script through your own domain.
  • Read script troubleshooting for other causes.

Track custom events

Svelte components also run on the server in SvelteKit, so check for window.

src/lib/components/ActionButton.svelte
<script>
  function handleAction() {
    if (typeof window !== "undefined") {
      window.onyx?.event("user_action", { component: "ActionButton" });
    }
  }
</script>

<button on:click={handleAction}>Perform action</button>

Troubleshooting

  • Missing pageviews after navigation: automatic tracking covers SvelteKit's router, so test that first. To force it, call pageview() from afterNavigate in your root layout:

    src/routes/+layout.svelte
    <script>
      import { afterNavigate } from "$app/navigation";
    
      afterNavigate(() => {
        if (typeof window !== "undefined") {
          window.onyx?.pageview();
        }
      });
    </script>

Next steps

On this page