Stats: ipalKit({ stats: true }) adds the stats-visits collection, POST /api/ipal/hit, the
/admin/stats view (30-day visits chart, top/entry pages, referrers, devices), a dashboard card
and a nav link. <PageStats> (or <SiteProviders stats>) sends one beacon per page view. Without
consent nothing is stored on the device and no IP is kept: page views are joined by a hash with
an in-memory salt replaced daily. With analytics consent an ipal_vid cookie links visits across
days (server re-checks the consent cookie and policy version; withdrawal deletes the cookie).
Bots, logged-in panel users and prefetches aren't counted. Retention 90 days.
@payloadcms/next added to peerDependencies (DefaultTemplate). Docs: stats.md with privacy text.
Also in this release: meta.description auto-filled from page blocks, <NoSnippet>,
MetaDescriptionCheck in the SEO tab, data-nosnippet on banner/language switcher, GA4 CSP
wildcards, consent version passed to Analytics' default consent.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import type { Payload } from 'payload'
|
|
|
|
import type { StatsVisit } from '../collection.js'
|
|
import type { StatsSummary } from '../summarize.js'
|
|
|
|
import { STATS_SLUG } from '../collection.js'
|
|
import { lastDays, summarize } from '../summarize.js'
|
|
|
|
const VISIT_FIELDS = {
|
|
day: true,
|
|
device: true,
|
|
entry: true,
|
|
exit: true,
|
|
lastAt: true,
|
|
pageCount: true,
|
|
pages: true,
|
|
referrer: true,
|
|
startedAt: true,
|
|
visitorId: true,
|
|
} as const
|
|
|
|
/** Summary of the last `days` days. Callers check the user is an admin first. */
|
|
export async function loadStats(payload: Payload, { days = 30, timeZone = 'Europe/Warsaw' } = {}): Promise<StatsSummary> {
|
|
const range = lastDays(days, timeZone)
|
|
const { docs } = await payload.find({
|
|
collection: STATS_SLUG as never,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
pagination: false,
|
|
select: VISIT_FIELDS as never,
|
|
where: { day: { greater_than_equal: range[0] } },
|
|
})
|
|
return summarize(docs as unknown as StatsVisit[], range)
|
|
}
|
|
|
|
/** Every stored visit of one consenting visitor, newest first. */
|
|
export async function loadVisitor(payload: Payload, visitorId: string): Promise<StatsVisit[]> {
|
|
const { docs } = await payload.find({
|
|
collection: STATS_SLUG as never,
|
|
depth: 0,
|
|
limit: 500,
|
|
overrideAccess: true,
|
|
pagination: false,
|
|
select: VISIT_FIELDS as never,
|
|
sort: '-startedAt',
|
|
where: { visitorId: { equals: visitorId } },
|
|
})
|
|
return docs as unknown as StatsVisit[]
|
|
}
|
|
|
|
/** Latest visits that carry a visitor id — the entry points into visitor history. */
|
|
export async function loadRecentVisitors(payload: Payload, limit = 20): Promise<StatsVisit[]> {
|
|
const { docs } = await payload.find({
|
|
collection: STATS_SLUG as never,
|
|
depth: 0,
|
|
limit,
|
|
overrideAccess: true,
|
|
select: VISIT_FIELDS as never,
|
|
sort: '-lastAt',
|
|
where: { visitorId: { exists: true } },
|
|
})
|
|
return docs as unknown as StatsVisit[]
|
|
}
|