# Inside the Pug Web SDK

A deep dive into the Pug Web SDK: a TypeScript browser SDK with autocapture, identity merging, consent-first tracking, batching, and typed well-known events.

Source: https://pug.sh/blog/web-analytics-sdk

@pug-sh/browser, the Pug Web SDK, autocaptures six kinds of interaction after one init(), keeps an anonymous ID until identify(), starts in cookieless mode, and sends events in batches to Pug Cloud or your own server. Here is how each part works, with a link to its source.

By [Praveen Kumar](https://github.com/poluruprvn), who builds Pug. Updated November 21, 2026

The [Pug Web SDK](https://pug.sh/sdks) is a TypeScript library for the browser, MIT-licensed, with two runtime dependencies (`@bufbuild/protobuf` and `uuidv7`). The source is [pug-sh/sdk-browser](https://github.com/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](https://pug.sh/glossary/autocapture) 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](https://github.com/pug-sh/sdk-browser/tree/main/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](https://pug.sh/blog/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](https://github.com/pug-sh/sdk-browser/blob/main/src/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](https://pug.sh/glossary/unified-customer-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](https://github.com/pug-sh/sdk-browser/blob/main/src/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](https://pug.sh/blog/cookieless-identity-daily-rotating-salt) describes. The code is in [session.ts](https://github.com/pug-sh/sdk-browser/blob/main/src/session.ts).

## Built for consent

The SDK is consent-first, which matters for [GDPR](https://pug.sh/learn/privacy-first-analytics). 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](https://github.com/pug-sh/sdk-browser/blob/main/src/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](https://pug.sh/blog/reliable-event-delivery) covers each rule and links [batch.ts](https://github.com/pug-sh/sdk-browser/blob/main/src/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](https://github.com/pug-sh/sdk-browser/blob/main/src/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](https://pug.sh/sdks) for install and the [platform overview](https://pug.sh/platform) for how events flow from the SDK to insights.

## Common questions

### What does the Pug Web SDK track automatically?

After a single init(), it autocaptures page views, clicks, scrolls, form starts and submits, plus rage clicks and dead clicks, with no manual instrumentation. You can narrow this to an allowlist, or disable autocapture entirely and send only the events you choose.

### Is the Pug Web SDK GDPR-friendly?

It starts in cookieless mode: events flow, but no identifier is stored on the device and identify() does nothing until consent is granted. Start with tracking consent denied instead and no automatic listeners attach and manual calls are dropped (not queued); call optInTracking() once the user agrees. A choice can be persisted across reloads, and self-hosting keeps the events on your own servers.

### How does the SDK handle identity?

With consent granted, events before sign-in carry an anonymous ID. The first identify() call sends that ID along, so the server merges the anonymous history into the person’s profile, and it attaches traits like plan or email. reset() clears identity on logout.

### Does it lose events on page navigation?

Not on an ordinary navigation or tab close. Events are sent in batches of 10 or every 5 seconds, and on pagehide the SDK sends what is still queued in one fetch with keepalive set, which outlives the page. Timeouts, network errors and 5xx answers are retried.
