Connect to customers

Revenue analytics is in beta

Revenue analytics is currently works best for:

  1. Small to medium-sized companies
  2. Companies with subscription models (mostly SaaS)

If you process more than 20,000 transactions per month, or your revenue comes primarily from one-off payments rather than recurring subscriptions, revenue analytics may feel less useful, slower, or provide less insight than expected.

The connection between your revenue data and persons and groups is automatically done when you're using revenue events (since we know what person/group an event belongs to) but we need your help to map them in case you're using a data warehouse source.

The easy way

Let the wizard handle the metadata configuration for you by running this command in your project directory with your terminal:

Learn more

Step 1: Add metadata when creating new customers

Search your codebase for where you create Stripe customers (e.g. stripe.Customer.create or equivalent) and add the posthog_person_distinct_id metadata field.

customer = stripe.Customer.create(
email=user.email,
metadata={"posthog_person_distinct_id": user.posthog_distinct_id},
)

Step 2: Update existing customers before charges

For customers created before you added the metadata, add a Customer.modify call before your charge or checkout code. This ensures existing customers get tagged the next time they make a payment.

Search your codebase for any of these patterns:

  • PaymentIntent.create — one-off charges
  • Subscription.create — new subscriptions
  • checkout.Session.create — Stripe Checkout
  • Invoice.create — manual invoices

Then add the following before that code:

stripe.Customer.modify(
user.stripe_customer_id,
metadata={"posthog_person_distinct_id": user.posthog_distinct_id},
)

Using Stripe Checkout? If you use checkout.Session.create and let Stripe create customers automatically, you won't have a Customer.create call in your code. Instead, handle this in your checkout.session.completed webhook:

# In your checkout.session.completed webhook handler
session = event.data.object
user = get_user_by_client_reference_id(session.client_reference_id)
stripe.Customer.modify(
session.customer,
metadata={"posthog_person_distinct_id": user.posthog_distinct_id},
)

Tip: Set client_reference_id to your internal user ID when creating Checkout Sessions. This lets you look up the user in your database when the webhook fires.

Once this is connected you'll be able to properly see who your top customers are in the Top customers dashboard.

You'll also get access to the persons_revenue_analytics and groups_revenue_analytics tables in the data warehouse. This is a simple map of person_id/group_key to what their all-time revenue is.

SQL
-- Count the number of persons with revenue greater than 1,000,000
SELECT COUNT(*)
FROM persons_revenue_analytics
WHERE amount > 1000000

Community questions

Was this page useful?

Questions about this page? or post a community question.