JavaScript frameworks
Add Loogika to Qwik
You add Loogika to a Qwik City app by placing one script tag in the head element of src/root.tsx. That file renders the whole HTML document, including head and body, so the script is present 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. The 12-character ID of your site goes 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 Qwik?
In src/root.tsx, place the tag in the head element, right after the RouterHead component:
import { component$ } from "@builder.io/qwik";
import { QwikCityProvider, RouterOutlet } from "@builder.io/qwik-city";
import { RouterHead } from "./components/router-head/router-head";
import "./global.css";
export default component$(() => {
return (
<QwikCityProvider>
<head>
<meta charset="utf-8" />
<RouterHead />
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body lang="en">
<RouterOutlet />
</body>
</QwikCityProvider>
);
});
Why does the tag go in root.tsx and not in router-head.tsx?
The tag in src/root.tsx does not depend on any single route. Each route exports a head with items such as the title, meta tags and links. The file that renders that export is src/components/router-head/router-head.tsx. A script tag kept in root.tsx stays on the page whatever a route exports.
Does Loogika track navigation in Qwik City?
Yes. Loogika records a pageview each time a visitor follows a Qwik City Link component to another route. No extra code is needed.
Loogika counts these route changes while SPA Navigation is on in Site Settings, which it is for a new site. The script waits 500 milliseconds after the URL changes before it records the pageview.
How do you track custom events in Qwik?
Call window.loogika.event() from an event handler whose name ends in $, such as onClick$. The browser is the only place where such handlers run, which means window is defined every time. Still check that window.loogika exists, because the script can be blocked or still loading.
import { component$ } from "@builder.io/qwik";
export const SignupButton = component$(() => {
return (
<button
onClick$={() => {
if (window.loogika) {
window.loogika.event("signup_click", { location: "hero" });
}
}}
>
Sign up
</button>
);
});
A button that only needs a click event can use a data attribute in place of the handler. Add data-loogika-event="signup_click" to the button element. The event tracking page covers properties and size limits.
How do you check that Loogika is installed?
Load your live Qwik site in a fresh browser tab and visit a few routes. The pageviews show up in the Loogika dashboard within a few seconds.
If the dashboard stays empty, look for script.js?siteId= in the page source. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track.
Visits can also go missing because an ad blocker stops the script. The proxy guide shows how to deliver the script through a domain you own.