How do you know which ad a Stripe checkout came from?
Stripe only knows what you hand it. Save the ad tags in the browser when someone first lands on your site, then add them to the Payment Link URL when they click buy, along with a client_reference_id that names the ad and the visitor. The reference ID comes back in the checkout.session.completed webhook, and the tags come back on your redirect page, so every paid checkout ties back to one ad.
Checkout on agentcocoon.com is a set of Stripe Payment Links. That keeps checkout simple, but on its own a Payment Link knows nothing about the ad that brought the buyer in. The tags on the ad URL are gone the moment the visitor clicks to a second page.
This is the setup we use, the ID format, and what our own click data showed when we checked.
What can Stripe record about where a buyer came from?
Two things, and only if you pass them in the Payment Link URL. The first is UTM codes. Stripe accepts utm_source, utm_medium, utm_campaign, utm_term and utm_content, each up to 150 characters of letters, numbers, dashes or underscores (Stripe Docs). They only come back if the link's confirmation behavior is set to redirect, and then they are added to your redirect URL after payment (Stripe Docs).
The second is client_reference_id, a string of up to 200 characters that Stripe attaches to the Checkout Session and sends in the checkout.session.completed webhook (Stripe Docs). It is also stored on the Checkout Session object itself, so you can look it up later (Stripe API Reference).
| Field | Where it comes back | Best for |
|---|---|---|
| UTM codes | Your redirect URL after payment | Firing ad conversion tags on the thank-you page |
| client_reference_id | Webhook and the Checkout Session | Matching the payment to your own records |
Use both. The UTMs help the browser report the conversion. The reference ID survives even if the buyer closes the tab before the redirect loads.
Why do the ad tags get lost before checkout?
Because buyers rarely pay on the page they land on. Someone clicks an ad, lands on a tagged URL, reads a guide or the pricing section, and then clicks buy from a page with no tags in its address. If your buy button just points at the plain Payment Link, Stripe gets nothing.
The fix is to save the tags when the visitor arrives, then rebuild the Payment Link URL at the moment they click.
How do you set it up?
This is what our site does on every page:
- On landing, read the URL. If it has utm_ tags, save them in the browser's local storage with a timestamp. A new tagged visit replaces the old one.
- Give each browser a random visitor ID and save it too. No name or email.
- Expire old tags. Ours drop after 30 days. Google click IDs (gclid, gbraid, wbraid) are kept 90 days.
- Tag organic visits too. If there are no tags but the visitor came from Google, Bing, ChatGPT, Perplexity or another site, save a source like o-google so organic buyers are not lumped in with "direct."
- When they click buy, build the link. Take the Payment Link, add the saved UTM codes, and add client_reference_id.
- Log the click on your side before leaving. We send the event with sendBeacon so it still records when the browser jumps to Stripe.
- Point the redirect at a page that reads the result. Ours goes to an intake page that receives the Stripe session ID, fires the purchase conversion once per session, and skips test-mode sessions.
What should client_reference_id contain?
Something a person can read and a database can join. Ours is ac_, then the ad code, then the visitor ID, for example ac_t11-1_k3j9x2ab7f1q. Looking at a payment in Stripe, you can see the ad at a glance. Joining on the visitor ID shows every page that person viewed before buying.
Two rules keep it from breaking. Strip anything that is not a letter, number, dash or underscore, and keep it short. Stripe silently drops invalid values while the payment page keeps working (Stripe Docs), so a bad ID never shows up as an error. You just lose the attribution.
What did our click data show?
That a quarter of checkout clicks still had no ad attached. From October 28 to November 6, our event log recorded 80 clicks to Stripe checkout from 64 people:
| Total | No ad tag | Share untagged | |
|---|---|---|---|
| All site visits | 2,346 | 344 | 14.7% |
| Checkout clicks | 80 | 22 | 27.5% |
Checkout clicks were almost twice as likely as visits to have no source. None of those 22 had a referrer either. The likely causes are people coming back later by typing the address, links shared without tags, and browsers clearing storage. Safari deletes a site's script-written storage, including local storage, after seven days of Safari use with no interaction on that site (WebKit). A 30-day window is really 7 days for some Safari visitors who do not come back.
The other 58 clicks carried their ad code all the way to Stripe, which is the point: those can be credited to a specific email, ad or post instead of a channel guess.
How do you check it is working?
Test it before you trust the numbers:
- Open your site with made-up tags, like ?utm_source=test&utm_content=check1.
- Browse to a different page, then click buy.
- Look at the Stripe URL in the address bar. It should contain your UTM codes and client_reference_id.
- Pay in test mode and confirm the reference ID appears in the checkout.session.completed event.
- Confirm the redirect page received the tags and fired the conversion once, not on every reload.
Run the same test in Safari and on a phone. For which numbers to trust once the data is flowing, see Can an AI agent manage Meta ads for a small eCommerce brand?
Common questions
Can I put the customer's email in client_reference_id?
You can, but we would not. A Payment Link with parameters attached can be forwarded, and Stripe advises sharing those links only with intended recipients. A random visitor ID does the same matching job without putting personal data in a URL.
How long should the saved ad tags last?
Long enough to cover how long your buyers take to decide. We keep them 30 days, and a newer tagged visit replaces the old one, so the last ad clicked gets the credit. Safari can clear stored data sooner if the visitor does not come back within 7 days of browsing.
Does this work for subscriptions?
Yes. A Payment Link that sells a subscription still creates a Checkout Session, and the client_reference_id rides on that session the same way it does for a one-time payment.
What about Google Ads click IDs?
Save them the same way. We store gclid, gbraid or wbraid for 90 days and send them with every lead, so a conversion can be matched back to the click later.