Revenue
Attribute Stripe PaymentIntents
On this page
Building your own payment form with the Payment Element or the PaymentIntents API? Add the visitor ID to the PaymentIntent's metadata when you create it.
First connect Stripe so payments are imported: see Connect Stripe.
Set it up
Send the visitor ID to your server
Include it in the request that creates the payment:
Browser// Read this in the visitor's browser when they start checkout. const visitorId = window.analytics?.getVisitorId() ?? null; // Send visitorId to your own checkout endpoint alongside its usual request data. // Use the authenticated user ID from your backend; do not trust a browser-supplied user ID.Add metadata when creating the PaymentIntent
Server// Your server: create the PaymentIntent for the Payment Element. // visitorId came from the browser; authenticatedUser from your session. const intent = await stripe.paymentIntents.create({ amount: 2900, currency: "usd", automatic_payment_methods: { enabled: true }, metadata: { metricraven_visitor_id: visitorId || "", metricraven_user_id: authenticatedUser.id, metricraven_plan: "pro", }, }); // Return intent.client_secret to the browser as usual.For subscriptions, add it to the subscription
Stripe creates the invoice's PaymentIntent for you, so put the keys on the subscription. Each invoice payment, including renewals, is then attributed.
Server// Subscriptions built with the Payment Element: put the keys on the subscription. const subscription = await stripe.subscriptions.create({ customer: customerId, items: [{ price: "YOUR_RECURRING_STRIPE_PRICE_ID" }], payment_behavior: "default_incomplete", expand: ["latest_invoice.confirmation_secret"], metadata: { metricraven_visitor_id: visitorId || "", metricraven_user_id: authenticatedUser.id, metricraven_plan: "pro_monthly", }, });
Verify it works
After a live payment and the next sync, the payment appears in Revenue and in the buyer's journey under Visitors.
Metadata keys
| Metadata key | What it does |
|---|---|
| metricraven_visitor_id | The value of window.analytics.getVisitorId() in the buyer's browser. Links the payment to that visit and its traffic source. Anything that isn't a MetricRaven visitor ID is ignored. |
| metricraven_user_id | Your own user ID. If you call Identify with the same ID, the payment joins that user's visits even without a visitor ID. |
| metricraven_plan | Optional plan name shown with the payment. Subscription invoices fall back to the price ID. |
| metricraven_renewal | Optional "true" for a repeat payment. Ignored for subscription invoices, where Stripe's billing reason decides. |
Details
- Creating Customers yourself? Put
metricraven_user_id(and the visitor ID, if you have it) in the Customer's metadata too. Every future charge for that customer then carries it. - Updating an existing PaymentIntent before confirmation (for example when the cart changes) keeps its metadata; you only need to set it once.
- Metadata is read when MetricRaven imports the charge, usually within a minute or two of the payment. Set it when you create the object; adding it after the import doesn't change that payment.
- Once one payment from a Stripe customer is linked, that customer's later payments (renewals, upgrades) are linked to the same visitor and user, and their earlier unlinked payments are filled in.
- MetricRaven does not read
client_reference_id, the customer's email, custom checkout fields or UTM parameters on Stripe links. Only the metadata keys above link a payment to a visit. - Only live-mode payments are imported. Test-mode payments never appear in MetricRaven.