Stripe knows who paid and how much. Your web analytics knows which post, search or ad brought each visitor. Neither knows both, so "which marketing actually makes money?" goes unanswered. The fix is a join: give every visitor an ID, carry it into checkout, and match each charge back to the visits before it.
Here is how to build that join yourself, and the open-source tool I built when I got tired of maintaining it.
The idea in one line
visitor ID (cookie) -> Stripe checkout (client_reference_id or metadata) -> charge -> join back to the visit Step 1: remember where the visitor came from
On the first page view, save the visitor's source in a first-party cookie. Keep the first touch, so a later direct visit does not overwrite it.
// on every page (function () { if (document.cookie.includes("src=")) return; const p = new URLSearchParams(location.search); const src = { vid: crypto.randomUUID(), source: p.get("utm_source") || document.referrer || "direct", campaign: p.get("utm_campaign") || "", landing: location.pathname, }; document.cookie = "src=" + encodeURIComponent(JSON.stringify(src)) + "; path=/; max-age=" + 60 * 60 * 24 * 90 + "; samesite=lax"; })(); Step 2: carry it into Stripe Checkout
When your server creates the Checkout Session, copy the cookie into the session. Put it on the PaymentIntent or subscription too, because that is what later charges and renewals point to.
const src = JSON.parse(decodeURIComponent(req.cookies.src || "%7B%7D")); const session = await stripe.checkout.sessions.create({ mode: "subscription", line_items: [{ price: "price_...", quantity: 1 }], client_reference_id: src.vid, metadata: { source: src.source, campaign: src.campaign, landing: src.landing }, subscription_data: { metadata: { source: src.source, campaign: src.campaign } }, success_url: "https://yourdomain.com/thanks", }); Stripe metadata allows 50 keys and 500 characters per value, so store only what you will group by.
Step 3: group revenue by source
Export payments monthly, or read them from the API, and sum by metadata.source. A few dozen payments fit in a spreadsheet.
Where the hand-built version breaks
- Payment Links carry no metadata unless you add tags to every link.
- Renewals need the first payment's source copied forward.
- Refunds and disputes need a second export to subtract.
- Direct fills up with links opened from apps, email and Discord, which drop the referrer. Tag every link you post with UTMs.
- Payments from before you added the cookie have no source at all.
The open-source version
I build Virling, which does this join for you. Its script sets the visitor ID, adds client_reference_id to Stripe Payment Links on its own, and Virling reads charges, refunds and renewals with a restricted read-only key (it refuses secret keys). The dashboard shows revenue and revenue per visitor by channel, referrer, campaign and landing page, and you can open any payment to see the visits before it.
It is one Rust binary with embedded storage, free to self-host under AGPL-3.0, or hosted. Source: github.com/virling/virling.
For a Checkout Session your server creates, the only change is one line:
client_reference_id: req.cookies.virling_vid, The full Stripe walk-through, including test mode, is in the original post: Which analytics tool connects website visits to Stripe revenue?


