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:
+54
-4
@@ -18,6 +18,7 @@ type line struct {
|
||||
intervalCount int
|
||||
proration bool
|
||||
product string
|
||||
invoice string
|
||||
}
|
||||
|
||||
const avgMonthSeconds = 365.25 / 12 * 24 * 3600
|
||||
@@ -49,7 +50,7 @@ func (l line) monthly() float64 {
|
||||
// loadLines reads the recurring lines of paid invoices (not open, void or
|
||||
// uncollectible) in the main currency.
|
||||
func loadLines(d *db.DB, currency string) ([]line, error) {
|
||||
rows, err := d.Query(`SELECT customer, amount, period_start, period_end, interval, interval_count, proration, product
|
||||
rows, err := d.Query(`SELECT customer, amount, period_start, period_end, interval, interval_count, proration, product, invoice
|
||||
FROM stripe_invoice_lines WHERE recurring = 1 AND status = 'paid' AND currency = ? AND customer != ''
|
||||
AND period_end > period_start ORDER BY invoice, id`, currency) // a fixed order: float sums come out the same everywhere
|
||||
if err != nil {
|
||||
@@ -59,7 +60,7 @@ func loadLines(d *db.DB, currency string) ([]line, error) {
|
||||
var out []line
|
||||
for rows.Next() {
|
||||
var l line
|
||||
if err := rows.Scan(&l.customer, &l.amount, &l.start, &l.end, &l.interval, &l.intervalCount, &l.proration, &l.product); err != nil {
|
||||
if err := rows.Scan(&l.customer, &l.amount, &l.start, &l.end, &l.interval, &l.intervalCount, &l.proration, &l.product, &l.invoice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, l)
|
||||
@@ -83,8 +84,47 @@ const (
|
||||
MoveContraction = "contraction"
|
||||
MoveChurn = "churn"
|
||||
MoveReactivation = "reactivation"
|
||||
// MoveExisting is a customer whose first payment Stripe shows is a
|
||||
// renewal: they were a customer already (their first year free, or
|
||||
// billed elsewhere). Counted as reactivation, not as new.
|
||||
MoveExisting = "existing"
|
||||
)
|
||||
|
||||
// renewalFirst are the customers whose earliest recurring line is on a
|
||||
// renewal invoice (billing_reason subscription_cycle).
|
||||
func renewalFirst(d *db.DB, lines []line) (map[string]bool, error) {
|
||||
earliest := map[string]line{}
|
||||
for _, l := range lines {
|
||||
if e, ok := earliest[l.customer]; !ok || l.start < e.start {
|
||||
earliest[l.customer] = l
|
||||
}
|
||||
}
|
||||
rows, err := d.Query(`SELECT id, coalesce(json_extract(data, '$.billing_reason'), '') FROM stripe_objects WHERE kind = 'invoice' AND status = 'paid'`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reason := map[string]string{}
|
||||
for rows.Next() {
|
||||
var id, r string
|
||||
if err := rows.Scan(&id, &r); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
reason[id] = r
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[string]bool{}
|
||||
for c, l := range earliest {
|
||||
if reason[l.invoice] == "subscription_cycle" {
|
||||
out[c] = true
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Month is one month's MRR and how it moved since the month before. Amounts
|
||||
// are in cents.
|
||||
type Month struct {
|
||||
@@ -123,7 +163,8 @@ func (m Month) NetNew() float64 {
|
||||
type Model struct {
|
||||
Currency string
|
||||
Months []Month // oldest first, from Start
|
||||
First map[string]string // customer → the month they first paid (from Start)
|
||||
First map[string]string // customer → the month they first paid (from Start): their cohort
|
||||
Existing map[string]string // customer → the month first seen paying, for those already customers then (a renewal): no cohort
|
||||
Start string // the first month counted ("" for all)
|
||||
Suggest *Suggestion // a start month worth considering, when none is set
|
||||
}
|
||||
@@ -177,7 +218,7 @@ func mrrAt(lines []line, t int64) map[string]float64 {
|
||||
// (the current month measured now, the others at their last second).
|
||||
func Build(d *db.DB, now time.Time) (*Model, error) {
|
||||
cur := MainCurrency(d)
|
||||
m := &Model{Currency: cur, First: map[string]string{}, Start: StartMonth(d)}
|
||||
m := &Model{Currency: cur, First: map[string]string{}, Existing: map[string]string{}, Start: StartMonth(d)}
|
||||
if cur == "" {
|
||||
return m, nil
|
||||
}
|
||||
@@ -188,6 +229,10 @@ func Build(d *db.DB, now time.Time) (*Model, error) {
|
||||
if len(lines) == 0 {
|
||||
return m, nil
|
||||
}
|
||||
renewal, err := renewalFirst(d, lines)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
first := lines[0].start
|
||||
for _, l := range lines {
|
||||
first = min(first, l.start)
|
||||
@@ -217,6 +262,11 @@ func Build(d *db.DB, now time.Time) (*Model, error) {
|
||||
case mo.Month == startMonth && m.Start != "":
|
||||
// Already paying when the count starts: the base, not new.
|
||||
m.First[c] = mo.Month
|
||||
case p == 0 && !ever[c] && renewal[c]:
|
||||
// Already a customer before Stripe shows them paying.
|
||||
mo.Reactivation += v
|
||||
mo.Moves[c] = MoveExisting
|
||||
m.Existing[c] = mo.Month
|
||||
case p == 0 && !ever[c]:
|
||||
mo.New += v
|
||||
mo.NewCount++
|
||||
|
||||
Reference in New Issue
Block a user