Skip to content

Analytics API

import { analytics, summarize, toGA4, toPostHog, toSegment, toPlausible } from 'pagepal/analytics';

Returns a plugin: pass it to plugins or use(). Use one instance per pal.

OptionTypeDefault
endpointstringnonePOST batches here via sendBeacon
sendSink | Sink[]noneYour own delivery, or adapters
consentbooleantrueStart without consent (false) and grant it later
respectPrivacySignalsbooleantrueOff when Do Not Track or GPC is set
sampleRatenumber1Share of page views to record
includeUXEventType[]all except js_errorEvent types to record
captureTextbooleantrueShort masked labels for clicked elements
slowRequestMsnumber3000Threshold for request_slow
batchSizenumber20Send when this many are queued
flushIntervalnumber5000Send at least this often (ms)
redact(event) => event | null | voidnoneScrub or drop events

Sink is (events: UXEvent[]) => void | Promise<unknown>.

Member
consent(granted)Grant or revoke. Revoking drops unsent events
flush()Send queued events now
events()Events recorded this page view (last 500)
summary()summarize(events())
activeWhether this view is recorded (installed, sampled in, no privacy signal)

Pure function: UXEvent[] → UXSummary. See dashboards.

FunctionArgument
toGA4(gtag)gtag function
toPostHog(posthog)object with capture(name, props)
toSegment(analytics)object with track(name, props)
toPlausible(plausible)plausible function

Each returns a Sink. Events are named pagepal_<type> with flat properties. page_view is not forwarded.

Function
describeElement(el, captureText = true)The privacy-safe descriptor used for element/field/form
sanitizePath(path)/users/123 → /users/:id
sanitizeUrl(url, base)Path only for same-origin; origin + path otherwise; no query or hash
maskText(text, max = 40)Digits → #, emails → [email], collapsed and truncated

UXEvent, UXEventOf<T>, UXEventType, UXSummary, AnalyticsOptions, Sink, Analytics.

The pal in the corner is pagepal itself.