Skip to content
measure

Adding analytics to Remix apps in 2026

Add privacy-first analytics to a Remix v2 or React Router v7 app — one script in root.tsx, automatic SPA pageviews, server-side events from actions, and MCP setup.

Guides4 min readBy Measure team

Remix v2 and React Router v7 are now effectively the same thing — React Router v7 is the path forward for Remix apps, with file-based routing, server-side rendering, and the loader/action model that makes Remix feel different from other React frameworks.

That hybrid architecture raises a couple of questions when adding analytics. Does client-side navigation get tracked? And how do you record things that happen in an action, without a page of their own? Measure handles both.

Here’s how to add privacy-first analytics (a couple of KB, no cookies, no cookie banner needed for Measure) to a Remix or React Router v7 app.

Script injection in root.tsx

In Remix and React Router v7, the right place to inject global scripts is root.tsx. Use the links export for a preconnect hint and put the script in the document head:

JSX
// app/root.tsx
import {
  Links,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from "@remix-run/react";
// or: from "react-router" in React Router v7

export function links() {
  return [
    {
      rel: "preconnect",
      href: "https://lets.measure.events",
    },
  ];
}

export default function App() {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
        <script
          defer
          src="https://lets.measure.events/api/script/YOUR_TRACKING_ID"
        />
      </head>
      <body>
        <Outlet />
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

The preconnect link hint tells the browser to establish a connection to the analytics domain early, reducing latency when the deferred script finally loads.

Client-side navigation is tracked automatically

Remix is an SPA after the first load — navigating between routes doesn’t reload the page. You don’t need a useLocation effect for that: the Measure script hooks the browser’s history API, so every pushState navigation and every back/forward popstate is recorded as a pageview, along with time on page.

The script tag above is the entire client-side setup.

Tracking conversions

The script is pageviews only — there’s no JavaScript API for custom events. For user actions, use one of these:

  • A destination route. Redirect to /welcome, /thanks or /checkout/complete after the action. Those routes’ pageviews are your conversion counts.
  • A server-side event, sent from the action itself (below).

Server-side events from actions

This is where Remix’s server/client model helps. Sometimes you want to record events that happen server-side — form submissions, purchases, webhook handlers — without relying on client-side JavaScript.

Create a small server-only helper that posts to the Measure REST API (https://lets.measure.events/api/v1). Events are recorded against a path, so give each one a descriptive virtual path:

TypeScript
// app/utils/measure.server.ts
export async function recordMeasureEvent(path: string): Promise<void> {
  await fetch("https://lets.measure.events/api/v1/events", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.MEASURE_API_KEY}`, // mk_...
    },
    body: JSON.stringify({ site_key: process.env.MEASURE_TRACKING_ID, path }),
  });
}

The .server.ts suffix keeps it (and your API key) out of the client bundle. Call it from your actions when something important happens:

JSX
// app/routes/checkout.tsx
import { ActionFunctionArgs, redirect } from "@remix-run/node";
import { recordMeasureEvent } from "~/utils/measure.server";

export async function action({ request }: ActionFunctionArgs) {
  const formData = await request.formData();
  const plan = formData.get("plan") as string;

  // ... process checkout ...

  // Recorded server-side — happens even if client JS fails
  await recordMeasureEvent(`/events/checkout-complete/${plan}`);

  return redirect("/dashboard");
}

Server-side tracking is especially useful for:

  • Purchase confirmations (don’t lose the event if the user closes the tab)
  • Form submissions processed on the server
  • API endpoint usage
  • Webhook handlers

The event body also accepts referrer, user_agent, agent and utm_* fields, and /api/v1/events/batch takes {"events": [...]} for bulk sends.

SSR considerations

Remix renders on the server first. The tracking script uses defer and only runs in the browser, and since there’s no client-side API to call, there’s nothing that can accidentally run during SSR.

MCP server setup for AI-powered queries

Once your analytics are collecting data, connect the Measure MCP server to your AI tools. Add it to your Cursor or Claude Code config with an API key (they start with mk_):

JSON
{
  "mcpServers": {
    "measure": {
      "url": "https://lets.measure.events/mcp",
      "headers": {
        "Authorization": "Bearer mk_your_api_key"
      }
    }
  }
}

With this configured, you can ask your AI assistant questions directly in your editor:

“What Remix routes are getting the most traffic?” “How many checkout-complete events came in this week?” “What’s the top referrer for my app?” “Which AI agents are crawling my docs?”

Behind the scenes the assistant calls tools like get_site_analytics, get_breakdown and get_agents. For developers building Remix apps with AI-assisted workflows in 2026, this makes your analytics data part of your development context instead of a separate dashboard tab.

Getting started

  1. Sign up at lets.measure.events/sign-up — $29/month covers 40 sites, with a 14-day trial
  2. Create a site and get your tracking ID
  3. Add the script to root.tsx with the <script defer> tag
  4. Add measure.server.ts and call it from the actions you care about
  5. Optional: connect the MCP server for AI-powered queries

Total setup: under 15 minutes including server-side events. Bytes added to your client bundle: 0.

Let your agent read the numbers.

$29 a month covers 40 sites, then $0.50 a site. 14-day trial.