Stripe Elements UTM parameters: track the source of each sale
By Devendra Variya, co-founder of Virling. Published 2026-11-24, updated 2026-12-01.
Stripe Elements does not send UTM parameters to Stripe. To see which source brought a payment, your server has to put an ID on the payment that points back to the visit. With Virling, set metadata.virling_vid on the PaymentIntent, using the visitor ID from the virling_vid cookie. For a subscription, set it on the PaymentIntent of the first invoice, not on the subscription. Virling reads it from the charge or the PaymentIntent and credits the payment to the visit, with its UTM tags and referrer.
Why UTM tags do not reach Stripe by themselves
- Your server creates the payment. Your page shows the Payment Element, but the PaymentIntent comes from a call your server makes. Stripe stores what that call contains, and the page URL is not part of it.
- The tags are often gone already. They are on the landing URL. By the time the buyer reaches your payment form, they are usually a few pages in.
- Metadata does not spread on its own. Stripe says metadata does not automatically copy to related objects, then lists the exceptions (Stripe metadata docs). Which object you pick decides whether the charge is matched.
You could copy the tags themselves into metadata, since Stripe allows 50 keys with values of up to 500 characters. One visitor ID is simpler: the visits behind it, with their tags and referrers, live in your analytics.
Which Stripe object carries the visitor ID
- PaymentIntent metadata: matched. Stripe copies it to the charge once, when the charge is created. Virling reads the charge and the PaymentIntent.
- Subscription metadata: not matched. Stripe copies it to the invoice, under
parent.subscription_details.metadata. The list of copies does not include the PaymentIntent or the charge, and Virling does not read the invoice's copy. - Customer metadata: not matched. Virling does not read it for Stripe.
Add the script
Put one tag in the <head> of every page, landing pages included. The app shows the exact tag for your site:
<script defer src="https://app.virling.com/v.js" data-site="yourdomain.com"></script>
The script records the referrer and UTM tags of every visit against a visitor ID in the first-party virling_vid cookie. The cookie is on your domain, so your server receives it. If your API is on another domain, send window.virling?.vid?.() in the request body instead. Both ways are in the checkout docs and in server-side revenue tracking.
One-time payments: the PaymentIntent
Set the metadata where you create the PaymentIntent. This is Express with cookie-parser; the amount is a made-up example:
app.post("/create-intent", async (req, res) => {
const vid = req.cookies.virling_vid;
const intent = await stripe.paymentIntents.create({
amount: 4900,
currency: "usd",
automatic_payment_methods: { enabled: true },
metadata: vid ? { virling_vid: vid } : {},
});
res.json({ clientSecret: intent.client_secret });
});The browser code does not change. This is the flow from Stripe's guide, where the PaymentIntent is created after the buyer submits the form (Stripe guide):
const stripe = Stripe("pk_live_...");
const elements = stripe.elements({ mode: "payment", amount: 4900, currency: "usd" });
elements.create("payment").mount("#payment-element");
document.querySelector("#payment-form").addEventListener("submit", async (event) => {
event.preventDefault();
const { error: submitError } = await elements.submit();
if (submitError) return;
const res = await fetch("/create-intent", { method: "POST" });
const { clientSecret } = await res.json();
const { error } = await stripe.confirmPayment({
elements,
clientSecret,
confirmParams: { return_url: "https://yourdomain.com/thanks" },
});
// Show submitError.message or error.message in your form.
});If you create the PaymentIntent as the page loads instead, make sure the Virling script has run first, or a first-time visitor has no cookie yet. The React Stripe UTM tracking post shows how to wait for it in a single-page app.
Subscriptions: the first invoice's PaymentIntent
With Elements, you create the subscription with payment_behavior: "default_incomplete", and Stripe creates the first invoice and its PaymentIntent (Stripe subscriptions overview). Your code did not create that PaymentIntent, so update its metadata, one of the fields Stripe lets you change on an invoice's PaymentIntent (Stripe invoicing docs), before you send the client secret to the browser:
app.post("/create-subscription", async (req, res) => {
const subscription = await stripe.subscriptions.create({
customer: customerId, // your signed-in user's Stripe customer
items: [{ price: "price_..." }],
payment_behavior: "default_incomplete",
payment_settings: { save_default_payment_method: "on_subscription" },
expand: ["latest_invoice.confirmation_secret", "latest_invoice.payments"],
});
const invoice = subscription.latest_invoice;
const vid = req.cookies.virling_vid;
const paid = invoice.payments.data.find((p) => p.payment.type === "payment_intent");
if (vid && paid) {
await stripe.paymentIntents.update(paid.payment.payment_intent, {
metadata: { virling_vid: vid },
});
}
res.json({ clientSecret: invoice.confirmation_secret.client_secret });
});In the browser, set mode: "subscription" in stripe.elements(), fetch /create-subscription and confirm with stripe.confirmPayment() as above. The order matters: Stripe copies the metadata to the charge when the charge is created, and later updates to the PaymentIntent do not reach it (PaymentIntents docs).
API version 2025-03-31.basil removed payment_intent from the invoice, which is why the code expands payments and confirmation_secret (Stripe changelog). On older versions, expand latest_invoice.payment_intent and update that one.
- Renewals need nothing. They take the channel of the customer's first payment, and so do upgrades.
- Free trials charge nothing up front, so Stripe's guide uses the
pending_setup_intentinstead of a payment, and the code above does not cover it. Virling sees trial starts only from Checkout Sessions. For an Elements trial, send the visitor's email as anidentifyevent at signup; a later charge with the same payer email is then matched (how it works).
Quick reference: which object carries the visitor ID
The payment types above, plus a card saved now and charged later. Virling reads the charges and events lists with its restricted key, so you need no webhook for it; the event column is for your own handler.
| Payment type | Object that carries the visitor ID | Stripe events that carry it | What Virling reads |
|---|---|---|---|
| One-time PaymentIntent | metadata.virling_vid on the PaymentIntent, set on create. Stripe copies it to the charge. | payment_intent.succeeded and charge.succeeded | The charge's metadata, then the PaymentIntent's |
| Subscription, first invoice | metadata.virling_vid on the first invoice's PaymentIntent, updated before the browser confirms. Not the subscription. | payment_intent.succeeded and charge.succeeded. invoice.paid carries only the subscription's copy. | The charge's metadata, then the PaymentIntent's. Renewals take the first payment's channel. |
| SetupIntent, then a charge you create later | The PaymentIntent you create for the charge. Stripe does not copy SetupIntent metadata to later payments, and the buyer's cookie is not there off-session, so save the visitor ID with your user when the card is saved. | setup_intent.succeeded for the SetupIntent's own metadata; payment_intent.succeeded and charge.succeeded for the charge | The later charge and its PaymentIntent. Virling never reads the SetupIntent. |
A worked example: one month of Elements payments
Example data, made up to show the check. An imaginary course site takes payments with the Payment Element: a $67 course, a $29 monthly membership, and a $43 coaching call charged to a saved card after the call. Its first month in Virling:
| Payment type | Payments | Where the visitor ID was | Revenue | In Virling |
|---|---|---|---|---|
| Course, one-time PaymentIntent | 23 | PaymentIntent metadata | $1,541 | Credited to the visits behind them |
| Membership, first invoice | 14 | Subscription metadata only | $406 | Unattributed |
| Coaching call, saved card | 9 | Saved at signup, set on the new PaymentIntent | $387 | Credited to the visits behind them |
| Total | 46 | $2,334 | $1,928 credited |
The 14 memberships are the usual mistake: the ID went on the subscription, which Stripe copies to the invoice and not to the payment. Updating the first invoice's PaymentIntent, as in the subscription code above, credits new memberships from then on; the 14 already paid stay Unattributed. The checkout snippet generator has the PaymentIntent code for the first row.
Connect Stripe
Create a restricted key with Read on Charges, Checkout Sessions, Customers, Events, Invoices, Subscriptions and PaymentIntents, and paste it into Virling. The payment is credited under the attribution model you pick: last non-direct by default, or first touch or last touch. The full setup is on the Stripe integration page and in the Stripe docs.
Test it
- Connect a test key that starts with
rk_test_and use your test publishable and secret keys on the site. Payments imported with a test key stay in your reports, so use a separate site in Virling if you want to keep them out. - In a private window, open your site with
?utm_source=test&utm_medium=test, go to the payment form and pay with the card4242 4242 4242 4242and any future date (Stripe test cards). - In the Stripe Dashboard, open the payment. Its metadata should hold
virling_vid. Do this once for a one-time payment and once for a subscription. - Wait a minute for the next sync and open the payments list at
/app/yourdomain.com/payments. The payment shows thetestsource instead of Unattributed.
On localhost the script sends nothing unless the tag has data-dev.
If you use Elements with Checkout Sessions
Stripe also lets you build the Payment Element on a Checkout Session, with ui_mode set to elements (custom before API version 2026-03-25.dahlia; Stripe changelog). There you do not create a PaymentIntent: set client_reference_id or metadata.virling_vid on the session, as in Stripe Checkout UTM parameters. How the join works, step by step, is in how to track Stripe revenue by traffic source. For a store that takes orders through Stripe without Shopify or WooCommerce, Stripe ecommerce analytics covers repeat orders, refunds and what stays Unattributed.
Frequently asked questions
Does Stripe Elements pass UTM parameters to Stripe?
No. The Payment Element collects payment details, and your server creates the PaymentIntent. Stripe stores what that call contains, and the UTM tags of your page are not part of it unless your code adds them.
Where does metadata.virling_vid go for a PaymentIntent?
In the metadata of stripe.paymentIntents.create(), on your server. Stripe copies PaymentIntent metadata to the charge when the charge is created, and Virling reads it from the charge or the PaymentIntent.
Can I put the visitor ID in subscription metadata?
Not for Virling. Stripe copies subscription metadata to the invoice, and Virling reads the charge, its PaymentIntent and the Checkout Session. Set the ID on the PaymentIntent of the first invoice.
Do subscription renewals need the visitor ID again?
No. A renewal takes the channel of the customer's first payment, matched by the Stripe customer ID. Only the first payment needs the visitor ID, and that payment has to be in Virling.
Can I store UTM tags in PaymentIntent metadata instead?
Yes, Stripe accepts any metadata keys within its limits, so utm_source can be one of them. You then keep the tags of one visit and build the reports yourself, and Virling does not read those keys. A visitor ID points to every recorded visit.
Why is an Elements payment Unattributed?
Usually no visitor ID was on the PaymentIntent: the cookie was missing when your server created it, the API is on another domain, the site runs in cookieless mode, or the ID was set only on the subscription. If the ID is there, the visit may be outside the lookback window.
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.