diff --git a/cloud/billing.py b/cloud/billing.py index 99630d2..b683180 100644 --- a/cloud/billing.py +++ b/cloud/billing.py @@ -332,6 +332,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). diff --git a/dashboard/index.html b/dashboard/index.html index 6c7e04a..4e476a6 100644 --- a/dashboard/index.html +++ b/dashboard/index.html @@ -191,7 +191,20 @@ - + + + + +
diff --git a/dashboard/src/components/AccountPage.jsx b/dashboard/src/components/AccountPage.jsx index 09fcdcb..4ec9bd9 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; @@ -21,12 +22,36 @@ export default function AccountPage() { const t = setInterval(async () => { tries += 1; const data = await refreshMe(); - if ((data && data.plan) || tries > 15) { + // 'free' is the default plan for any signed-in account, so it does NOT + // mean the checkout landed — keep polling until the webhook writes the + // paid subscription. + const paidPlan = data?.plan && data.plan !== 'free'; + if (paidPlan || 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 (paidPlan) { + 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')) { + if (paidPlan && !localStorage.getItem('os_socials_prompted')) { localStorage.setItem('os_socials_prompted', '1'); try { const { access_url } = await apiJson('/api/social/connect', { method: 'POST' }); diff --git a/dashboard/src/components/PricingSection.jsx b/dashboard/src/components/PricingSection.jsx index 42efff9..c96774a 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']; @@ -31,14 +32,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) { @@ -118,7 +127,7 @@ export default function PricingSection({ onRequireLogin }) { {plan === 'pro' &&
  • Priority processing queue
  • }