Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 220 additions & 57 deletions src/AnalyticsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "react";
import { trendRows, downloadCsv, type TrendMetric } from "./analytics-data";
import { trendRows, type TrendMetric } from "./analytics-data";
import { Button } from "@/components/ui/button";
import { Select } from "@/components/ui/select";
import { Input } from "@/components/ui/input";
Expand All @@ -17,7 +17,6 @@ import { Badge } from "@/components/ui/badge";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
Expand All @@ -34,6 +33,7 @@ function percentage(value: number | null) {

function day(value: string) {
return new Intl.DateTimeFormat("de-DE", {
timeZone: "UTC",
day: "2-digit",
month: "short",
}).format(new Date(value));
Expand Down Expand Up @@ -161,24 +161,9 @@ function GrowthChart({ analytics }: { analytics: Analytics }) {
<CardHeader className="gap-3">
<div className="flex items-center justify-between">
<CardTitle className="text-base">Verlauf · UTC</CardTitle>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => setTable(!table)}
>
{table ? "Diagramm" : "Tabelle"}
</Button>
<Button
variant="outline"
size="sm"
onClick={() =>
downloadCsv(`lanis-trends-${analytics.range_days}d.csv`, points)
}
>
CSV
</Button>
</div>
<Button variant="outline" size="sm" onClick={() => setTable(!table)}>
{table ? "Diagramm" : "Tabelle"}
</Button>
</div>
<Select
aria-label="Metrik"
Expand Down Expand Up @@ -219,7 +204,7 @@ function GrowthChart({ analytics }: { analytics: Analytics }) {
"active_schools",
] as const
).map((key) => (
<td key={key} className="p-2 font-mono">
<td key={key} className="p-2 tabular-nums">
{number(point[key])}
</td>
))}
Expand Down Expand Up @@ -271,7 +256,7 @@ function GrowthChart({ analytics }: { analytics: Analytics }) {
{Object.entries(labels).map(([key, label]) => (
<span key={key}>
{label}:{" "}
<b className="font-mono">
<b className="tabular-nums">
{number(current[key as TrendMetric])}
</b>
</span>
Expand Down Expand Up @@ -302,16 +287,7 @@ function ModuleUsage({ analytics }: { analytics: Analytics }) {
return (
<Card>
<CardHeader className="gap-3">
<div className="flex items-center justify-between">
<CardTitle className="text-base">Module</CardTitle>
<Button
variant="outline"
size="sm"
onClick={() => downloadCsv("lanis-modules.csv", modules)}
>
CSV
</Button>
</div>
<CardTitle className="text-base">Module</CardTitle>
<Input
aria-label="Modul suchen"
placeholder="Modul suchen …"
Expand Down Expand Up @@ -342,13 +318,13 @@ function ModuleUsage({ analytics }: { analytics: Analytics }) {
{modules.map((row) => (
<tr key={row.module} className="border-t">
<td className="p-3 font-medium">{row.module}</td>
<td className="p-3 text-right font-mono">
<td className="p-3 text-right tabular-nums">
{number(row.launches)}
</td>
<td className="p-3 text-right font-mono">
<td className="p-3 text-right tabular-nums">
{number(row.unique_users)}
</td>
<td className="p-3 text-right font-mono">
<td className="p-3 text-right tabular-nums">
{percentage(total ? (row.launches / total) * 100 : 0)}
</td>
</tr>
Expand Down Expand Up @@ -427,13 +403,204 @@ function Anomalies({ analytics }: { analytics: Analytics }) {
);
}

type Journey = NonNullable<Analytics["new_user_journey"]>;

function NewUserChart({ dates }: { dates: Journey["days"] }) {
const [mode, setMode] = useState<"volume" | "rates">("volume");
const [selected, setSelected] = useState("");
const current = dates.find((row) => row.day === selected) ?? dates.at(-1);
const maximum = Math.max(1, ...dates.map((row) => row.new_users));
const volume = mode === "volume";
const top = volume ? maximum : 100;
const scale = [top, top / 2, 0];
return (
<section className="min-w-0 space-y-3">
<div className="flex flex-wrap items-center justify-between gap-2">
<h3 className="text-sm font-semibold">Pro Tag</h3>
<Select aria-label="Diagrammansicht" value={mode} onChange={(event) => setMode(event.target.value as typeof mode)}>
<option value="volume">Anzahl</option>
<option value="rates">Quoten</option>
</Select>
</div>
<div className="flex flex-wrap gap-x-4 gap-y-1 text-xs text-surface-500">
{[["bg-primary-500", "Wieder aktiv"],
[volume ? "bg-surface-300 dark:bg-surface-700" : "bg-violet-500",
volume ? "Ohne Rückkehr" : "Modul gestartet"],
["bg-amber-400", "Beobachtung läuft"]].map(([color, label]) => (
<span key={label} className="inline-flex items-center gap-1.5">
<span className={`size-2.5 rounded-sm ${color}`} />{label}
</span>
))}
</div>
<div className="flex gap-2" aria-hidden="true">
<div className="flex h-40 shrink-0 flex-col justify-between text-right tabular-nums text-[10px] text-surface-500">
{scale.map((tick) => <span key={tick}>{volume ? tick.toLocaleString("de-DE") : `${tick}%`}</span>)}
</div>
<div className="min-w-0 flex-1 overflow-x-auto">
<div style={{ minWidth: dates.length * 6 }}>
<div className="relative flex h-40 items-end gap-px border-y border-surface-200 dark:border-surface-800">
<div className="pointer-events-none absolute inset-x-0 top-1/2 border-t border-surface-200 dark:border-surface-800" />
{dates.map((row) => {
const pending = row.new_users - row.eligible;
const height = (value: number) => `${value / maximum * 100}%`;
const share = (value: number) => `${row.eligible ? value / row.eligible * 100 : 0}%`;
return (
<div key={row.day} onPointerEnter={() => setSelected(row.day)} onClick={() => setSelected(row.day)}
className={`relative flex h-full min-w-0 flex-1 justify-center ${current?.day === row.day ? "bg-primary-100/60 dark:bg-primary-900/30" : ""}`}>
{volume ? (
<div className="flex h-full w-full max-w-8 flex-col-reverse">
<div className="bg-primary-500" style={{ height: height(row.returned) }} />
<div className="bg-surface-300 dark:bg-surface-700" style={{ height: height(row.eligible - row.returned) }} />
<div className="bg-amber-400" style={{ height: height(pending) }} />
</div>
) : (
<div className="flex h-full w-full max-w-8 items-end gap-px">
<div className="flex-1 bg-primary-500" style={{ height: share(row.returned) }} />
<div className="flex-1 bg-violet-500" style={{ height: share(row.opened_module) }} />
{pending > 0 && <span className="absolute inset-x-0 bottom-0 h-1 bg-amber-400" />}
</div>
)}
</div>
);
})}
</div>
<div className="mt-1 flex justify-between text-[10px] text-surface-500">
<span>{day(dates[0].day)}</span><span>{day(dates.at(-1)!.day)}</span>
</div>
</div>
</div>
</div>
<div className="flex flex-wrap items-center gap-2 rounded-lg bg-surface-50 p-2 text-xs dark:bg-surface-900">
<Select aria-label="Datum für Tagesdetails" value={current?.day ?? ""} onChange={(event) => setSelected(event.target.value)}>
{dates.map((row) => <option key={row.day}>{row.day}</option>)}
</Select>
{current && <span>
<strong>{number(current.new_users)} neu</strong>
{current.eligible > 0
? ` · ${current.returned} / ${current.eligible} zurück (${percentage(100 * current.returned / current.eligible)}) · ${current.opened_module} / ${current.eligible} mit Modulstart (${percentage(100 * current.opened_module / current.eligible)})`
: current.new_users > 0 && " · Beobachtung läuft"}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
</span>}
</div>
</section>
);
}

function WeekComparison({ dates }: { dates: Journey["days"] }) {
// The latest eight dates include today and seven cohorts still under observation.
const recent = dates.slice(-15, -8);
const previous = dates.slice(-22, -15);
if (recent.length !== 7 || previous.length !== 7) return null;
const windows = [previous, recent].map((week) => ({
label: `${day(week[0].day)} – ${day(week[6].day)}`,
eligible: week.reduce((sum, row) => sum + row.eligible, 0),
returned: week.reduce((sum, row) => sum + row.returned, 0),
opened_module: week.reduce((sum, row) => sum + row.opened_module, 0),
}));
return (
<section className="space-y-3">
<h3 className="text-sm font-semibold" title="Letzte zwei vollständig beobachtete Anmeldewochen">Wochenvergleich</h3>
<div className="grid gap-3 sm:grid-cols-2">
{([["returned", "Wieder aktiv"], ["opened_module", "Modul gestartet"]] as const).map(([metric, label]) => (
<div key={metric} className="space-y-2 rounded-lg border border-surface-200 p-3 dark:border-surface-800">
<p className="text-xs font-medium">{label}</p>
{windows.map((window, index) => {
const rate = window.eligible ? 100 * window[metric] / window.eligible : null;
return (
<div key={window.label} className="grid grid-cols-[auto_minmax(24px,1fr)_auto] items-center gap-2 text-xs">
<span className="text-surface-500">{window.label}</span>
<div aria-hidden="true" className="h-2 overflow-hidden rounded bg-surface-100 dark:bg-surface-800">
<div className={`h-full rounded ${index ? "bg-primary-500" : "bg-surface-400"}`} style={{ width: `${rate ?? 0}%` }} />
</div>
<span className="text-right tabular-nums">
{percentage(rate)}
{window.eligible > 0 && (
<span className="block text-[10px] text-surface-500">{window[metric]} / {window.eligible}</span>
)}
</span>
</div>
);
})}
</div>
))}
</div>
</section>
);
}

function NewUserJourney({ journey, analytics }: { journey: Journey; analytics: Analytics }) {
const outcomes = new Map(journey.days.map((row) => [row.day, row]));
const dates = analytics.login_series.map(({ day }) =>
outcomes.get(day) ?? { day, new_users: 0, eligible: 0, returned: 0, opened_module: 0 },
);
const totals = dates.reduce(
(sum, row) => ({
eligible: sum.eligible + row.eligible,
returned: sum.returned + row.returned,
opened: sum.opened + row.opened_module,
}),
{ eligible: 0, returned: 0, opened: 0 },
);

return (
<Card>
<CardHeader>
<CardTitle className="text-base">Neue Konten · Erste Woche</CardTitle>
</CardHeader>
<CardContent className="space-y-5">
<div className="grid gap-3 sm:grid-cols-2">
{([
["Rückkehr (Tag 1–7)", totals.returned],
["Modulstart (Tag 0–7)", totals.opened],
] as const).map(([label, count]) => (
<div key={label} className="rounded-lg border border-surface-200 p-3 dark:border-surface-800">
<p className="text-sm text-surface-500">{label}</p>
<p className="mt-1 text-2xl font-semibold tabular-nums">
{percentage(totals.eligible ? count / totals.eligible * 100 : null)}
</p>
<p className="text-xs text-surface-500">
{totals.eligible
? `${number(count)} von ${number(totals.eligible)} Konten`
: "Beobachtung läuft"}
</p>
</div>
))}
</div>
<div className="grid gap-5 lg:grid-cols-[minmax(0,1.4fr)_minmax(0,1fr)]">
{journey.days.length ? <NewUserChart dates={dates} />
: <p className="text-sm text-surface-500">Keine neuen Konten im Zeitraum.</p>}
<section className="min-w-0">
<h3 className="mb-2 text-sm font-semibold">Beliebteste Module</h3>
{journey.modules.length ? (
<ul className="max-h-72 space-y-3 overflow-auto">
{journey.modules.map((module) => (
<li key={module.module}>
<div className="mb-1 flex flex-wrap justify-between gap-x-2 text-xs">
<span className="break-all font-medium">{module.module}</span>
<span className="text-surface-500">{percentage(module.unique_users / totals.eligible * 100)} · {number(module.unique_users)} Konten · {number(module.launches)} Starts</span>
</div>
<div aria-hidden="true" className="h-1.5 overflow-hidden rounded-full bg-surface-200 dark:bg-surface-800">
<div className="h-full rounded-full bg-primary-500"
style={{ width: `${module.unique_users / totals.eligible * 100}%` }} />
</div>
</li>
))}
</ul>
) : <p className="text-sm text-surface-500">Keine Modulstarts erfasst.</p>}
</section>
</div>
{journey.days.length > 0 && <WeekComparison dates={dates} />}
</CardContent>
</Card>
);
}

function Retention({ analytics }: { analytics: Analytics }) {
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2 text-base">
<CardTitle className="flex items-center gap-2 text-base" title={analytics.definitions.retention}>
<Users size={17} className="text-primary-600" />
Retention · Kohorten
Rückkehr nach Erstnutzungswoche
</CardTitle>
</CardHeader>
<CardContent className="p-0">
Expand All @@ -458,18 +625,17 @@ function Retention({ analytics }: { analytics: Analytics }) {
<td className="px-5 py-3 font-medium text-surface-700 dark:text-surface-200">
{day(cohort.cohort)}
</td>
<td className="px-3 py-3 text-right font-mono text-surface-500">
<td className="px-3 py-3 text-right tabular-nums text-surface-500">
{number(cohort.new_users)}
</td>
<td className="px-3 py-3 text-right font-mono">
{percentage(cohort.retention_1d)}
</td>
<td className="px-3 py-3 text-right font-mono">
{percentage(cohort.retention_7d)}
</td>
<td className="px-5 py-3 text-right font-mono">
{percentage(cohort.retention_30d)}
</td>
{[cohort.retention_1d, cohort.retention_7d, cohort.retention_30d].map((rate, index) => (
<td key={index} className="px-2 py-1 text-center tabular-nums">
<span className="relative isolate block overflow-hidden rounded px-1 py-2 text-surface-900 dark:text-surface-100">
{rate !== null && <span aria-hidden="true" className="absolute inset-y-0 left-0 -z-10 bg-primary-500/25" style={{ width: `${rate}%` }} />}
{percentage(rate)}
</span>
</td>
))}
</tr>
))}
</tbody>
Expand Down Expand Up @@ -561,32 +727,29 @@ export function AnalyticsPage({
{error}
</p>
)}
<p className="text-xs text-surface-500">
{loading
? "Laden …"
: `${rows[0]?.day || "—"} – ${rows.at(-1)?.day || "—"} · UTC · ${new Date(analytics.generated_at).toLocaleString("de-DE")}`}
</p>
{loading && <p role="status" className="text-xs text-surface-500">Laden …</p>}
<div className="grid grid-cols-2 gap-3 lg:grid-cols-4">
{totals.map(([label, value]) => (
<Card key={label}>
<CardContent className="p-4">
<p className="text-xs text-surface-500">{label}</p>
<p className="mt-2 font-mono text-2xl">
<p className="mt-2 text-2xl font-semibold tabular-nums">
{value.toLocaleString("de-DE", { maximumFractionDigits: 1 })}
</p>
</CardContent>
</Card>
))}
</div>
{analytics.new_user_journey && (
<NewUserJourney journey={analytics.new_user_journey} analytics={analytics} />
)}
<Retention analytics={analytics} />
<div className="grid gap-4 xl:grid-cols-[minmax(0,1.25fr)_minmax(340px,.75fr)]">
<GrowthChart analytics={analytics} />
<ModuleUsage analytics={analytics} />
</div>
<Heatmap analytics={analytics} />
<div className="grid gap-4 xl:grid-cols-2">
<Anomalies analytics={analytics} />
<Retention analytics={analytics} />
</div>
<Anomalies analytics={analytics} />
</div>
);
}
4 changes: 4 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export type Analytics = {
heatmap: Array<{ weekday: number; hour: number; events: number; unique_users: number }>
module_usage: Array<{ module: string; launches: number; unique_users: number }>
anomalies: Array<{ day: string; metric: string; value: number; baseline: number; direction: 'spike' | 'drop'; severity: 'medium' | 'high' }>
new_user_journey?: {
days: Array<{ day: string; new_users: number; eligible: number; returned: number; opened_module: number }>
modules: Array<{ module: string; unique_users: number; launches: number }>
}
retention: Array<{ cohort: string; new_users: number; retention_1d: number | null; retention_7d: number | null; retention_30d: number | null }>
definitions: { heatmap: string; modules: string; anomalies: string; retention: string }
}
Expand Down
Loading