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%:
<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.jsreturns200and 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.
<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()fromafterNavigatein your root layout:src/routes/+layout.svelte <script> import { afterNavigate } from "$app/navigation"; afterNavigate(() => { if (typeof window !== "undefined") { window.onyx?.pageview(); } }); </script>
Next steps
- Track custom events such as signups, purchases, and clicks.
- Identify users to tie sessions to accounts.
- Proxy the script through your own domain to get past ad blockers.
- Script attributes let you skip or mask URLs and tag events.