Remix Stripe UTM tracking: tie each payment to its source
By Devendra Variya, co-founder of Virling. Published 2026-11-22, updated 2026-11-27.
In Remix and React Router, a Stripe Checkout Session belongs in a route action: it runs on the server, it gets the form post as a standard Request with the browser's cookies on it, and it can answer with a redirect straight to Stripe. Take virling_vid from request.headers, pass it as client_reference_id, and return redirect(session.url, 303).
The buyer pays on Stripe's own domain, where the campaign that sent them is unknown; the visitor ID on the session lets Virling tie the payment to that visit and its UTM tags. The same flow for every provider is described in link checkout to visits. On Paddle, see Remix Paddle UTM tracking.
Remix v2 or React Router v7
Remix v2 continued as React Router v7: its framework mode carries Remix routes, loaders and actions under the React Router name. The examples below use React Router v7. On Remix v2 the code is the same apart from three changes:
redirectcomes from@remix-run/node(or the package of your runtime) andFormfrom@remix-run/react, where v7 imports both fromreact-router.- The action is typed with
ActionFunctionArgsinstead of the generatedRoute.ActionArgsfrom./+types/pricing. - The dev server is
remix vite:devinstead ofreact-router dev.
If you are moving a Remix v2 app to v7 anyway, the checkout code needs no change beyond those imports, and routes keep their file names when app/routes.ts uses flatRoutes() from @react-router/fs-routes.
Step 1: the tag in app/root.tsx
Add it to the <head> that the root route renders, which in a v7 app is the exported Layout. Use your own server's address in src if you self-host:
// app/root.tsx
export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<Meta />
<Links />
<script defer src="https://app.virling.com/v.js"
data-site="yourdomain.com"></script>
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}The server renders the tag into the first page of a visit, and the router never replaces the root, so the script runs once and counts each <Link> navigation through pushState.
Step 2: parse the Cookie header
An action gets no cookie map, only request. createCookie is no help here: it parses values it encoded itself, and virling_vid is a plain string the script wrote. A small helper reads it from the raw header instead:
// app/virling.server.ts
export function visitorId(request: Request) {
return request.headers.get("Cookie")
?.match(/(?:^|;\s*)virling_vid=([^;]+)/)?.[1];
}The .server suffix keeps the file out of the browser bundle. The helper returns undefined when the cookie is absent, which Stripe takes as no value.
Step 3: an action that redirects to the Checkout URL
// app/routes/pricing.tsx
import { Form, redirect } from "react-router";
import Stripe from "stripe";
import type { Route } from "./+types/pricing";
import { visitorId } from "~/virling.server";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function action({ request }: Route.ActionArgs) {
const session = await stripe.checkout.sessions.create({
client_reference_id: visitorId(request),
line_items: [{ price: "price_...", quantity: 1 }],
mode: "subscription",
success_url: "https://yourdomain.com/thanks",
});
return redirect(session.url!, 303);
}
export default function Pricing() {
return (
<Form method="post">
<button type="submit">Buy</button>
</Form>
);
}<Form> submits with fetch, and when the action answers with a redirect to another origin, the router leaves the app with a full page load, so the buyer lands on checkout.stripe.com. With JavaScript off, the form posts normally and the browser follows the 303 itself. Keep this in the action and never in a loader: loaders also run for <Link prefetch> and for crawlers, and each run would create a session.
If client_reference_id already carries your own user or cart ID, send the visitor ID as metadata: { virling_vid: id } instead, leaving metadata out when the helper returns nothing. Virling checks both; the order is in the Stripe docs, and per-channel reports are on the Stripe integration page.
SPA mode, with no server
With ssr: false in react-router.config.ts the build is static files, nothing runs on a server, and the build rejects a route that exports action. You have two ways out:
- Payment Links. Link to
buy.stripe.comwith a normal<a href>. The script addsclient_reference_idto the link on load and on click, and no checkout code runs anywhere. - A
clientActionand a backend of your own. Read the ID in the browser and send it to the service that creates the session.
// app/routes/pricing.tsx, with ssr: false
declare global {
interface Window { virling?: { vid?: () => string | null } }
}
export async function clientAction() {
const res = await fetch("/api/checkout", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ vid: window.virling?.vid?.() ?? null }),
});
const { url } = await res.json();
window.location.assign(url);
return null;
}The backend passes vid to Stripe exactly as the action in step 3 does. vid() returns null in cookieless mode and before consent, so check for it there.
Test it in React Router
Follow the test payment in check that it works, with these details:
- Visits to
localhostare not kept. Run the app on a subdomain such asdev.yourdomain.com(a hosts file entry is enough), and add that name toserver.allowedHostsinvite.config.ts. - Put a
sk_test_key inSTRIPE_SECRET_KEY. - After the tagged visit, click through to
/pricingwith a<Link>before pressing Buy, so the test covers a client-side navigation and the<Form>redirect.
To see what the action sent, log session.client_reference_id and session.metadata before the redirect. For other stacks, see Next.js, Astro, Express and Cloudflare Workers.
Common mistakes in Remix and React Router
An Unattributed payment that shows no Visitor under Details reached Stripe without the ID.
| What you see | Cause | Fix |
|---|---|---|
createCookie("virling_vid").parse() returns an empty object | It expects a value it encoded itself, and virling_vid is a plain string | Use the helper from step 2 (sessions and cookies) |
The server action never runs | The route also exports a clientAction, which takes priority and runs in the browser | Remove it, or call serverAction() in it (route module) |
The build fails because of the action | ssr: false leaves no server at run time | Turn ssr on, or use the SPA mode setup above (pre-rendering) |
The console shows a CORS error for checkout.stripe.com | The form is sent with your own fetch, which follows the redirect to Stripe itself | Use <Form> or useFetcher, so the router handles the redirect (calling actions) |
| Sessions in Stripe that nobody asked for | The session is created in a loader | Create it in the action (Link prefetch) |
| 405 Method Not Allowed when the form is on an index route | A plain <form> posts to the parent route, which has no action | Use <Form>, or add ?index to the form's action (index query param) |
| 400 Bad Request on submit behind a proxy, in React Router 7.12 and later | The origin of the form is not the origin of request.url, and the router rejects submissions from other origins | Forward the original host and protocol, or list your host in allowedActionOrigins (react-router.config.ts) |
| The dev server blocks requests from the test host | Vite answers only hosts it knows | Add the host to server.allowedHosts in vite.config.ts (Vite server options) |
Causes that have nothing to do with the router, such as blocked scripts or a returning Stripe customer, are listed in Unattributed revenue.
Frequently asked questions
How do I pass UTM parameters to a Stripe Checkout Session in Remix?
Send the visitor ID, not the tags. In the route action that creates the session, read virling_vid from the request's Cookie header and pass it as client_reference_id. Virling already has the UTM tags of every visit under that ID.
How do I read the virling_vid cookie in a Remix or React Router action?
Call request.headers.get("Cookie") and take the virling_vid value from the string, for example with a regular expression. createCookie does not work for it, because it only parses values it encoded itself.
Can a Remix action redirect to Stripe Checkout?
Yes. Return redirect(session.url, 303) from the action. A <Form> submission sees a redirect to another origin and does a full page load to Stripe, and without JavaScript the browser follows the 303 itself.
Does the checkout code change when I move from Remix v2 to React Router v7?
Only the imports and types. redirect and Form come from react-router, and the action is typed with Route.ActionArgs from ./+types instead of ActionFunctionArgs. The cookie parsing and the Stripe call stay the same.
What if my React Router app runs without a server?
With ssr: false there is no action to read the cookie. Use Stripe Payment Links, which the script fills in the browser, or read the ID with window.virling?.vid?.() in a clientAction and send it to the backend that creates the Checkout Session.
See which campaigns bring Stripe revenue
Virling is open source under AGPL. Self-host it free, or use the hosted version with a 14-day free trial. It connects to Stripe, Paddle, Shopify, WooCommerce, Polar, LemonSqueezy, Dodo, Creem and RevenueCat.