Phase 4 part 4: the spend log and CAC
Spend by month and channel (the funnel's channels and general), by hand or from pasted CSV (all or nothing); origami.chat and explee.com campaign spend added hourly as the difference since last seen. CAC per month and over three months, and per channel over 12 months from each new customer's first funnel step; unknown, not $0, without spend. Metrics → Spend and CAC. Schema 21. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -179,6 +179,9 @@ func (s *Server) RunMetrics(ctx context.Context) {
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if c := s.stripe(); c != nil {
|
||||
if _, err := metrics.SyncOutsideSpend(s.DB, time.Now()); err != nil {
|
||||
s.Log.Warn("metrics: outside spend", "err", err)
|
||||
}
|
||||
last := metrics.LastSync(s.DB)
|
||||
if last == nil || time.Since(last.At) >= time.Hour {
|
||||
sctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
@@ -319,3 +322,81 @@ func (s *Server) queueAccount(cust string) error {
|
||||
_, err = crm.Enqueue(s.DB, crm.Action{Key: "stripe-customer:" + cust, Entity: "Account", Fields: fields, Match: match, Origin: "stripe-customer:" + cust})
|
||||
return err
|
||||
}
|
||||
|
||||
// --- Spend and CAC ---
|
||||
|
||||
func (s *Server) metricsSpend(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderSpend(w, r, "")
|
||||
}
|
||||
|
||||
func (s *Server) renderSpend(w http.ResponseWriter, r *http.Request, errMsg string) {
|
||||
model, err := metrics.Build(s.DB, time.Now())
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
names, err := metrics.CustomerNames(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
cac, err := metrics.BuildCAC(s.DB, model, names)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
log, err := metrics.SpendLog(s.DB, 200)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
months := cac.Months
|
||||
if len(months) > 13 {
|
||||
months = months[:13]
|
||||
}
|
||||
s.render(w, r, "metrics-spend", page{Title: "Spend and CAC", Nav: "metrics/spend", Error: errMsg, Data: map[string]any{
|
||||
"CAC": cac, "Months": months, "Log": log, "Channels": metrics.SpendChannels(), "This": time.Now().Format("2006-01"),
|
||||
}})
|
||||
}
|
||||
|
||||
// metricsSpendAdd logs spend, or imports a CSV.
|
||||
func (s *Server) metricsSpendAdd(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
if csvText := strings.TrimSpace(r.PostFormValue("csv")); csvText != "" {
|
||||
n, err := metrics.ImportSpendCSV(s.DB, strings.NewReader(csvText), now)
|
||||
if err != nil {
|
||||
s.renderSpend(w, r, "Nothing imported: "+err.Error())
|
||||
return
|
||||
}
|
||||
s.Log.Info("metrics: spend imported", "rows", n)
|
||||
redirect(w, r, "/metrics/spend", "imported")
|
||||
return
|
||||
}
|
||||
cents, err := metrics.ParseAmount(r.PostFormValue("amount"))
|
||||
if err == nil {
|
||||
err = metrics.AddSpend(s.DB, metrics.Spend{Month: strings.TrimSpace(r.PostFormValue("month")), Channel: r.PostFormValue("channel"),
|
||||
AmountCents: cents, Note: r.PostFormValue("note")}, now)
|
||||
}
|
||||
if err != nil {
|
||||
s.renderSpend(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/metrics/spend", "saved")
|
||||
}
|
||||
|
||||
func (s *Server) metricsSpendDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err := metrics.DeleteSpend(s.DB, id); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/metrics/spend", "deleted")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user