From fb1c3a849fe08522098eb8f8c915f5524f915d6c Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Wed, 29 Jul 2026 08:47:36 -0700 Subject: [PATCH] analytics page fix --- src/lib/analytics.ts | 4 +- src/pages/analytics/AnalyticsPage.tsx | 294 +++++++++++++++++++++++++- src/types/analytics.types.ts | 41 ++++ 3 files changed, 334 insertions(+), 5 deletions(-) diff --git a/src/lib/analytics.ts b/src/lib/analytics.ts index e7eb5e7..2e01e92 100644 --- a/src/lib/analytics.ts +++ b/src/lib/analytics.ts @@ -199,7 +199,9 @@ export function formatPercentRate(rate: number, fractionDigits = 1): string { /** Display label for dashboard breakdown rows (regions, enums, free text). */ export function formatAnalyticsLabel(label: string): string { const text = label?.trim() ?? "" - if (!text || text.toLowerCase() === "unknown") return "Unknown" + if (!text || text.toLowerCase() === "unknown" || text.toUpperCase() === "OTHER") { + return "Other" + } if (text.includes("_")) return text.replace(/_/g, " ") return text } diff --git a/src/pages/analytics/AnalyticsPage.tsx b/src/pages/analytics/AnalyticsPage.tsx index aab1525..17ac022 100644 --- a/src/pages/analytics/AnalyticsPage.tsx +++ b/src/pages/analytics/AnalyticsPage.tsx @@ -7,7 +7,9 @@ import { BarChart, CartesianGrid, Cell, - // Legend, + Legend, + Line, + LineChart, Pie, PieChart, ResponsiveContainer, @@ -128,10 +130,12 @@ function BreakdownList({ title, data, total, + scrollable: _scrollable, }: { title: string data: LabelCount[] total?: number + scrollable?: boolean }) { const computedTotal = total ?? data.reduce((s, d) => s + d.count, 0) const sorted = [...data].sort((a, b) => b.count - a.count) @@ -186,6 +190,68 @@ function BreakdownList({ ) } +function AmountBreakdownList({ + title, + data, +}: { + title: string + data: { label: string; count: number; amount: number }[] +}) { + const totalAmount = data.reduce((s, d) => s + d.amount, 0) + const sorted = [...data].sort((a, b) => b.amount - a.amount) + return ( + + + {title} + + + {sorted.length > 0 ? ( +
+ {sorted.map((item, i) => { + const pct = totalAmount > 0 ? (item.amount / totalAmount) * 100 : 0 + const displayLabel = formatAnalyticsLabel(item.label) + return ( +
+
+
+ + + {displayLabel} + +
+ + + ETB {item.amount.toLocaleString()} + + + ({item.count.toLocaleString()} · {pct.toFixed(0)}%) + + +
+
+
+
+
+ ) + })} +
+ ) : ( +
No data available
+ )} + + + ) +} + function DonutCard({ title, data, @@ -384,6 +450,49 @@ export function AnalyticsPage() { count: d.count, })) + const snapshot = subscriptions.snapshot + const renewal = subscriptions.renewal + const cancelByDate = new Map( + (subscriptions.cancellations_last_30_days ?? []).map((d) => [d.date, d.count]), + ) + const expireByDate = new Map( + (subscriptions.expirations_last_30_days ?? []).map((d) => [d.date, d.count]), + ) + const resubByDate = new Map( + (subscriptions.resubscriptions_last_30_days ?? []).map((d) => [d.date, d.count]), + ) + const churnDates = [ + ...new Set([ + ...(subscriptions.cancellations_last_30_days ?? []).map((d) => d.date), + ...(subscriptions.expirations_last_30_days ?? []).map((d) => d.date), + ...(subscriptions.resubscriptions_last_30_days ?? []).map((d) => d.date), + ]), + ].sort() + const churnSeries = churnDates.map((date) => ({ + date: formatDate(date), + cancellations: cancelByDate.get(date) ?? 0, + expirations: expireByDate.get(date) ?? 0, + resubscriptions: resubByDate.get(date) ?? 0, + })) + + const activeCategoryPie = (subscriptions.active_by_category ?? []).map((s, i) => ({ + name: formatAnalyticsLabel(s.label), + value: s.count, + color: PIE_COLORS[i % PIE_COLORS.length], + })) + + const acquisitionPie = (subscriptions.acquisition_by_source ?? []).map((s, i) => ({ + name: formatAnalyticsLabel(s.label), + value: s.count, + color: PIE_COLORS[i % PIE_COLORS.length], + })) + + const cohortPie = (subscriptions.new_by_cohort ?? []).map((s, i) => ({ + name: formatAnalyticsLabel(s.label), + value: s.count, + color: PIE_COLORS[i % PIE_COLORS.length], + })) + const revenueData = payments.revenue_last_30_days.map((d) => ({ date: formatDate(d.date), revenue: d.revenue, @@ -513,9 +622,9 @@ export function AnalyticsPage() { /> 0 ? "up" : "neutral"} /> + {snapshot && ( +
+ + 0 ? "down" : "neutral"} + /> + + 0 ? "up" : "neutral"} + /> +
+ )} + {renewal && ( +
+ = 0.3 ? "up" : renewal.expired_in_period > 0 ? "down" : "neutral"} + /> + 0 ? "up" : "neutral"} + /> + + 0 + ? renewal.returning_subscriptions / + (renewal.first_time_subscriptions + renewal.returning_subscriptions) + : 0, + )} + sub={`${formatNumber(renewal.returning_subscriptions)} of ${formatNumber(renewal.first_time_subscriptions + renewal.returning_subscriptions)} new subs`} + trend={renewal.returning_subscriptions > 0 ? "up" : "neutral"} + /> +
+ )}
@@ -835,6 +1013,65 @@ export function AnalyticsPage() {
+ + {churnSeries.length > 0 && ( + + +
+
+ Cancellations, Expirations & Resubscribes +
+ Churn vs win-back over {seriesPeriodLabel.toLowerCase()} +
+
+ {seriesPeriodLabel} +
+
+ + + + + + + + + + + + + + +
+ )} +
+ + +
+
+ s + r.count, 0) || + subscriptions.total_subscriptions + ).toString()} + centerLabel="Subs" + /> + + +
+
+
+
+ +
{/* ─── Issues & Support ─── */} diff --git a/src/types/analytics.types.ts b/src/types/analytics.types.ts index 84b5cc2..f754a78 100644 --- a/src/types/analytics.types.ts +++ b/src/types/analytics.types.ts @@ -37,6 +37,36 @@ export interface DashboardUsers { registrations_last_30_days: DateCount[] } +export interface LabelAmount { + label: string + count: number + amount: number +} + +export interface SubscriptionSnapshot { + active_now: number + active_learners: number + pending_now: number + expired_now: number + cancelled_now: number + expiring_within_7_days: number + expiring_within_30_days: number + auto_renew_enabled: number + lifetime_active: number + term_active: number +} + +export interface SubscriptionRenewal { + first_time_subscriptions: number + returning_subscriptions: number + resubscribe_after_expiry: number + expired_in_period: number + renewed_after_expiry: number + renewal_rate: number + avg_days_to_resubscribe: number + median_days_to_resubscribe: number +} + export interface DashboardSubscriptions { total_subscriptions: number active_subscriptions: number @@ -46,6 +76,17 @@ export interface DashboardSubscriptions { by_status: LabelCount[] revenue_by_plan: RevenuePlan[] new_subscriptions_last_30_days: DateCount[] + snapshot?: SubscriptionSnapshot + active_by_category?: LabelCount[] + active_by_plan?: LabelCount[] + active_by_lifetime?: LabelCount[] + acquisition_by_source?: LabelCount[] + revenue_by_category?: LabelAmount[] + cancellations_last_30_days?: DateCount[] + expirations_last_30_days?: DateCount[] + renewal?: SubscriptionRenewal + new_by_cohort?: LabelCount[] + resubscriptions_last_30_days?: DateCount[] } export interface DashboardPayments {