Documentation menuJavaScript frameworks: Astro

JavaScript frameworks

Add Loogika to Astro

You add Loogika to an Astro site by placing one script tag in the head of your shared layout component. Astro pages render their HTML document through that layout, so every page that uses the layout is tracked.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in the Loogika dashboard, under the Tracking Script tab. Your own 12-character site ID replaces YOUR_SITE_ID in the tag:

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

How do you add the Loogika script to an Astro layout?

The tag belongs inside the head element of your shared layout. In most projects that file is src/layouts/Layout.astro. Add the is:inline attribute to the tag. Astro then leaves the tag untouched and does not try to bundle the script.

---
const { title } = Astro.props;
---
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <title>{title}</title>
    <script is:inline src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
  </head>
  <body>
    <slot />
  </body>
</html>

What if your Astro site has no shared layout?

Without a shared layout, every page in src/pages/ needs the tag in its own head. A shared layout is less work to maintain, because the tag then lives in one file.

Does Loogika work with Astro view transitions?

Yes. By default an Astro site consists of separate pages, so the browser loads a full page on every link click and Loogika counts a new pageview. Once you turn on view transitions, Astro changes the URL in the browser without a full load, and Loogika records those route changes as pageviews.

Route changes without a page load are counted while SPA Navigation is on in Site Settings. It is on for a new site. The Loogika script waits 500 milliseconds after the URL changes before it records the pageview.

How do you track custom events in Astro?

Any .astro component can send an event when its client-side script calls window.loogika.event(). Inside a framework island you call the same function the way that framework handles clicks.

<button id="tracked-button">Click me</button>

<script>
  document.getElementById("tracked-button")?.addEventListener("click", () => {
    if (window.loogika) {
      window.loogika.event("astro_button_click", { source: "TrackedButton.astro" });
    }
  });
</script>

A click needs no script at all when you use a data attribute. Add data-loogika-event="astro_button_click" to the button, and Loogika sends the event on each click. See how to track events for properties and limits.

How do you check that Loogika is installed?

Open your live Astro site in a new tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.

If no data arrives, open the page source and look for script.js?siteId=. A page without the tag does not use the layout you edited. In the browser's Network tab, script.js should return status 200 and POST requests should go to /api/track.

Why are pageviews missing with view transitions?

Missing pageviews with Astro view transitions are rare, so test the default behaviour first. If route changes do go missing, add a client-side script with a listener for the astro:after-swap event that Astro fires. Inside the listener, window.loogika.pageview() records each page for you.

Low numbers on every page point to ad blockers, not to Astro. You can load the script through your own domain, as the proxy guide explains.