Documentation menuJavaScript frameworks: Svelte (Vite)

JavaScript frameworks

Add Loogika to Svelte with Vite

A Svelte app that Vite builds for the browser serves a single index.html file from the project root. You paste the Loogika tag into the head of that file, and the script loads on every route of the app.

Last updated .

Which Svelte projects does this guide cover?

This guide covers Svelte apps that run fully in the browser, such as a project created with npm create vite@latest -- --template svelte. A SvelteKit project has no index.html in the root, so use the SvelteKit guide for it.

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

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

How do you add Loogika to index.html?

Paste the tag into the head of index.html, the file in your project root. The file then looks like this:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Svelte App</title>
    <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

The tag also works just before the closing </body> tag. We recommend the head, because the browser starts to load the script sooner from there.

Does Loogika track route changes in a Svelte app?

Yes. Loogika records a pageview when the app changes the address through the browser History API or through a hash route. Both svelte-routing and svelte-spa-router are examples of routers that Loogika follows this way.

Hash routes that start with #/ count as paths in Loogika, so /#/shipping is reported as /shipping. Other fragments, such as #shipping, do not create a pageview.

Route tracking depends on the SPA Navigation setting in Site Settings, which is on by default. With the setting turned off, call window.loogika.pageview() from the navigation callback of your router.

How do you track custom events in Svelte?

Every Svelte component can call window.loogika.event() with an event name and optional properties. The check for window.loogika keeps the button working when an ad blocker has stopped the script.

<script>
  function handleAction() {
    if (window.loogika) {
      window.loogika.event("user_action", { component: "ActionButton" });
    }
  }
</script>

<button onclick={handleAction}>Perform action</button>

The example uses the event attributes of Svelte 5. On Svelte 4, write on:click={handleAction} in place of onclick={handleAction}.

A plain click needs no JavaScript at all. Add data-loogika-event="user_action" to the button, as the custom events guide describes.

How do you check that Loogika is installed?

Visit your app in another browser tab and open a few routes. The pageviews show up in the Loogika dashboard within a few seconds.

If nothing arrives, check whether the page source contains script.js?siteId=. The browser Network tab should show script.js with status 200 and requests to /api/track. The script troubleshooting page covers ad blockers and wrong site IDs.