JavaScript frameworks
Add Loogika to Preact
You add Loogika to a Preact app by pasting one script tag into the head of index.html in the project root. A Preact app has that single HTML file, so the script loads on every route.
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 site ID, a 12-character string, takes the place of YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
The steps on this page fit an app created with npm init preact or with the preact template of Vite. In both setups, index.html lives in the project root.
How do you add the Loogika script to Preact?
Paste the tag into the head element of index.html, the file in your project root:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Preact</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/index.jsx"></script>
</body>
</html>
The tag also works just before the closing </body> tag. In the head element the browser starts to fetch the script sooner.
Does Loogika track route changes in Preact?
Yes. Loogika records a pageview whenever the URL changes through a router such as preact-iso or preact-router. You do not call anything from your own code.
Route changes count while SPA Navigation is on in Site Settings, and it is on for a new site. The Loogika script waits 500 milliseconds after a route change before it records the pageview. A redirect inside the Preact app is then counted once.
How do you track custom events in Preact?
Call window.loogika.event() from any component. Check that window.loogika exists before the call. A blocked script, or one that is still loading, would otherwise cause an error in your click handler.
export function SignupButton() {
const handleClick = () => {
if (window.loogika) {
window.loogika.event("signup_click", { location: "hero" });
}
};
return <button onClick={handleClick}>Sign up</button>;
}
A data attribute does the same job for a plain click. Write data-loogika-event="signup_click" on the button, and Loogika sends the event without a handler. The event tracking page shows how to add properties.
How do you check that Loogika is installed?
Visit the deployed Preact app in another browser tab and open a few routes. Loogika's dashboard shows those pageviews within a few seconds.
If no pageviews arrive, check whether the page source contains script.js?siteId=. Then look at the Network tab of the browser. The file script.js should return status 200, and POST requests should go to /api/track.
What if the numbers look too low?
Ad blockers stop analytics scripts for some visitors, which makes the totals lower than your real traffic. You can load the Loogika script through your own domain to lose fewer visits. The proxy guide lists the steps.