diff --git a/src/lib/analytics.ts b/src/lib/analytics.ts index 269ccae..75fb866 100644 --- a/src/lib/analytics.ts +++ b/src/lib/analytics.ts @@ -131,6 +131,11 @@ export function getSeriesPeriodLabel(dateFilter?: DashboardDateFilter): string { } } +export function formatPercentRate(rate: number, fractionDigits = 1): string { + if (!Number.isFinite(rate)) return "0%" + return `${(rate * 100).toFixed(fractionDigits)}%` +} + /** Display label for dashboard breakdown rows (regions, enums, free text). */ export function formatAnalyticsLabel(label: string): string { const text = label?.trim() ?? "" diff --git a/src/pages/analytics/AnalyticsPage.tsx b/src/pages/analytics/AnalyticsPage.tsx index b3b50f5..b178494 100644 --- a/src/pages/analytics/AnalyticsPage.tsx +++ b/src/pages/analytics/AnalyticsPage.tsx @@ -33,6 +33,9 @@ import { FolderOpen, RefreshCw, ChevronDown, + PlayCircle, + RotateCcw, + CheckCircle2, } from "lucide-react" import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/card" import { Badge } from "../../components/ui/badge" @@ -46,6 +49,7 @@ import { formatAnalyticsLabel, getSubscriptionMetrics, getVideoLessonsSummary, + formatPercentRate, } from "../../lib/analytics" import type { DashboardData, DashboardFilters, LabelCount } from "../../types/analytics.types" @@ -363,7 +367,7 @@ export function AnalyticsPage() { ) } - const { users, subscriptions, payments, courses, content, notifications, issues, team } = dashboard + const { users, subscriptions, payments, courses, content, notifications, issues, team, videos } = dashboard const subscriptionMetrics = getSubscriptionMetrics(subscriptions) const seriesPeriodLabel = getSeriesPeriodLabel(dashboard.date_filter) const lms = courses.lms @@ -402,6 +406,21 @@ export function AnalyticsPage() { color: PIE_COLORS[i % PIE_COLORS.length], })) + const dropOffData = + videos?.drop_off_by_checkpoint.map((d) => ({ + checkpoint: `${d.checkpoint_percent}%`, + checkpointPercent: d.checkpoint_percent, + dropOffRate: Number((d.drop_off_rate * 100).toFixed(1)), + viewersReached: d.viewers_reached, + totalSessions: d.total_sessions, + })) ?? [] + + const retentionData = + videos?.drop_off_by_checkpoint.map((d) => ({ + checkpoint: `${d.checkpoint_percent}%`, + viewers: d.viewers_reached, + })) ?? [] + const generatedAt = new Date(dashboard.generated_at).toLocaleString("en-US", { month: "short", day: "numeric", @@ -880,6 +899,160 @@ export function AnalyticsPage() { + {/* ─── Video Analytics ─── */} + {videos && ( +
+
+ 0 ? "up" : "neutral"} + /> + = 0.25 ? "up" : "neutral"} + /> + 0 ? "up" : "neutral"} + /> + +
+ +
+ + +
+
+ Viewer Retention by Checkpoint +
+ Viewers still watching at each progress milestone +
+
+ {seriesPeriodLabel} +
+
+ + {retentionData.length > 0 ? ( + + + + + + + + + + + + [Number(v).toLocaleString(), "Viewers"]} + contentStyle={{ + borderRadius: 12, + border: "1px solid #E0E0E0", + boxShadow: "0 10px 30px rgba(0,0,0,0.08)", + fontSize: 12, + }} + /> + + + + ) : ( +
+ No checkpoint data available +
+ )} +
+
+ + + +
+
+ Drop-off Rate by Checkpoint +
+ Share of sessions that stopped before each milestone +
+
+ {seriesPeriodLabel} +
+
+ + {dropOffData.length > 0 ? ( + + + + + `${v}%`} + domain={[0, 100]} + /> + { + const row = props?.payload as (typeof dropOffData)[number] | undefined + return [ + `${Number(v).toFixed(1)}% drop-off`, + `${row?.viewersReached?.toLocaleString() ?? 0} viewers reached`, + ] + }} + contentStyle={{ + borderRadius: 12, + border: "1px solid #E0E0E0", + boxShadow: "0 10px 30px rgba(0,0,0,0.08)", + fontSize: 12, + }} + /> + + + + ) : ( +
+ No drop-off data available +
+ )} +
+
+
+ + {dropOffData.length > 0 && ( +
+ ({ + label: `${d.checkpointPercent}% — ${d.viewersReached} viewers (${d.dropOffRate}% drop-off)`, + count: d.viewersReached, + }))} + total={videos.total_watch_sessions} + /> +
+ )} +
+ )} + {/* ─── Team ─── */}
diff --git a/src/types/analytics.types.ts b/src/types/analytics.types.ts index 0e2bd8d..84b5cc2 100644 --- a/src/types/analytics.types.ts +++ b/src/types/analytics.types.ts @@ -117,6 +117,24 @@ export interface DashboardTeam { by_status: LabelCount[] } +export interface VideoDropOffCheckpoint { + checkpoint_percent: number + total_sessions: number + viewers_reached: number + drop_off_rate: number +} + +export interface DashboardVideos { + total_watch_sessions: number + completed_sessions: number + replay_sessions: number + unique_video_starts: number + users_who_replayed: number + completion_rate: number + replay_rate: number + drop_off_by_checkpoint: VideoDropOffCheckpoint[] +} + export type DashboardDateFilterMode = "all_time" | "year" | "year_month" | "custom" export interface DashboardDateFilter { @@ -153,6 +171,7 @@ export interface DashboardData { notifications: DashboardNotifications issues: DashboardIssues team: DashboardTeam + videos?: DashboardVideos } export interface DashboardResponse {