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).
This commit is contained in:
victormanuel.caverogracia 2026-07-19 14:18:11 +02:00
parent 747a1d5f95
commit 99e113130d
5 changed files with 62 additions and 5 deletions

View file

@ -191,6 +191,7 @@
<!-- Analytics -->
<script defer data-domain="openshorts.app" src="https://analytics.fotoexamen.com/js/script.js"></script>
<script>window.plausible = window.plausible || function () { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
</head>
<body>

View file

@ -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')) {

View file

@ -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' && <li className="flex items-start gap-2"><Zap size={16} className="text-brass shrink-0 mt-0.5" /> <span>Priority processing queue</span></li>}
</ul>
<button
onClick={() => checkout(entry.price_id)}
onClick={() => checkout(entry)}
disabled={busyPrice === entry.price_id}
className={`w-full ${highlight ? 'btn-primary' : 'btn-ghost'}`}
>

View file

@ -7,6 +7,7 @@
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
import { getApiUrl } from '../config';
import { apiFetch, apiJson, getToken, setToken, clearToken } from '../lib/api';
import { track } from '../lib/analytics';
const AuthContext = createContext(null);
export const useAuth = () => useContext(AuthContext);
@ -65,6 +66,9 @@ export function AuthProvider({ children }) {
}
}
const signedInMe = await refreshMe();
// This handler only runs on the auth redirect, so a resolved user here is
// a fresh sign-in / sign-up — the top of the conversion funnel.
if (signedInMe?.user) track('Signup', { props: { method: kind === 'verify' ? 'magic_link' : 'google' } });
// New sign-ups (and anyone without an active plan/trial) go straight to
// pricing so they can start their free trial; entitled users land in the app.
if (signedInMe?.user && !signedInMe?.entitled) destination = '#/pricing';

View file

@ -0,0 +1,22 @@
// Lightweight Plausible custom-event helper.
//
// The hosted openshorts.app build loads the Plausible script (see index.html),
// which exposes `window.plausible`. Self-hosted builds, ad-blockers or offline
// dev simply won't have it — every call here is a safe no-op in that case, so
// analytics can never break the app or leak into the open-source experience.
//
// This targets a Plausible Community Edition instance, which has NO revenue
// goals (that's a Cloud-only feature) — so prices ride along as ordinary props
// (e.g. value_usd) that CE can break a goal down by. Goals to create (by name):
// - Signup (custom event) — account created / signed in
// - CheckoutStarted (custom event) — redirected to Stripe Checkout
// - Subscribed (custom event) — plan activated after checkout
export function track(event, options) {
try {
if (typeof window !== 'undefined' && typeof window.plausible === 'function') {
window.plausible(event, options);
}
} catch (_) {
/* analytics must never throw into the app */
}
}