From 747a1d5f954dbea15b56b9f492625d041eedd783 Mon Sep 17 00:00:00 2001 From: "victormanuel.caverogracia" <> Date: Sat, 18 Jul 2026 23:42:20 +0200 Subject: [PATCH 1/3] fix(billing): don't let a non-current subscription clobber the live row subscriptions keeps one row per user, keyed on user_id in _upsert. An event for a *different* sub of the same user (e.g. canceling a dangling past_due annual while a monthly trial is live) would overwrite the good row. Add a cross-subscription guard: another sub only takes over the row when the stored one is no longer live. --- cloud/billing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cloud/billing.py b/cloud/billing.py index 5e30c2a..130a09d 100644 --- a/cloud/billing.py +++ b/cloud/billing.py @@ -328,6 +328,18 @@ async def _upsert_subscription(sub_obj: dict, event_created: datetime): # Order guard: ignore events older than what we've already applied. if row and row.last_event_at and event_created < row.last_event_at: return + # Cross-subscription guard: the table keeps one row per user, so an + # event for a *different* subscription of the same user must not clobber + # the row that represents their live plan. This is exactly what happens + # when a user has a dangling sub (e.g. a past_due annual that gets + # canceled) alongside a live one (a trialing monthly): the canceled + # sub's update would otherwise overwrite the good row. Only let another + # subscription take over when the stored one is no longer live. + if row and row.stripe_subscription_id != sub_obj["id"]: + stored_live = row.status in ("active", "trialing", "past_due", "unpaid") + incoming_live = sub_obj.get("status") in ("active", "trialing", "past_due") + if stored_live and not incoming_live: + return start, end = _sub_period(sub_obj) now_canceling = bool(sub_obj.get("cancel_at_period_end")) # Detect the moment the user hits "cancel" (False -> True). From 99e113130d1acc9ee77c77dadd2667b65a0476b9 Mon Sep 17 00:00:00 2001 From: "victormanuel.caverogracia" <> Date: Sun, 19 Jul 2026 14:18:11 +0200 Subject: [PATCH 2/3] analytics: track funnel goals (Signup, CheckoutStarted, Subscribed) via Plausible Add a small no-op-safe track() helper and emit three custom events so we can see where users drop off between landing and paid: Signup on auth callback, CheckoutStarted before the Stripe redirect, Subscribed on plan activation. Plausible here is Community Edition (no revenue goals), so the plan price rides along as a plain value_usd prop instead of a revenue field. Self-hosted builds without the analytics script are unaffected (track() no-ops). --- dashboard/index.html | 1 + dashboard/src/components/AccountPage.jsx | 21 ++++++++++++++++++++ dashboard/src/components/PricingSection.jsx | 19 +++++++++++++----- dashboard/src/contexts/AuthContext.jsx | 4 ++++ dashboard/src/lib/analytics.js | 22 +++++++++++++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 dashboard/src/lib/analytics.js diff --git a/dashboard/index.html b/dashboard/index.html index 6c7e04a..a62660a 100644 --- a/dashboard/index.html +++ b/dashboard/index.html @@ -191,6 +191,7 @@ + diff --git a/dashboard/src/components/AccountPage.jsx b/dashboard/src/components/AccountPage.jsx index 55bf477..7cbd6fb 100644 --- a/dashboard/src/components/AccountPage.jsx +++ b/dashboard/src/components/AccountPage.jsx @@ -2,6 +2,7 @@ import React, { useState, useEffect, useCallback } from 'react'; import { Loader2, CreditCard, LogOut, Plus } from 'lucide-react'; import { useAuth } from '../contexts/AuthContext'; import { apiJson } from '../lib/api'; +import { track } from '../lib/analytics'; const fmt1 = (n) => Math.round((n || 0) * 10) / 10; @@ -24,6 +25,26 @@ export default function AccountPage() { if ((data && data.plan) || tries > 15) { clearInterval(t); setActivating(false); + // Fire the Subscribed conversion goal. A pending-checkout stash (set in + // PricingSection) carries the plan price, so we can attach real revenue; + // top-ups don't set it, so they never count as a subscription. + if (data && data.plan) { + let pending = null; + try { pending = JSON.parse(localStorage.getItem('os_pending_checkout') || 'null'); } catch (_) { /* ignore */ } + if (pending) { + // This Plausible is Community Edition, which has no revenue goals — + // so the price rides along as plain props (value_usd / plan) that CE + // can break the goal down by. The exact MRR still lives in Stripe. + track('Subscribed', { + props: { + plan: pending.plan, + interval: pending.interval, + value_usd: Math.round((pending.amount || 0) / 100), + }, + }); + try { localStorage.removeItem('os_pending_checkout'); } catch (_) { /* ignore */ } + } + } // First time a plan activates, take the user straight to connect their // socials. Guard with a flag so top-up checkouts don't re-trigger it. if (data && data.plan && !localStorage.getItem('os_socials_prompted')) { diff --git a/dashboard/src/components/PricingSection.jsx b/dashboard/src/components/PricingSection.jsx index 0f138f4..249be1a 100644 --- a/dashboard/src/components/PricingSection.jsx +++ b/dashboard/src/components/PricingSection.jsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { Check, Loader2, Zap, Github, Server } from 'lucide-react'; import { useAuth } from '../contexts/AuthContext'; import { apiJson } from '../lib/api'; +import { track } from '../lib/analytics'; import SegmentedControl from './ui/SegmentedControl'; const PLAN_ORDER = ['starter', 'creator', 'pro']; @@ -30,14 +31,22 @@ export default function PricingSection({ onRequireLogin }) { .finally(() => setLoading(false)); }, []); - const checkout = async (price_id) => { - if (!isSignedIn) { onRequireLogin?.(price_id); return; } - setBusyPrice(price_id); + const checkout = async (entry) => { + if (!isSignedIn) { onRequireLogin?.(entry.price_id); return; } + setBusyPrice(entry.price_id); + track('CheckoutStarted', { props: { plan: entry.plan, interval: entry.interval } }); + // Stash the price so we can attach real revenue to the Subscribed goal when + // the user returns from Stripe (see AccountPage's checkout=success handler). + try { + localStorage.setItem('os_pending_checkout', JSON.stringify({ + plan: entry.plan, interval: entry.interval, amount: entry.amount, currency: entry.currency, + })); + } catch (_) { /* ignore storage errors */ } try { const { url } = await apiJson('/api/billing/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ price_id }), + body: JSON.stringify({ price_id: entry.price_id }), }); window.location.href = url; } catch (e) { @@ -94,7 +103,7 @@ export default function PricingSection({ onRequireLogin }) { {plan === 'pro' &&
  • Priority processing queue
  • }