The Pug Web SDK is a TypeScript library for the browser, MIT-licensed, with two runtime
dependencies (@bufbuild/protobuf and uuidv7). The source is
pug-sh/sdk-browser on GitHub, and because Pug is self-hostable, the events it sends can stay on
your own infrastructure.
Autocapture after one init()
After init(), the SDK autocaptures six kinds of interaction with no
extra code: page views (including SPA route changes via patched history),
clicks (with element, text, and coordinates), scroll depth,
form starts and submits, and two frustration signals, rage clicks (four or more
clicks within a second in one spot) and dead clicks (clicks that change nothing). Each tracker is
one file in src/events. You can narrow capture to an allowlist when
you only want some of them:
init(projectId, {
apiKey,
autoCapture: { pageView: true, click: true },
}) A typed track(), and well-known events
Beyond autocapture, track() sends any custom event. Pug also defines
well-known events, standard names like purchase or
signup whose properties are typed, so TypeScript rejects a known property of the wrong type (an
amount passed as a string) before the code ships. The types come from Pug’s protobuf catalog and are
compile-time only, so well-known-events.ts adds nothing to the
bundle:
track('signup', { plan: 'pro' })
track('purchase', { productId: 'sku_123', amount: 49, currency: 'USD' }) Extra properties beyond the schema are allowed and sent as custom properties, like plan above.
Identity that merges
With consent granted, events before sign-in carry a persistent anonymous ID. When a visitor identifies, the SDK merges that anonymous history into a unified profile and attaches traits:
identify('user_123', { email: 'ada@example.com', plan: 'pro' })
The first identify() sends the anonymous ID along, so the server merges the two profiles; later calls
update traits only. reset() clears identity on logout so the next visitor starts clean. The code is in
profile.ts.
Sessions, automatically
With consent granted, the SDK keeps the session on the device and shares it across tabs, with a 30-minute idle
timeout and a 24-hour maximum by default (session.idleTimeoutMinutes and
session.maxSessionMinutes). There are no timers to leak: expiry is checked when an event fires. In
cookieless mode the server works out sessions instead, as the
cookieless identity post describes. The code is in
session.ts.
Built for consent
The SDK is consent-first, which matters for GDPR. It defaults to
'cookieless': events flow, but no identifier is stored on the device and identify() does
nothing. Initialize with consent denied instead and no automatic listeners attach, while manual
track() and identify() are dropped, not queued for later replay. Grant consent when the
user agrees:
init(projectId, { apiKey, trackingConsent: 'denied' })
// after the user agrees:
optInTracking() trackingConsent: { initial: 'denied', persist: true } remembers the choice across reloads, and
opting out tears the listeners back down. The three states and their rules are in
tracking-consent.ts.
Reliable delivery
Events go out in batches, one request once 10 are queued or 5 seconds after the first. A consenting visitor’s queue
is kept in localStorage and survives a reload, while cookieless events stay in memory. On
pagehide, the SDK sends what is still queued in one fetch with keepalive set, which the
browser lets outlive the page. Timeouts and network errors roll back and retry, and a priority event can skip the
batch with an immediate flag. How we deliver events
reliably covers each rule and links batch.ts.
Properties you don’t have to set
Every event carries context the SDK adds itself: URL, referrer, locale, screen size, SDK version, UTM parameters, the page title on page views, and device and browser details from UA Client Hints where the browser has them. The server adds geo on ingest. The list is in track.ts.
Own the whole pipeline
Because Pug is open source and self-hostable, the SDK talks to a backend you can run yourself, with your data on your servers. See the SDKs page for install and the platform overview for how events flow from the SDK to insights.