Skip to content
Install guide · Frameworks

Install Linqry on SvelteKit

To add Linqry to SvelteKit, paste the two-line snippet into the `<head>` of `src/app.html`, the page template every route renders into. SvelteKit's client-side navigations are recorded as page views automatically.

Updated · 2 min read

Install in src/app.html

  1. Create the site in Linqry

    Start a free trial, add your production domain and copy the snippet from the Install page.

  2. Paste the snippet into the template

    src/app.html is the HTML shell for every page. %sveltekit.head% is where SvelteKit injects its own <link> and <script> elements and any <svelte:head> content; put the snippet just above it.

    <!-- src/app.html -->
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script>window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}</script>
        <script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" src="https://linqry.com/t.js"></script>
        %sveltekit.head%
      </head>
      <body data-sveltekit-preload-data="hover">
        <div style="display: contents">%sveltekit.body%</div>
      </body>
    </html>
  3. Deploy and verify

    Deploy, browse a few pages on the live site, then open Realtime in Linqry. Each page appears as a pageview in the live event stream.

Using app.html instead of <svelte:head> in your root +layout.svelte keeps the tracker out of Svelte's rendering entirely: it's plain HTML, sent with the first response, and never re-rendered.

Client-side navigation and preloading

After the first load, SvelteKit's router navigates with the History API. The tracker wraps history.pushState and replaceState and listens for popstate, so each navigation is a page view and the time spent on the previous page is sent as it is left. You don't need an afterNavigate hook.

Preloading (data-sveltekit-preload-data) fetches a page's data when a visitor hovers or taps a link, but it doesn't change the URL, so preloads are not counted as page views. Only real navigations are.

Content Security Policy

If you configure kit.csp with nonces, scripts you add to app.html by hand need the nonce placeholder, which SvelteKit fills in on each render. Also allow https://linqry.com in script-src and connect-src.

<script nonce="%sveltekit.nonce%">window.linqry=window.linqry||function(){(window.linqry.q=window.linqry.q||[]).push(arguments)}</script>
<script nonce="%sveltekit.nonce%" defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" src="https://linqry.com/t.js"></script>

Skip routes you don't want counted

Admin areas, account settings or draft previews often aren't worth tracking. Add data-exclude to the tracker tag with comma-separated paths; * matches anything. Excluded paths send nothing at all, including page views, time on page and custom events.

<script defer data-website-id="YOUR_SITE_ID" data-domain="yourdomain.com" data-exclude="/admin/*,/account/*,/drafts/*" src="https://linqry.com/t.js"></script>

A site ID per environment

app.html can read public environment variables with %sveltekit.env.NAME%. That's handy if you keep a separate Linqry site for staging:

<script defer data-website-id="%sveltekit.env.PUBLIC_LINQRY_SITE_ID%" data-domain="yourdomain.com" src="https://linqry.com/t.js"></script>

Custom events

Call window.linqry in event handlers; they only run in the browser. Add the type declaration to src/app.d.ts or any .d.ts file. For a no-JavaScript option, put data-linqry-event="name" on a link or button. Read custom events for naming rules, and the custom event tracking guide for what's worth tracking.

<script lang="ts">
  function trackDownload() {
    window.linqry?.("whitepaper-downloaded", { format: "pdf" });
  }
</script>

<a href="/files/whitepaper.pdf" onclick={trackDownload}>Download the PDF</a>
// linqry.d.ts (anywhere your tsconfig includes)
export {};

declare global {
  interface Window {
    linqry?: (
      event: string,
      props?: Record<string, string | number | boolean>,
      revenueCents?: number,
    ) => void;
  }
}

Exclude your own visits

Run this once in the browser console on your live site and that browser stops sending events:

localStorage.setItem('linqry_ignore', 'true')

Troubleshooting

  • Prerendered pages show nothing? Check the generated HTML in your build output contains the snippet; it's copied from app.html into every page.
  • Blocked by CSP? See the nonce example above and allow https://linqry.com in script-src and connect-src.
  • Still nothing? Confirm the site ID matches the Install page and test without an ad blocker.

Frequently asked questions

Does it work with adapter-static and prerendering?

Yes. Every prerendered page is built from app.html, so each one carries the snippet, and the tracker runs in the browser whatever adapter you deploy with.

Is there a Svelte package to install?

No. Linqry has no npm package; the snippet in app.html is the complete install, and window.linqry handles custom events.

Sources

  1. SvelteKit: Project structure (src/app.html placeholders) (checked Nov 9, 2026)

Start measuring your SvelteKit site

No card required · 30-day trial · No cookies