Documentation menuJavaScript frameworks: React Router

JavaScript frameworks

Add Loogika to React Router

In React Router framework mode, every route gets its HTML document from the file app/root.tsx. You add the Loogika tag to the head inside the Layout export of that file. The script then loads on every route, and route changes count as pageviews.

Last updated .

Which React Router setups does this guide cover?

This guide covers framework mode in React Router v7 and later, which is the successor to Remix. An app that still runs on Remix follows the Remix guide.

In some Vite projects, React Router serves purely as a library. Those apps have no app/root.tsx, so follow the React with Vite guide instead.

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

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

How do you add Loogika to app/root.tsx?

Open app/root.tsx and place the tag inside the head of the Layout component, after <Meta /> and <Links />. Restart the dev server after you save the file.

import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router";

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
        <script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}

The Layout export wraps the app, HydrateFallback and ErrorBoundary. The tag therefore stays on the page while a route loads and when a route shows an error.

A root.tsx file without a Layout export works too. In that case the head of the default App component takes the tag.

Does Loogika track React Router route changes?

Yes. Loogika records a pageview when a visitor follows a Link or when your code calls useNavigate(). You do not add a router hook for it.

Route tracking depends on the SPA Navigation setting in Site Settings, which is on by default. The script waits 500 milliseconds after a route change before it sends the pageview, so a redirect inside the app is not counted twice.

How do you track custom events in React Router?

Any route or component can call window.loogika.event() with an event name and optional properties. Route modules also render on the server, where window does not exist, so check for it first.

export default function Pricing() {
  const handleClick = () => {
    if (typeof window !== "undefined" && window.loogika) {
      window.loogika.event("plan_selected", { plan: "pro" });
    }
  };

  return <button onClick={handleClick}>Choose Pro</button>;
}

The example is a route module at app/routes/pricing.tsx. The custom events guide explains event names, properties and their limits.

What if your app uses a Content Security Policy with a nonce?

The Loogika tag needs the nonce attribute you already pass to <Scripts /> and <ScrollRestoration />, with the same value. The browser blocks a script tag without the nonce.

A Content Security Policy (CSP) is a response header that lists the sources a page may load from. The policy must also allow the Loogika domain as a script source and as a connection target:

script-src 'self' https://app.loogika.com;
connect-src 'self' https://app.loogika.com;

How do you check that Loogika is installed?

Visit your app in another browser tab and open a few routes. The pageviews show up in the Loogika dashboard within a few seconds.

If nothing arrives, check whether the page source contains script.js?siteId=. The browser Network tab should show script.js with status 200 and requests to /api/track. The script troubleshooting page covers ad blockers and wrong site IDs.