How to pass a visitor ID into Stripe Checkout for revenue attribution
Step-by-step: read the analytics visitor ID on your server, add it to Stripe Checkout Session or PaymentIntent metadata, and verify that payments link to their traffic source.
By Graytower team
TL;DR
To attribute a Stripe payment to a marketing source, put the visitor's analytics ID on the Stripe object your server creates. Read the first-party _gt_id cookie in your checkout endpoint and set it as graytower_visitor_id in Checkout Session or PaymentIntent metadata, plus your app user ID as graytower_user_id. Then make a live payment and confirm it appears as attributed. Payment Links and client-only flows have separate fallbacks.
Key takeaways
- Stripe does not know which website visit preceded a payment. You have to carry an identifier into checkout.
- Set metadata on the server when you create the Checkout Session or PaymentIntent; never trust a visitor ID sent from the client alone.
- Add your immutable app user ID too, so signed-in customers match even when the browser cookie is missing.
- Payment Links and client-only flows can use a success-page helper or a checkout_started call instead.
- Verify with one live payment, then watch matching coverage rather than attributed dollars.
On this page
- Why does Stripe need a visitor ID?
- What you need before you start
- Step 1: Read the visitor ID on your server
- Step 2: Add metadata to the Checkout Session
- Step 3: Or add it to a PaymentIntent
- Step 4: Identify signed-in users
- What if you use Payment Links or client-only checkout?
- Step 5: Verify with a live payment
- Common mistakes
- How to measure success
- FAQ
Why does Stripe need a visitor ID?
Short answer
Stripe records who paid and how much, but not which website visit or campaign came first. Your analytics tool records visits and sources, but not payments. The visitor ID in Stripe metadata is the bridge. When your server creates a Checkout Session or PaymentIntent, copy the analytics visitor ID into its metadata. After the payment syncs, the payment can be matched to the exact visitor and credited to that visitor's source.
Without that bridge, an analytics tool has two options: leave the payment unattributed, or guess from timing and email similarity. Guessing makes the channel chart look complete and quietly wrong.
What you need before you start
- The Graytower script installed on your marketing site and app, on the same registered domain so they share the
_gt_idcookie (install guide). - Stripe connected to your Website (connect Stripe).
- A server endpoint that creates the Checkout Session or PaymentIntent. If checkout is created entirely in the browser, skip to the fallbacks.
Step 1: Read the visitor ID on your server
The tracker stores an anonymous visitor ID in a first-party cookie named _gt_id. Because it is first-party, the cookie is sent with requests to your own domain, including your checkout endpoint.
In a Next.js route handler or server action:
import { cookies } from "next/headers";
const visitorId = (await cookies()).get("_gt_id")?.value;In Express with cookie-parser:
const visitorId = req.cookies?._gt_id;If the cookie is absent, the visitor was blocked, declined consent, or is on a different root domain. Continue without it; do not invent a value.
Step 2: Add metadata to the Checkout Session
Add the metadata to the session your integration already creates. Keep your existing prices, URLs, and options.
const checkout = await stripe.checkout.sessions.create({ mode: "subscription", line_items: [{ price: priceId, quantity: 1 }], success_url: `${origin}/welcome?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${origin}/pricing`, metadata: { ...(visitorId ? { graytower_visitor_id: visitorId } : {}), graytower_user_id: user.id, // immutable app user ID, if signed in },});graytower_visitor_id gives a Verified match. graytower_user_id gives a Strong match when the same ID was sent to identify after sign-in, which covers customers who pay from a different browser or device.
Step 3: Or add it to a PaymentIntent
For custom payment flows using Stripe Elements:
const intent = await stripe.paymentIntents.create({ amount: 4900, currency: "usd", metadata: { ...(visitorId ? { graytower_visitor_id: visitorId } : {}), graytower_user_id: user.id, },});Step 4: Identify signed-in users
Call identify after sign-in and whenever your authenticated app loads, using the same immutable ID you put in graytower_user_id:
window.graytower("identify", { userId: user.id, email: user.email });Call window.graytower("reset") before a different person uses the same browser, typically on logout. See identify users.
What if you use Payment Links or client-only checkout?
Stripe Payment Links. You cannot set per-visitor metadata on a shared link. Instead, redirect to your site after payment with ?gt_session_id={CHECKOUT_SESSION_ID} and load the opt-in Stripe helper on that success page. It reads _gt_id and links only completed Checkout Sessions.
Client-created sessions. If your browser code has the Checkout Session or PaymentIntent ID, send a reserved event once per attempt:
window.graytower("checkout_started", { checkoutSessionId: session.id });This records an Observed link. It is weaker than metadata and does not, by itself, mean a payment succeeded; success still comes from Stripe.
Server-side attribution API. If you can't change checkout creation but your backend sees the payment ID, post the visitor and user ID with a server key. All three options are covered in the payment matching docs.
Step 5: Verify with a live payment
- Open your site in a private window using a tagged link, for example
?utm_source=test&utm_medium=email. - Complete a real, low-value checkout. Test-mode payments are not imported.
- In Stripe, open the payment and confirm
graytower_visitor_idis in the metadata. - In Graytower, confirm the payment appears in revenue and is attributed to test. Refund it afterwards; the refund lowers revenue without changing the source link.
If the payment is present but unattributed, follow the unattributed revenue diagnostic.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Marketing site and app on different root domains | Cookie missing at checkout | Register both roots or use cross-domain links (subdomains) |
| Verifying with a test-mode payment | Payment never appears in revenue | Use a small live payment, then refund it |
| Sending the visitor ID from the browser in the request body | Spoofable value | Read the cookie on the server |
Calling reset right after identify | New anonymous visitor every login | Only reset on logout or account switch |
| Expecting old payments to gain sources | Historical revenue stays unattributed | Metadata improves future payments only |
How to measure success
Watch matching coverage, the share of payments linked to a visitor, rather than attributed dollars. Coverage and match-level counts appear in Website settings. A jump in coverage after this change can make every channel look like it grew, even though total revenue did not. That is the instrument getting better, not the marketing. With the bridge in place, the SaaS revenue attribution reports become trustworthy enough to act on.
Frequently asked questions
How do I connect Stripe payments to website visitors?
Read the visitor ID your analytics tool stores in a first-party cookie, and copy it into the metadata of the Stripe Checkout Session or PaymentIntent your server creates. When the payment syncs, the analytics tool matches the metadata to the recorded visitor.
Should I use client_reference_id for attribution?
Graytower does not use client_reference_id as a match input. Use metadata keys graytower_visitor_id and graytower_user_id, which keep your existing client_reference_id free for your own order or user reference.
Does Stripe metadata copy from a Checkout Session to the PaymentIntent?
Not automatically. Checkout Session metadata stays on the session. Graytower reads the Checkout Session for Checkout flows; for direct PaymentIntent flows, put the metadata on the PaymentIntent itself.
Will test-mode payments show up?
No. Graytower imports live-mode payments only, so use a small real payment and refund it to verify the full path.