Skip to content

Open source CapCut alternative.

TypeScript

feat(site): revenue card names the person behind every event

David Leadbd1f1bf03b
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
index 146e9f5c..e97b882f 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
@@ -20,6 +20,17 @@
"2026-09-02"
],
"events": [
+ {
+ "amountMicros": "5000000",
+ "customerId": "cus_n",
+ "day": "2026-09-01",
+ "detail": "topup",
+ "email": "[email protected]",
+ "ended": false,
+ "endsAt": null,
+ "kind": "paid",
+ "objectId": "pi_2"
+ },
{
"amountMicros": null,
"customerId": "cus_p",
@@ -41,6 +52,17 @@
"endsAt": null,
"kind": "declined",
"objectId": "pi_3"
+ },
+ {
+ "amountMicros": "20000000",
+ "customerId": "cus_p",
+ "day": "2026-08-30",
+ "detail": "pro",
+ "email": "[email protected]",
+ "ended": false,
+ "endsAt": null,
+ "kind": "paid",
+ "objectId": "pi_1"
}
],
"funded": 2,
diff --git a/site/src/app/su/analytics/product/page.tsx b/site/src/app/su/analytics/product/page.tsx
index d7600850..49fe61c6 100644
--- a/site/src/app/su/analytics/product/page.tsx
+++ b/site/src/app/su/analytics/product/page.tsx
@@ -406,6 +406,10 @@ const netOf = (point: Pick<RevenuePoint, "pro" | "topups" | "other" | "refunds">
type BillingEvent = AnalyticsBilling["events"][number];
+// What a paid charge bought, from the event's detail.
+const PAID_LABELS: Record<string, string> = { other: "Other", pro: "Pro", topup: "Top-up" };
+const paidLabel = (event: BillingEvent) => PAID_LABELS[event.detail ?? ""] ?? "Paid";
+
/** Where a canceled subscription stands now: still running until its end
* date, or already stopped. */
function cancelStatus(event: BillingEvent): string {
@@ -433,18 +437,18 @@ function CancelMarker({ viewBox }: { viewBox?: { x: number; y: number; height: n
);
}
-/** The revenue tooltip: net on the date row, the day's money rows, and the
- * cancel requests made that day: who, and where the subscription stands
- * now. A day with no money shows only
- * its cancels, and a day with nothing at all shows no tooltip. */
+/** The revenue tooltip: net on the date row, the day's money rows, then the
+ * people behind them: who paid what, whose card was declined and why, and
+ * who asked to cancel, with where each subscription stands now. A day with
+ * nothing at all shows no tooltip. */
function RevenueTooltipContent({
events,
...props
}: React.ComponentProps<typeof ChartTooltipContent> & { events: AnalyticsBilling["events"] }) {
const point = props.payload?.[0]?.payload as RevenuePoint | undefined;
- const cancels = point ? events.filter((e) => e.kind === "canceled" && e.day === point.day) : [];
+ const people = point ? events.filter((e) => e.day === point.day) : [];
const hasMoney = props.payload?.some((item) => item.value !== 0) ?? false;
- if (!hasMoney && cancels.length === 0) return null;
+ if (!hasMoney && people.length === 0) return null;
return (
<div className="grid gap-1.5">
{hasMoney && (
@@ -462,15 +466,27 @@ function RevenueTooltipContent({
)}
/>
)}
- {cancels.length > 0 && (
+ {people.length > 0 && (
<div className="max-w-80 space-y-1.5 rounded-lg border border-border/50 bg-background px-2.5 py-1.5 text-xs shadow-xl">
- {cancels.map((event) => (
- <div key={event.objectId ?? event.email ?? event.day}>
+ {people.map((event) => (
+ <div key={`${event.kind}-${event.objectId ?? event.email ?? event.day}`}>
<p className="flex items-center justify-between gap-3">
- <span className="font-medium text-destructive">canceled</span>
- <span className="font-medium text-foreground">{cancelStatus(event)}</span>
+ {event.kind === "canceled" ? (
+ <>
+ <span className="font-medium text-destructive">canceled</span>
+ <span className="font-medium text-foreground">{cancelStatus(event)}</span>
+ </>
+ ) : (
+ <>
+ <span className={cn("font-medium", event.kind === "declined" ? "text-muted-foreground" : "text-foreground")}>
+ {event.kind === "declined" ? "declined" : paidLabel(event)}
+ </span>
+ <span className="font-medium text-foreground tabular-nums">{formatMicros(event.amountMicros ?? "0")}</span>
+ </>
+ )}
</p>
<p className="text-muted-foreground">{event.email ?? "unknown customer"}</p>
+ {event.kind === "declined" && event.detail && <p className="text-muted-foreground">{event.detail}</p>}
</div>
))}
</div>
@@ -485,6 +501,7 @@ const stripeLinks = (base: string) => ({
declined: `${base}/payments?status%5B%5D=failed`,
paid: `${base}/payments?status%5B%5D=successful`,
payment: (id: string) => `${base}/payments/${id}`,
+ payments: `${base}/payments`,
refunded: `${base}/payments?status%5B%5D=refunded`,
subscribers: `${base}/subscriptions?status=active`,
subscription: (id: string) => `${base}/subscriptions/${id}`,
@@ -517,7 +534,7 @@ function StripeLink({
// The most recent declines and cancel requests shown under the chart; the
// rest sit behind the Stripe link.
-const BILLING_EVENTS_SHOWN = 6;
+const BILLING_EVENTS_SHOWN = 10;
// The dashboard blocks in their default order. The ids are the saved-layout
// contract: renaming one drops that block back to its default position.
@@ -1345,7 +1362,9 @@ export default function SuAnalyticsPage() {
: links.canceled
: event.objectId
? links.payment(event.objectId)
- : links.declined;
+ : event.kind === "paid"
+ ? links.paid
+ : links.declined;
return (
<li
key={`${event.kind}-${event.objectId ?? event.day}`}
@@ -1355,7 +1374,9 @@ export default function SuAnalyticsPage() {
<StripeLink href={href} className="text-foreground">
{event.kind === "canceled"
? "canceled"
- : `${formatMicros(event.amountMicros ?? "0")} declined`}
+ : event.kind === "paid"
+ ? `${formatMicros(event.amountMicros ?? "0")} ${paidLabel(event)}`
+ : `${formatMicros(event.amountMicros ?? "0")} declined`}
</StripeLink>
{event.kind === "canceled" && (
<span className="shrink-0 text-foreground">{cancelStatus(event)}</span>
@@ -1371,7 +1392,7 @@ export default function SuAnalyticsPage() {
})}
{billing.events.length > BILLING_EVENTS_SHOWN && (
<li>
- <StripeLink href={links.declined}>
+ <StripeLink href={links.payments}>
+{billing.events.length - BILLING_EVENTS_SHOWN} more in Stripe
</StripeLink>
</li>
diff --git a/site/src/lib/analytics/billing.test.ts b/site/src/lib/analytics/billing.test.ts
index 2c420228..b2795dde 100644
--- a/site/src/lib/analytics/billing.test.ts
+++ b/site/src/lib/analytics/billing.test.ts
@@ -74,8 +74,10 @@ describe("buildBilling", () => {
expect(billing.funded).toBe(2);
expect(billing.fundedMicros).toBe("30000000");
expect(billing.events.map((e) => [e.day, e.kind, e.objectId, e.detail])).toEqual([
+ ["2026-09-03", "paid", "pi_2", "topup"],
["2026-09-02", "canceled", "sub_1", "switched_service · Export broke."],
["2026-09-02", "declined", "pi_4", "Your card has insufficient funds."],
+ ["2026-09-01", "paid", "pi_1", "pro"],
]);
});
diff --git a/site/src/lib/analytics/pipeline.ts b/site/src/lib/analytics/pipeline.ts
index 2b159e9f..5553fbd1 100644
--- a/site/src/lib/analytics/pipeline.ts
+++ b/site/src/lib/analytics/pipeline.ts
@@ -425,7 +425,7 @@ function fundedMicrosByUser(stripe: AnalyticsStripeSnapshot | undefined): Map<st
}
// The Stripe record folded into what the dashboard shows: subscription state
-// now, per-day money, and the window's declines and cancel requests with the
+// now, per-day money, and the window's charges and cancel requests with the
// ids that link into Stripe. Abandoned checkouts never subscribed, so they
// count nowhere in churn; past_due and paused sit outside both buckets until
// Stripe resolves them.
@@ -503,6 +503,17 @@ export function buildBilling(
if (charge.kind === "pro") revenue[i].proMicros += amount;
else if (charge.kind === "topup") revenue[i].topupMicros += amount;
else revenue[i].otherMicros += amount;
+ events.push({
+ amountMicros: charge.amountMicros,
+ customerId: charge.customerId,
+ day: charge.day,
+ detail: charge.kind,
+ email: charge.email,
+ ended: false,
+ endsAt: null,
+ kind: "paid",
+ objectId: charge.paymentIntentId,
+ });
} else if (i !== undefined) {
declinedMicros += amount;
declinedCustomers.add(charge.customerId ?? charge.id);
diff --git a/site/src/lib/analytics/schema.ts b/site/src/lib/analytics/schema.ts
index 83b387aa..51c8d192 100644
--- a/site/src/lib/analytics/schema.ts
+++ b/site/src/lib/analytics/schema.ts
@@ -240,18 +240,20 @@ export const analyticsRollupSchema = z.object({
cancels: z.number(),
}),
),
- // The window's declined charges and cancel requests, newest first, each
- // with the ids that link into Stripe.
+ // The window's paid and declined charges and cancel requests, newest
+ // first, each with the ids that link into Stripe. Paid charges are
+ // absent from rollups written before they shipped.
events: z.array(
z.object({
day: analyticsDaySchema,
- kind: z.enum(["declined", "canceled"]),
+ kind: z.enum(["paid", "declined", "canceled"]),
email: z.string().nullable(),
customerId: z.string().nullable(),
- // Payment intent for a decline, subscription for a cancel.
+ // Payment intent for a charge, subscription for a cancel.
objectId: z.string().nullable(),
amountMicros: z.string().nullable(),
- // The decline reason, or the cancel feedback and comment.
+ // What a paid charge bought (pro, topup, other), the decline
+ // reason, or the cancel feedback and comment.
detail: z.string().nullable(),
// For a cancel: when the subscription ends (or ended), and whether
// it has already stopped.