virling Pricing Self-host Demo Changelog Agencies GitHub Start free trial
blog / 2026-11-22

React Stripe UTM tracking: pass UTM to Stripe Checkout from a SPA

By Devendra Variya, co-founder of Virling. Published 2026-11-22.

To tie a Stripe Checkout payment in a React single-page app to the visit and UTM tags that brought it, add the Virling script to index.html, read the visitor ID in the browser with window.virling?.vid?.(), send it to the backend that creates the Checkout Session, and pass it as client_reference_id. Stripe keeps that one ID, and Virling joins the payment to every visit behind it, with the UTM tags of each. You change one HTML file, one click handler and one line on the server.

This post covers a Vite or Create React App frontend with a small Node backend. For Payment Links, metadata limits and where the values show up in Stripe, see the general guide to Stripe Checkout UTM parameters.

Why UTMs disappear after the first navigation

In a single-page app, a visitor lands on /?utm_source=newsletter&utm_campaign=launch, then clicks through to /pricing. React Router changes the URL with history.pushState and drops the query string, so by the time the visitor clicks Buy, location.search is empty. Reading the tags from the URL at checkout time gets nothing on most purchases.

The Virling script hooks into pushState, replaceState and the back button, and sends a pageview when the path or query string changes. The landing pageview carries the tags and the referrer; later route changes carry no referrer, so they stay in the same session and channel as the landing. Since the visit is already recorded with its tags, checkout only needs the visitor ID. If the tags never reach your landing page at all, a redirect is usually dropping them; see UTM parameters lost after redirect.

Step 1: add the script to index.html

In a Vite app, paste the tag into index.html at the project root, before </head>. Vite leaves an external script without type="module" as it is. In Create React App the file is public/index.html. If you self-host, replace app.virling.com with your Virling server.

<!-- index.html -->
<script defer src="https://app.virling.com/v.js"
  data-site="yourdomain.com"></script>

Load it once here, not from a component, so it runs on every route. The script sets the first-party virling_vid cookie on your domain and records the UTM tags of each visit against it. It stays quiet on localhost; add data-dev to the tag to test with npm run dev.

Step 2: read the visitor ID in the browser

window.virling?.vid?.() returns the visitor ID. The script loads with defer, so a click right after the page loads can come before it; wait for it briefly. It returns null in cookieless mode and nothing when the script is blocked, so the backend must accept a missing value:

// src/BuyButton.jsx
async function visitorId() {
  for (let i = 0; i < 20 && !window.virling?.vid; i++) {
    await new Promise((r) => setTimeout(r, 100));
  }
  return window.virling?.vid?.() || null;
}

export default function BuyButton({ price }) {
  async function buy() {
    const res = await fetch("/api/checkout", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ price, vid: await visitorId() }),
    });
    window.location.href = (await res.json()).url;
  }
  return <button onClick={buy}>Buy</button>;
}

Step 3: create the Checkout Session on the backend

Your backend holds the Stripe secret key, so it creates the session. Take the visitor ID from the body and pass it as client_reference_id, then return the session URL for the browser to open:

// server.js
const express = require("express");
const Stripe = require("stripe");

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const app = express();
app.use(express.json());

app.post("/api/checkout", async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    client_reference_id: req.body.vid || undefined,
    line_items: [{ price: req.body.price, quantity: 1 }],
    mode: "subscription",
    success_url: "https://yourdomain.com/thanks",
  });
  res.json({ url: session.url });
});

app.listen(3001);

When the ID is missing, undefined makes the stripe library leave the field out. Check the price against a list of your own price IDs before you use it. In development, point Vite's server.proxy at port 3001 for /api so the fetch stays on the same origin. If your API is on another domain, allow the frontend origin with CORS.

When the API runs on your own domain or a subdomain of it, the browser also sends the virling_vid cookie, so the server can read it from the Cookie header instead of the body. Sending it in the body works in both cases.

When client_reference_id is taken

If you already use client_reference_id for your own user or cart ID, put the visitor ID in the session's metadata.virling_vid instead. Virling reads either:

  const vid = req.body.vid;
  const session = await stripe.checkout.sessions.create({
    client_reference_id: req.user.id,
    metadata: vid ? { virling_vid: vid } : {},
    line_items: [{ price: req.body.price, quantity: 1 }],
    mode: "subscription",
    success_url: "https://yourdomain.com/thanks",
  });

If you create your own PaymentIntent for Elements instead of a Checkout Session, set the same metadata on stripe.paymentIntents.create(), as in Stripe Elements UTM tracking. To show Checkout inside your app, see Stripe embedded checkout UTM tracking. The full lookup order is in the Stripe docs and on the Stripe integration page. The checkout snippet generator writes the same call for other providers.

Start free trial

Check that it works

Open your app from a URL with UTM tags, click through to another route, and make a test payment. Wait for the next sync, then open the payments list at /app/yourdomain.com/payments. The payment should show a channel instead of Unattributed. Open it to see its journey. If it still shows Unattributed, check in the browser console that window.virling.vid() returns an ID, and that the request to /api/checkout carries it.

Payments without a visitor ID still count toward revenue, as Unattributed. How the join works from visit to payment is in how to track Stripe revenue by traffic source, and the Stripe for SaaS page shows what you get per channel. With a server-rendered framework, see Next.js Stripe UTM tracking and Remix Stripe UTM tracking, where the server reads the cookie directly. With a hosted backend instead of your own server, see Stripe UTM tracking with Firebase and Stripe UTM tracking with Supabase. For the same flow in another front-end framework, see Vue Stripe UTM tracking and Angular Stripe UTM tracking.

Frequently asked questions

How do I pass UTM parameters to Stripe Checkout from a React app?

Read the visitor ID in the browser with window.virling?.vid?.(), send it to the backend that creates the Checkout Session, and pass it as client_reference_id. The Virling script records the UTM tags of every visit against that ID, so the payment is joined to all of them.

Why are my UTM parameters gone when the visitor reaches checkout?

React Router changes the URL with history.pushState and drops the query string on the first navigation. The Virling script records the tags from the landing pageview, so you pass the visitor ID at checkout, not the tags.

Should I use client_reference_id or metadata.virling_vid?

Use client_reference_id unless it already holds your own user or cart ID. In that case put the visitor ID in the session's metadata.virling_vid. Virling reads either.

What happens if the visitor ID is missing?

The session is created without a visitor ID, for example when the script is blocked or runs in cookieless mode. The payment still counts toward revenue, as Unattributed.

Do Stripe Payment Links in a React app need code?

No. Render a normal link to buy.stripe.com on a page that runs the script. The script adds client_reference_id to the link when the page loads and again on click, unless the link already has one, so links rendered after a route change are covered too.

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.

Start free trial See the demo