Documentation menuJavaScript frameworks: TanStack Start

JavaScript frameworks

Add Loogika to TanStack Start

In TanStack Start, the root route file src/routes/__root.tsx has a head() option that builds the document head. You add the Loogika script to the scripts array that head() returns. The script then loads on every route of the app.

Last updated .

What do you need before you start?

Find your site ID in Site Settings in Loogika, under the Tracking Script tab. The tab shows the full script tag, and the address in its src attribute is the part you need here:

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

The site ID is a 12-character string. Replace YOUR_SITE_ID with it in the code on this page.

How do you add Loogika to the root route?

In src/routes/__root.tsx, the object returned by head() needs a new scripts entry. The <HeadContent /> component in the root document turns that entry into a script tag inside the head.

import type { ReactNode } from "react";
import { Outlet, createRootRoute, HeadContent, Scripts } from "@tanstack/react-router";

export const Route = createRootRoute({
  head: () => ({
    meta: [
      { charSet: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { title: "My App" },
    ],
    scripts: [
      {
        src: "https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID",
        defer: true,
      },
    ],
  }),
  component: RootComponent,
});

function RootComponent() {
  return (
    <RootDocument>
      <Outlet />
    </RootDocument>
  );
}

function RootDocument({ children }: Readonly<{ children: ReactNode }>) {
  return (
    <html>
      <head>
        <HeadContent />
      </head>
      <body>
        {children}
        <Scripts />
      </body>
    </html>
  );
}

Which scripts option should you use?

Use the scripts key inside head(). TanStack Start also has a route-level scripts option, and the two are easy to mix up.

The route-level scripts option renders its entries in the body through <Scripts />. Loogika works from there too, but the script loads later than it does from the head.

Does Loogika track TanStack Start 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.

How do you track custom events in TanStack Start?

A route component can call window.loogika.event() with an event name and optional properties. By default TanStack Start also renders each route on the server, where window does not exist, so check for it first.

import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/pricing")({
  component: Pricing,
});

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 the route file src/routes/pricing.tsx. The custom events guide explains event names, properties and their limits.

How do you check that Loogika is installed?

Load your app in a fresh browser tab and visit a few routes. The pageviews show up in the Loogika dashboard within a few seconds.

If nothing arrives, look for script.js?siteId= in the page source. 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.