Phase 1 of replacing Microsoft Clarity with first-party usage analytics: - Plain Postgres tables via shared DDL module (src/lib/analytics/schema.ts) executed by both the add_analytics_tables migration and the test bootstrap; test-db.ts drops them around drizzle push (composite-PK introspector bug) - /api/collect beacon collector with IP/UA enrichment, retroactive login attribution, bot blocklist, per-IP rate limiting, strict validation - Client SDK (src/lib/client-analytics): SPA pageviews, clicks, scroll depth, focus, data-track visibility, heartbeats; NEXT_PUBLIC_ANALYTICS_DEV=1 dev gate with console mirror; mounted beside Clarity in the two frontend layouts - Hourly rollup + daily retention bins (payload analytics-rollup/-purge) - Chronicle read progress/completion, wiki new-clicked/created, minigame route mapping custom events - admin:analytics:manage permission (+ roles enum migration) and the /admin/analytics Payload view with six recharts-backed report panels
138 lines
4.9 KiB
TypeScript
138 lines
4.9 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
Area,
|
|
AreaChart,
|
|
Bar,
|
|
BarChart,
|
|
CartesianGrid,
|
|
ResponsiveContainer,
|
|
Tooltip,
|
|
XAxis,
|
|
YAxis,
|
|
} from "recharts";
|
|
|
|
import type { DailyPoint, GameReportRow, ScrollBucket } from "@/lib/analytics/queries";
|
|
|
|
/**
|
|
* Recharts panels for the analytics admin view. Kept as tiny client islands:
|
|
* the surrounding view (tabs, tables, filters) renders server-side. Colors
|
|
* are explicit hex values because the Payload admin panel does not define the
|
|
* frontend chart CSS variables.
|
|
*/
|
|
|
|
const AXIS_STYLE = { fill: "#a3a3a3", fontSize: 11 } as const;
|
|
|
|
interface TooltipEntry {
|
|
name?: string | number;
|
|
value?: string | number;
|
|
color?: string;
|
|
}
|
|
|
|
function AdminTooltip({
|
|
active,
|
|
payload,
|
|
label,
|
|
labelFormatter,
|
|
}: {
|
|
active?: boolean;
|
|
payload?: TooltipEntry[];
|
|
label?: string | number;
|
|
labelFormatter?: (label: string | number) => string;
|
|
}) {
|
|
if (!active || !payload || payload.length === 0) return null;
|
|
return (
|
|
<div className="rounded border border-white/15 bg-neutral-900 px-2.5 py-1.5 text-xs text-neutral-200 shadow-lg">
|
|
<p className="mb-1 font-medium text-neutral-400">
|
|
{labelFormatter && label !== undefined ? labelFormatter(label) : label}
|
|
</p>
|
|
{payload.map((entry, index) => (
|
|
<p key={index} className="flex items-center gap-1.5">
|
|
<span
|
|
className="inline-block size-2 rounded-sm"
|
|
style={{ backgroundColor: entry.color ?? "#a3a3a3" }}
|
|
/>
|
|
<span>{entry.name}</span>
|
|
<span className="ml-auto tabular-nums">{entry.value}</span>
|
|
</p>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function OverviewTrendChart({ series }: { series: DailyPoint[] }) {
|
|
return (
|
|
<div className="h-64 w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<AreaChart data={series} margin={{ top: 8, right: 8, bottom: 0, left: -16 }}>
|
|
<defs>
|
|
<linearGradient id="pv" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="0%" stopColor="#a78bfa" stopOpacity={0.35} />
|
|
<stop offset="100%" stopColor="#a78bfa" stopOpacity={0} />
|
|
</linearGradient>
|
|
<linearGradient id="ss" x1="0" y1="0" x2="0" y2="1">
|
|
<stop offset="0%" stopColor="#22d3ee" stopOpacity={0.35} />
|
|
<stop offset="100%" stopColor="#22d3ee" stopOpacity={0} />
|
|
</linearGradient>
|
|
</defs>
|
|
<CartesianGrid stroke="rgba(255,255,255,0.06)" vertical={false} />
|
|
<XAxis dataKey="day" tick={AXIS_STYLE} tickLine={false} axisLine={false} />
|
|
<YAxis tick={AXIS_STYLE} tickLine={false} axisLine={false} width={48} allowDecimals={false} />
|
|
<Tooltip content={<AdminTooltip />} cursor={{ stroke: "rgba(255,255,255,0.2)" }} />
|
|
<Area
|
|
type="monotone"
|
|
dataKey="pageviews"
|
|
name="Pageviews"
|
|
stroke="#a78bfa"
|
|
strokeWidth={1.5}
|
|
fill="url(#pv)"
|
|
/>
|
|
<Area
|
|
type="monotone"
|
|
dataKey="sessions"
|
|
name="Sessions"
|
|
stroke="#22d3ee"
|
|
strokeWidth={1.5}
|
|
fill="url(#ss)"
|
|
/>
|
|
</AreaChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function ScrollDistributionChart({ buckets }: { buckets: ScrollBucket[] }) {
|
|
return (
|
|
<div className="h-56 w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<BarChart data={buckets} margin={{ top: 8, right: 8, bottom: 0, left: -16 }}>
|
|
<CartesianGrid stroke="rgba(255,255,255,0.06)" vertical={false} />
|
|
<XAxis dataKey="bucket" tick={AXIS_STYLE} tickLine={false} axisLine={false} />
|
|
<YAxis tick={AXIS_STYLE} tickLine={false} axisLine={false} width={48} allowDecimals={false} />
|
|
<Tooltip
|
|
content={<AdminTooltip labelFormatter={(label) => `Scrolled ${label}` as string} />}
|
|
cursor={{ fill: "rgba(255,255,255,0.04)" }}
|
|
/>
|
|
<Bar dataKey="pages" name="Page views" fill="#34d399" radius={[2, 2, 0, 0]} />
|
|
</BarChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function GamesBarChart({ rows }: { rows: GameReportRow[] }) {
|
|
const data = rows.map((row) => ({ name: row.gameKey, sessions: row.sessions }));
|
|
return (
|
|
<div className="h-56 w-full">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<BarChart data={data} margin={{ top: 8, right: 8, bottom: 0, left: -16 }}>
|
|
<CartesianGrid stroke="rgba(255,255,255,0.06)" vertical={false} />
|
|
<XAxis dataKey="name" tick={AXIS_STYLE} tickLine={false} axisLine={false} />
|
|
<YAxis tick={AXIS_STYLE} tickLine={false} axisLine={false} width={48} allowDecimals={false} />
|
|
<Tooltip content={<AdminTooltip />} cursor={{ fill: "rgba(255,255,255,0.04)" }} />
|
|
<Bar dataKey="sessions" name="Play sessions" fill="#fbbf24" radius={[2, 2, 0, 0]} />
|
|
</BarChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
}
|