# Stripe revenue attribution with CrispStats

Updated 2026-11-24

To see which traffic sources pay, connect Stripe in CrispStats with a restricted key, then put the visitor id in the Checkout Session metadata as crispstats_visitor_id. One-time payments, subscriptions, renewals and refunds then appear on the Revenue screen, each credited to the visit that led to it.

## Steps

### 1. Open the Stripe panel

The CrispStats script must already run on your site. Open the site's **Settings**, select **Integrations**, choose **Connect a payment provider** and pick Stripe. You need permission to manage the site's settings.

### 2. Create a restricted key in Stripe

Select **Create a restricted key in Stripe**. It opens Stripe's restricted key form with six permissions filled in, and only **Webhook Endpoints: Write** can change anything; the rest are read-only and serve the product names and the history import. Leave every other resource at **None**, create the key and copy it. It starts with `rk_live_` (`rk_test_` in a sandbox).

### 3. Paste the key and connect

Paste the key and select **Connect Stripe**. CrispStats creates a webhook endpoint for this site with the five events below, saves its signing secret and the key encrypted, and starts importing the payments made before today. Each payment is read back from Stripe's API with your key before it counts as verified revenue. A full secret key (`sk_live_`) is refused.

```text
checkout.session.completed
checkout.session.async_payment_succeeded
invoice.paid
payment_intent.succeeded
charge.refunded
```

### 4. Pass the visitor id at checkout

Call `crispstats.id()` in the browser before you send the visitor to checkout, and put the id in the Checkout Session's `metadata`. For subscriptions, put it in `subscription_data.metadata` too, so renewals are attributed. Get the id in a click handler, not on your server: the id belongs to the browser that asks.

`checkout.js`

```js
// In the browser, before you send the visitor to checkout
const visitorId = await crispstats.id();

// On your server, when you create the Checkout Session
const session = await stripe.checkout.sessions.create({
  // ...your line items and URLs
  metadata: { crispstats_visitor_id: visitorId },
  // Subscriptions only: renewals carry the subscription's metadata
  subscription_data: { metadata: { crispstats_visitor_id: visitorId } },
});
```

## Check that it works

The Stripe row in **Settings**, **Integrations** shows **Connected**, whether it is live, and when it last updated. Make a purchase from a page with the script: it appears under **Payments** on the Revenue screen with the source of that visit. If the key stops working or loses a permission, the row says so and names the way out.

## What you get

- Revenue by source, channel, campaign and landing page, credited to the first touch known for the visitor.
- New subscriptions and renewals kept apart, one-time payments on their own, and MRR on the Revenue screen.
- Refunds recorded as negative payments; Stripe reports the total refunded so far, and only the difference is stored.
- Product names from each Checkout Session's line items, and past payments imported when you connect with a key.
- Customer email stored only hashed and masked; renewals without a visitor id take the attribution of the customer's earliest attributed payment.

## Notes for Stripe

- On a Payment Link there is no metadata per visitor: add the id to the link's URL as `client_reference_id`. Values that are not 32 hexadecimal characters are ignored, so your own order ids there never become visitor ids.
- Payments taken with a Payment Intent and no Checkout are recorded only when the Payment Intent carries the visitor id in its own metadata.
- Stripe sends each payment within seconds. CrispStats also asks Stripe for recent events every 15 minutes, so a payment the webhook missed still arrives, and a payment seen both ways is stored once.
- A test key connects in test mode once you choose **Connect in test mode**. Test payments show with a **Test** tag on the Revenue screen and are kept out of revenue, MRR and every total.
- A payment without a visitor id is still recorded and counted in revenue, MRR and every total. It is left out of the source, campaign and channel breakdowns rather than filed under Direct.
- Connections made earlier with a webhook you added by hand keep working, but cannot import history or verify payments. Connect again with a key to add both.
- If RevenueCat bills through the same Stripe account and is also connected, connect only one of the two, or each payment counts twice.
- CrispStats does not calculate [churn](/glossary/churn-rate) or [expansion](/glossary/expansion-revenue). For those, and for invoices and disputes, Stripe stays the source of truth.

Full reference: [Stripe in the docs](https://docs.crispstats.com/revenue/stripe)

## Frequently asked questions

### Why does the key need write access to webhook endpoints?

So CrispStats can create the webhook endpoint for you, and delete it when you disconnect. Every other permission on the key is read-only, and a full secret key is refused.

### Does it attribute subscription renewals?

Yes, when the visitor id is also in subscription_data.metadata. Each renewal invoice then carries the id and is credited to the same first touch as the first payment.

### What happens to payments without a visitor id?

They are recorded and counted in revenue, MRR and every total, and left out of the source and campaign breakdowns. A renewal or refund without an id takes the attribution of the same customer's earliest attributed payment.

### Can I import revenue from before I connected?

Yes. Connecting with a restricted key starts the import at once. It reads past Checkout payments, subscription invoices and refunds, from your first payment, on every plan. Most of them have no visit to tie to, so they count in revenue and MRR but stay out of the source breakdowns.

### Is revenue attribution on every plan?

Yes, history import included. Every feature is on every plan and in the 14-day free trial, and payments are kept on every plan, while other traffic data is kept for the plan's period, 24 months on Starter.

## Related

- [Revenue attribution: which traffic pays](/features/revenue-attribution)
- [First touch vs last touch attribution](/blog/revenue-attribution-first-touch-vs-last-touch)
- [Revenue per visitor calculator](/tools/revenue-per-visitor-calculator)
- [Web analytics for SaaS products](/for/saas)
