Stripe revenue analytics for Astro
This guide adds ProductLense to a Astro site and connects Stripe, so every Stripe payment is attributed to the channel, campaign and landing page that first brought the customer. It takes four steps and about ten minutes.
1. Add ProductLense to Astro
The script goes in src/layouts/Layout.astro. Replace SITE_ID with the id from Settings → Sites.
---
const { title } = Astro.props;
---
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{title}</title>
<script is:inline defer data-site="SITE_ID" src="https://api.productlense.app/a.js"></script>
</head>
<body>
<slot />
</body>
</html>2. Identify signed-in users
---
const { userId } = Astro.props;
---
<script is:inline define:vars={{ userId }}>
window.productlense = window.productlense || function () { (window.productlense.q = window.productlense.q || []).push(arguments); };
if (userId) window.productlense('identify', userId);
</script>3. Connect Stripe
Setup steps
- In Stripe, open Developers → API keys and create a restricted key with Read on Customers, Subscriptions, Charges, Refunds and Balance transactions.
- In ProductLense, open Settings → Billing connections → Stripe and paste the key. ProductLense makes one read call to check it.
- In Stripe → Developers → Webhooks, add a webhook endpoint with the URL ProductLense shows after you save the connection (pattern https://api.productlense.app/api/webhooks/{provider}/{connection_id}).
- Select the events listed below, save, and paste the signing secret into ProductLense.
- ProductLense backfills the last 12 months, then keeps up through webhooks and a nightly reconcile.
Credentials and permissions
| Field | Secret | Where to find it |
|---|---|---|
| Secret key | Yes, encrypted at rest | A restricted key (rk_live_...) with read access to customers, subscriptions, charges, refunds and balance transactions. A full secret key also works; a restricted key is safer. |
| Webhook signing secret | Yes, encrypted at rest | The whsec_... secret of the webhook endpoint you point at ProductLense. |
Webhook
URL pattern, shown with your connection id after you save:
https://api.productlense.app/api/webhooks/stripe/{connection_id}Events: charge.succeeded, charge.refunded, charge.dispute.created, customer.created, customer.updated, customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, invoice.paid
Verification: Stripe-Signature header: timestamp plus HMAC-SHA256, rejected after 5 minutes.
What ProductLense reads
- Customers
- Customers, including email and metadata such as pl_user_id.
- Subscriptions
- Subscriptions with plan, interval and status.
- Payments
- Charges and paid invoices.
- Refunds
- Refunds, plus disputes recorded as chargebacks.
- Fees
- The fee on each balance transaction, and the dispute fee on chargebacks.
What ProductLense never touches
- Never creates, changes, refunds or cancels anything: read-only API calls and incoming webhooks only.
- Never moves money, issues payouts, or reads bank account details.
- Never sees full card numbers; providers do not expose them.
- Never sends your customer data to the AI model: Ask and the daily insight see aggregates only.
Match payments to your users
Set metadata.pl_user_id on Stripe customers to the same id you pass to identify for the strongest match. Without it, ProductLense matches by customer email.
const customer = await stripe.customers.create({
email: user.email,
metadata: { pl_user_id: user.id },
});Stripe on Astro: questions
- Where does the ProductLense script go in Astro?
- In src/layouts/Layout.astro. Add the script tag with is:inline to the <head> of your base layout. The script is about 2.3 KB, loads with defer, sets no cookies, and records route changes in single-page apps through the history API, so no extra router code is needed.
- How do I identify users in Astro?
- Call window.productlense('identify', userId) once the signed-in user is known, as in the src/components/ProductLenseIdentify.astro example on this page. Use the same id you store on Stripe as pl_user_id, so Stripe payments match by id rather than by email.
- Does ProductLense work with Stripe?
- Yes. ProductLense connects to Stripe with secret key, webhook signing secret, backfills 12 months of history, and stays current through signed webhooks plus a nightly reconcile. One connection is included on Starter; Growth and Scale allow every provider at once on the same site.
- What data does ProductLense read from Stripe?
- Customers, subscriptions, payments, refunds and fees, read-only. Payments: charges and paid invoices. Refunds: refunds, plus disputes recorded as chargebacks. Fees: the fee on each balance transaction, and the dispute fee on chargebacks.
- Can ProductLense change anything in my Stripe account?
- No. ProductLense only makes read calls and receives webhooks. It never creates, changes, refunds or cancels anything, never moves money, and never sees card numbers. Credentials are encrypted at rest, and the AI features only ever see aggregated numbers, never your customers’ data.
Free up to 10k events a month. No card.
Add the script, connect Stripe or any of 14 other billing providers, and see revenue by channel the same day.