JavaScript frameworks
Add Loogika to htmx
You add Loogika to an htmx site by pasting one script tag into the head of your base layout, where the script tag for htmx already sits. htmx pages are plain HTML rendered on the server, so the tag belongs in the template that returns the full page.
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 12-character site ID stands where this example says YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
How do you add the Loogika script to an htmx layout?
Find the template that produces the complete HTML document and open it. It is the template that already loads htmx. Paste the Loogika tag inside the head element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>My htmx App</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.min.js"></script>
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
<main id="content" hx-boost="true">...</main>
</body>
</html>
Should partial responses include the tag?
No. Only responses that return a full page need the Loogika tag. Leave the tag out of every partial response that htmx swaps in, because the browser would then run the script again on every swap.
Which htmx requests count as pageviews?
Loogika counts an htmx request as a pageview when htmx records it in the browser history. A swap that leaves the URL unchanged is not a pageview.
| htmx feature | What the browser does | Pageview in Loogika |
|---|---|---|
hx-boost and hx-push-url | Adds a history entry with history.pushState() | Yes |
hx-replace-url | Replaces the current entry with history.replaceState() | Yes |
| Back and forward buttons | Fires a popstate event | Yes |
A swap without hx-push-url | Keeps the same URL | No |
History tracking depends on SPA Navigation in Site Settings, which 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 an htmx swap that keeps the URL?
Send a custom event for it. Add the data-loogika-event attribute to the element that triggers the htmx request, and Loogika records the event when a visitor clicks it. Attributes that start with data-loogika-prop- add properties to the event.
<button
hx-post="/subscribe"
hx-target="#form"
data-loogika-event="newsletter_subscribe"
data-loogika-prop-plan="free"
>
Subscribe
</button>
Loogika listens for clicks on the whole document. The attribute therefore also works on HTML that htmx swapped in after the page first loaded.
How do you record an event only after the server has answered?
Listen for the htmx:afterRequest event that htmx fires, and call window.loogika.event() from that listener. The click attribute records the click itself, also when the request fails. The listener lets you wait for the response first.
The event tracking page lists the limits for event names and properties.
How do you check that Loogika is installed?
Load your live site in a fresh browser tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.
If the dashboard stays empty, open the page source and look for script.js?siteId=. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track. The script troubleshooting page covers the other common causes.