Skip to content

Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.

feat(autopilot): ROAS = Stripe + RevenueCat (App-Store IAP revenue) (#340)

Aurora Capital08ba4275eb65
diff --git a/claude-ops/bin/ops-marketing-autopilot b/claude-ops/bin/ops-marketing-autopilot
index aa0e8bc0..d687abe2 100755
--- a/claude-ops/bin/ops-marketing-autopilot
+++ b/claude-ops/bin/ops-marketing-autopilot
@@ -1311,6 +1311,52 @@ gather_stripe_revenue() {
report "- stripe: \$${tot} attributed revenue 7d (UTM-tagged charges) → ${out##*/}"
}
+# gather_revenuecat_revenue <project>
+# RevenueCat is the App-Store/Play IAP revenue source (Stripe only sees web charges).
+# For mobile apps (e.g. my-project) the real ROAS denominator lives here. Pulls the
+# v2 metrics overview (28d realized revenue) and normalizes to a 7d estimate
+# (/4) so it sums cleanly with the Stripe 7d window. Only runs for projects with
+# a `.revenuecat` config block; degrades gracefully to 0 (never hard-fails).
+gather_revenuecat_revenue() {
+ local proj="$1"
+ local out="${STATE_DIR}/${proj}-revenuecat.json"
+ # No revenuecat block → no-op (web-only projects stay Stripe-only, zero regression).
+ local has_block
+ has_block="$(jq -r --arg p "$proj" '.marketing.projects[$p].revenuecat // empty | if . == null then "" else "1" end' "$PREFS" 2>/dev/null)"
+ # Drop stale JSON when RevenueCat is not configured — otherwise surface_perf_signals
+ # would still sum a leftover file and inflate ground-truth ROAS.
+ [ -z "$has_block" ] && { rm -f "$out"; return 0; }
+ local api_key project_id
+ api_key="$(resolve_cred "$(jq -r --arg p "$proj" '.marketing.projects[$p].revenuecat.api_key // .marketing.projects[$p].revenuecat.secret_key // empty' "$PREFS" 2>/dev/null)")"
+ project_id="$(resolve_cred "$(jq -r --arg p "$proj" '.marketing.projects[$p].revenuecat.project_id // empty' "$PREFS" 2>/dev/null)")"
+ if [ -z "$api_key" ] || [ -z "$project_id" ]; then
+ report "- revenuecat: api_key/project_id not configured — skipped (App-Store revenue not counted in ROAS)"
+ printf '%s' '{"revenuecat_revenue_7d":0,"revenuecat_revenue_28d":0,"_note":"not_configured"}' > "$out"
+ return 0
+ fi
+ if [ "$DRY_RUN" = "1" ]; then
+ report "- [DRY] would query RevenueCat v2 metrics overview (project=${project_id}) — 28d revenue → 7d est"
+ printf '%s' '{"revenuecat_revenue_7d":0,"revenuecat_revenue_28d":0,"_dry_run":true}' > "$out"
+ return 0
+ fi
+ local resp rev28
+ resp="$(curl -sS --max-time 15 -H "Authorization: Bearer ${api_key}" \
+ "https://api.revenuecat.com/v2/projects/${project_id}/metrics/overview" 2>/dev/null || echo '{}')"
+ # Overview returns a metrics array; the realized-revenue metric id is "revenue"
+ # (trailing-28-days, USD). Match liberally in case the id label shifts.
+ rev28="$(printf '%s' "$resp" | jq -r '
+ (.metrics // []) | map(select((.id // "") | test("^revenue$|revenue_28|last_28.*revenue"; "i"))) | (.[0].value // 0)
+ ' 2>/dev/null || echo 0)"
+ [ -z "$rev28" ] && rev28=0
+ case "$rev28" in ''|*[!0-9.]*) rev28=0;; esac
+ local rev7
+ rev7="$(awk -v r="${rev28:-0}" 'BEGIN{ printf "%.2f", (r+0)/4.0 }')"
+ jq -nc --arg r7 "$rev7" --arg r28 "$rev28" \
+ '{revenuecat_revenue_7d:($r7|tonumber),revenuecat_revenue_28d:($r28|tonumber),_window:"28d_realized/4_est"}' \
+ > "$out" 2>/dev/null || printf '%s' '{"revenuecat_revenue_7d":0,"revenuecat_revenue_28d":0}' > "$out"
+ report "- revenuecat: \$${rev7} revenue 7d-est (\$${rev28} realized 28d ÷4) → ${out##*/}"
+}
+
# stripe_revenue_for_ad <project> <ad_id>
# Returns total stripe-revenue for charges where metadata.ad_id matches.
# Empty / 0 when stripe data is absent or the ad has no charges.
@@ -1330,16 +1376,27 @@ surface_perf_signals() {
local kpi_ledger="${OPS_DATA_DIR}/autopilot_state/${proj}/kpi.jsonl"
local stripe_f="${STATE_DIR}/${proj}-stripe.json"
- # Stripe ground-truth ROAS slice
+ # Ground-truth ROAS slice. Denominator = Stripe (web) + RevenueCat (App-Store/Play IAP)
+ # revenue, both attributed to the app. RevenueCat is summed only when that project
+ # has a revenuecat state file (i.e. a .revenuecat config block) — web-only projects
+ # stay Stripe-only with no behaviour change.
+ local revcat_f="${STATE_DIR}/${proj}-revenuecat.json"
if [ -f "$stripe_f" ] && [ -f "$kpi_ledger" ]; then
- local meta_spend_7d stripe_rev_7d gt_roas
+ local meta_spend_7d stripe_rev_7d revcat_rev_7d combined_rev_7d gt_roas
meta_spend_7d="$(tail -n 2000 "$kpi_ledger" 2>/dev/null | jq -sr '[.[].spend // 0] | add // 0' 2>/dev/null || echo 0)"
stripe_rev_7d="$(jq -r '.total_revenue_7d // 0' "$stripe_f" 2>/dev/null || echo 0)"
- gt_roas="$(awk -v r="${stripe_rev_7d:-0}" -v s="${meta_spend_7d:-0}" \
+ revcat_rev_7d="$( [ -f "$revcat_f" ] && jq -r '.revenuecat_revenue_7d // 0' "$revcat_f" 2>/dev/null || echo 0 )"
+ [ -z "$revcat_rev_7d" ] && revcat_rev_7d=0
+ combined_rev_7d="$(awk -v a="${stripe_rev_7d:-0}" -v b="${revcat_rev_7d:-0}" 'BEGIN{ printf "%.2f", (a+0)+(b+0) }')"
+ gt_roas="$(awk -v r="${combined_rev_7d:-0}" -v s="${meta_spend_7d:-0}" \
'BEGIN{ if (s+0>0) printf "%.2f", r/s; else print 0 }')"
report ""
- report "### Ground-truth ROAS (Stripe-attributed)"
- report "- meta_spend_7d=\$${meta_spend_7d} stripe_revenue_7d=\$${stripe_rev_7d} ROAS=${gt_roas}"
+ report "### Ground-truth ROAS (Stripe + RevenueCat attributed)"
+ if awk "BEGIN{exit !(${revcat_rev_7d:-0} > 0)}" 2>/dev/null; then
+ report "- meta_spend_7d=\$${meta_spend_7d} revenue_7d=\$${combined_rev_7d} (stripe \$${stripe_rev_7d} + revenuecat \$${revcat_rev_7d}) ROAS=${gt_roas}"
+ else
+ report "- meta_spend_7d=\$${meta_spend_7d} revenue_7d=\$${combined_rev_7d} (stripe \$${stripe_rev_7d}; revenuecat \$${revcat_rev_7d}) ROAS=${gt_roas}"
+ fi
fi
# GSC rescue + hook signals
@@ -2378,6 +2435,7 @@ for proj in "${PROJECTS[@]}"; do
gather_gsc_signal "$proj"
gather_klaviyo_metrics "$proj"
gather_stripe_revenue "$proj"
+ gather_revenuecat_revenue "$proj"
surface_perf_signals "$proj"
apply_calibration_and_bandit "$proj"
diff --git a/claude-ops/tests/test-revenuecat-roas.sh b/claude-ops/tests/test-revenuecat-roas.sh
new file mode 100755
index 00000000..b98160b0
--- /dev/null
+++ b/claude-ops/tests/test-revenuecat-roas.sh
@@ -0,0 +1,58 @@
+#!/usr/bin/env bash
+# test-revenuecat-roas.sh — asserts the autopilot's ground-truth ROAS denominator
+# combines Stripe (web) + RevenueCat (App-Store/Play IAP) revenue.
+#
+# Hermetic: no network, no creds. Validates the combination arithmetic + the
+# graceful-skip contract (projects without a .revenuecat block stay Stripe-only).
+set -euo pipefail
+
+pass=0; fail=0
+ok() { echo " PASS: $1"; pass=$((pass+1)); }
+err() { echo " FAIL: $1 — $2"; fail=$((fail+1)); }
+
+BIN="$(cd "$(dirname "$0")/.." && pwd)/bin/ops-marketing-autopilot"
+echo "Testing combined-revenue ROAS in $BIN"
+echo ""
+
+[ -f "$BIN" ] && ok "autopilot bin exists" || { err "bin missing" "$BIN"; exit 1; }
+
+# 1. The combined-revenue ROAS code path is present.
+grep -q 'Ground-truth ROAS (Stripe + RevenueCat attributed)' "$BIN" \
+ && ok "ROAS report line names both sources" \
+ || err "ROAS report line" "missing Stripe + RevenueCat label"
+
+grep -q 'gather_revenuecat_revenue' "$BIN" \
+ && ok "gather_revenuecat_revenue helper wired" \
+ || err "helper" "gather_revenuecat_revenue not found"
+
+# 2. Combination arithmetic: combined = stripe + revenuecat (mirrors the awk in-bin).
+combine() { awk -v a="$1" -v b="$2" 'BEGIN{ printf "%.2f", (a+0)+(b+0) }'; }
+roas() { awk -v r="$1" -v s="$2" 'BEGIN{ if (s+0>0) printf "%.2f", r/s; else print 0 }'; }
+
+c="$(combine 0 480.00)" # Stripe $0 (mobile app) + RevenueCat $480
+[ "$c" = "480.00" ] && ok "combined: \$0 stripe + \$480 revcat = \$480" || err "combine" "got $c"
+
+c2="$(combine 120.50 480.00)"
+[ "$c2" = "600.50" ] && ok "combined: \$120.50 + \$480 = \$600.50" || err "combine2" "got $c2"
+
+# ROAS with combined revenue vs Stripe-only ($0) at $50/day × 7d = $350 spend
+r_stripe_only="$(roas 0 350)"
+r_combined="$(roas 480 350)"
+[ "$r_stripe_only" = "0.00" ] && ok "stripe-only ROAS on a mobile app = 0.00 (the bug)" || err "roas stripe-only" "got $r_stripe_only"
+awk "BEGIN{exit !($r_combined > 1)}" && ok "combined ROAS = ${r_combined} (now meaningful)" || err "roas combined" "got $r_combined (expected >1)"
+
+# 3. Graceful-skip contract: helper no-ops without a .revenuecat block (string check
+# on the guard so web-only projects keep Stripe-only behaviour, zero regression).
+grep -q 'No revenuecat block .* no-op' "$BIN" \
+ && ok "web-only projects skip RevenueCat (no regression)" \
+ || err "guard" "missing no-revenuecat-block no-op guard"
+
+# 4. 28d→7d normalization (÷4) so RevenueCat sums with the Stripe 7d window.
+grep -q '28d_realized/4_est' "$BIN" \
+ && ok "RevenueCat 28d revenue normalized to 7d (÷4)" \
+ || err "normalization" "missing 28d→7d est"
+
+echo ""
+echo "---"
+echo "Results: $pass passed, $fail failed"
+[ "$fail" -eq 0 ]