import type { AdminViewServerProps } from 'payload' import { DefaultTemplate } from '@payloadcms/next/templates' import { Gutter } from '@payloadcms/ui' import type { StatsVisit } from '../collection.js' import { isAdmin } from '../../access/predicates.js' import { VISITOR_ID_PATTERN } from '../parse.js' import { loadRecentVisitors, loadStats, loadVisitor } from './loadStats.js' import { STATS_CSS, VisitsChart } from './VisitsChart.js' type Props = { adminRoute?: string; timeZone?: string } & AdminViewServerProps const fmt = (n: number) => n.toLocaleString('en-US') const when = (iso: string, timeZone: string) => new Intl.DateTimeFormat('en-GB', { dateStyle: 'medium', timeStyle: 'short', timeZone }).format(new Date(iso)) function TopList({ rows, title, unit }: { rows: [string, number][]; title: string; unit: string }) { return (

{title}

{rows.length ? ( {rows.map(([key, count]) => ( ))}
{title} {unit}
{key} {fmt(count)}
) : (

No data yet.

)}
) } function VisitorHistory({ base, timeZone, visitorId, visits }: { base: string; timeZone: string; visitorId: string; visits: StatsVisit[] }) { return ( <>

← All statistics

Visitor {visitorId}

{visits.length} visit{visits.length === 1 ? '' : 's'} recorded with analytics consent. To erase this visitor’s data, filter Statistics → Visits by this visitor id and delete the results.

{visits.map((v) => (
{when(v.startedAt, timeZone)} {' '}· {v.pageCount} page{v.pageCount === 1 ? '' : 's'} · {v.device ?? 'unknown device'} {v.referrer ? ` · from ${v.referrer}` : ''}
    {(v.pages ?? []).map((p, i) => (
  1. {p.p} {when(p.t, timeZone).split(', ').pop()}
  2. ))}
))} ) } /** * Admin view at /admin/stats (ipalKit({ stats: true })): visits per day for the * last 30 days, top pages, entry pages, referrers, devices, and — for visitors * who accepted analytics cookies — a per-visitor history (?visitor=). * Admins only. */ export async function StatsView(props: Props) { const { adminRoute = '/admin', initPageResult, searchParams, timeZone = 'Europe/Warsaw' } = props const { req, visibleEntities } = initPageResult const base = `${adminRoute.replace(/\/$/, '')}/stats` const visitorParam = typeof searchParams?.visitor === 'string' ? searchParams.visitor : undefined const visitorId = visitorParam && VISITOR_ID_PATTERN.test(visitorParam) ? visitorParam : undefined let content: React.ReactNode if (!isAdmin(req.user)) { content =

Statistics are available to administrators only.

} else if (visitorId) { content = } else { const [stats, recent] = await Promise.all([loadStats(req.payload, { timeZone }), loadRecentVisitors(req.payload, 50)]) const { totals } = stats const visitors = [...new Map(recent.map((v) => [v.visitorId!, v])).values()].slice(0, 15) content = ( <>

Statistics

Last 30 days. Counted without cookies, so no consent is needed; no IP addresses are stored. Visitors who accept analytics cookies also get a history across days.

{fmt(totals.visits)}Visits
{fmt(totals.views)}Page views
{totals.visits ? (totals.views / totals.visits).toFixed(1) : '0'}Pages per visit
{Math.round(totals.bounceRate * 100)}%Single-page visits
{fmt(totals.consented)}Visits with analytics consent

Visits per day

Visitor history (analytics consent only)

{visitors.length ? ( {visitors.map((v) => ( ))}
Visitor Last seen
{v.visitorId} {when(v.lastAt, timeZone)}
) : (

No visitor has accepted analytics cookies yet.

)}
) } return (
{content}
) } export default StatsView