Metrics: a customer whose first invoice is a renewal is an existing one

When the earliest recurring invoice Stripe has for a customer is a renewal
(billing_reason subscription_cycle), they were a customer already, their
first year free or billed elsewhere. That first payment now counts as
reactivation (movement "existing"), not new: it stays out of New, the
cohorts and CAC, and the month's movements still add up. The customers
export gives the month first seen paying as first_paid. A $0 trial signup
before the first payment still makes a customer new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 12:56:34 -07:00
co-authored by Claude Opus 5.5
parent fab349d166
commit 06b17b9eab
4 changed files with 107 additions and 6 deletions
+42
View File
@@ -152,3 +152,45 @@ func TestStartMonthAndSuggestion(t *testing.T) {
t.Errorf("the starting base's cohort: %+v", cos)
}
}
// A customer whose first invoice Stripe shows is a renewal was a customer
// already: their first payment counts as reactivation, not new, and puts
// them in no cohort. One whose first invoice is a signup is new.
func TestRenewalFirstIsExisting(t *testing.T) {
d := openDB(t)
addLines(t, d, monthly("cus_a", 5000, "2026-01", "2026-07")...)
for _, c := range []struct{ cust, invoice, reason string }{
{"cus_old", "in_old", "subscription_cycle"},
{"cus_new", "in_new", "subscription_create"},
} {
if _, err := d.Exec(`INSERT INTO stripe_invoice_lines (invoice, id, customer, status, currency, amount, period_start, period_end,
interval, interval_count, proration, recurring) VALUES (?, ?, ?, 'paid', 'usd', 120000, ?, ?, 'year', 1, 0, 1)`,
c.invoice, "il_"+c.cust, c.cust, day("2026-03-02"), day("2027-03-02")); err != nil {
t.Fatal(err)
}
if _, err := d.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, 'paid', 'usd', ?)`,
c.invoice, c.cust, `{"billing_reason":"`+c.reason+`"}`); err != nil {
t.Fatal(err)
}
}
m, err := Build(d, time.Date(2026, 6, 20, 12, 0, 0, 0, time.UTC))
if err != nil {
t.Fatal(err)
}
mar := m.Find("2026-03")
if mar.NewCount != 1 || !near(mar.New, 10000) || !near(mar.Reactivation, 10000) ||
mar.Moves["cus_old"] != MoveExisting || mar.Moves["cus_new"] != MoveNew {
t.Errorf("march: new %d %.0f, reactivation %.0f, moves %v", mar.NewCount, mar.New, mar.Reactivation, mar.Moves)
}
if !near(mar.NetNew(), mar.MRR-m.Find("2026-02").MRR) {
t.Errorf("movements don't add up: %.0f vs %.0f", mar.NetNew(), mar.MRR-m.Find("2026-02").MRR)
}
if _, ok := m.First["cus_old"]; ok || m.Existing["cus_old"] != "2026-03" || m.First["cus_new"] != "2026-03" {
t.Errorf("first %v, existing %v", m.First, m.Existing)
}
for _, co := range m.Cohorts(12, 3) {
if co.Month == "2026-03" && co.Customers != 1 {
t.Errorf("March cohort has %d customers, want only cus_new", co.Customers)
}
}
}