JavaScript frameworks
Add Loogika to a React app with Vite or Create React App
You add Loogika to a client-side React app by pasting one script tag into the head of its HTML entry file. Vite and Create React App each produce an app with a single file of that kind, 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. The tag holds your site ID, a 12-character string, where this example says YOUR_SITE_ID:
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
Where is the HTML entry file in a React project?
The location depends on the build tool.
| Build tool | HTML entry file |
|---|---|
| Vite | index.html, at the top level of the project |
| Create React App | public/index.html |
Start new React projects with Vite, because Create React App is deprecated. The steps on this page still work for an existing Create React App project.
How do you add the Loogika script to React?
Open the HTML entry file and paste the tag just before the closing </head> tag:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My React App</title>
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
The tag also works just before the closing </body> tag, but the browser fetches the script sooner from the head element. Keep the tag in the HTML file and do not add it from inside a component. A tag added by a component can load after the first render, and the first pageview is then lost.
Does Loogika track React Router navigation?
Yes. Loogika records a pageview when React Router or a similar library changes the URL. You do not need a hook or a listener for it.
Route changes are counted while SPA Navigation is on in Site Settings, which is the default for a new site. The Loogika script waits 500 milliseconds after a route change before it records the pageview, so a redirect counts once.
How do you track custom events in React?
Call window.loogika.event() from any component. Check that window.loogika exists before the call. A blocked script, or one that has not finished loading, would otherwise throw an error in the click handler.
export default function LearnMoreButton() {
const handleClick = () => {
if (window.loogika) {
window.loogika.event("button_click", { category: "CTA", label: "Learn More" });
}
};
return <button onClick={handleClick}>Learn More</button>;
}
A data attribute replaces the handler for a plain click. Add data-loogika-event="button_click" to the button in your JSX, and Loogika sends the event on each click. The event tracking page explains properties and limits.
How do you check that Loogika is installed?
Visit the deployed React app in another browser tab and open a few routes. The pageviews show up in the Loogika dashboard within a few seconds.
If no data arrives, check whether the page source contains script.js?siteId=. In the Network tab of the browser, script.js should return status 200 and POST requests should go to /api/track.
Why are pageviews missing after a route change?
Missing pageviews after navigation are rare with React Router, so test the default behaviour first. Check that SPA Navigation is on in Site Settings, under the Tracking tab. If route changes still go uncounted, send the pageview yourself with window.loogika.pageview() every time the route changes.
Low numbers on all pages usually come from ad blockers. The proxy guide shows how your own domain can serve the script.