Browser SDK
Lightweight client-side tracking SDK for sending user events, identifying users, and tracking page views. Published as @pyrx/synapse-browser on npm. Approximately 5 KB minified.
Installation
Script Tag (Recommended)
Drop the snippet into your HTML <head> to start tracking immediately.
Commands called before the SDK script loads are queued and replayed automatically.
npm (For Bundlers)
The Browser SDK sends the API key in request headers. Always use a browser-scoped API key for client-side code. Browser keys can only track events -- they cannot send emails, read contacts, or access admin endpoints. Create one in Dashboard > Settings > API Keys under the "Client-side" section.
Never use a data, full, or management scoped key in client-side code.
Initialization
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey | string | required | Your Synapse API key (browser scope recommended for client-side) |
endpoint | string | https://synapse-events.pyrx.tech | Event ingestion endpoint |
flushInterval | number | 5000 | Milliseconds between automatic queue flushes |
flushSize | number | 10 | Number of queued events that trigger an immediate flush |
debug | boolean | false | Enable [Synapse] console logging |
Identify Users
Call after login to associate events with a known user.
The identify call:
- Persists the user ID in
localStorageso subsequenttrackcalls include it automatically - Sends a
$identifyevent with the provided traits - Sets
contact_overrideswithemail,first_name,last_name, andphonefields (when provided) to upsert the contact server-side
Track Events
Events are queued in memory and flushed in batches to POST /v1/events/batch.
Page Views
Page views are tracked as $pageview events with automatic capture of url, path, title, and referrer.
Reset (On Logout)
Clears the identified user, generates a new anonymous ID, and removes the stored user ID from localStorage. Call this when a user logs out to prevent events from being attributed to the wrong user.
How It Works
- Events are queued in memory and persisted to
localStorage - The queue flushes every 5 seconds or when 10 events accumulate (configurable)
- Events are sent as a batch to
POST /v1/events/batch - On page unload,
sendBeaconensures queued events are delivered - Failed flushes retry with exponential backoff (1s, 2s, 4s, up to 3 retries)
- The queue survives page refreshes via
localStorage(max 500 events)
Anonymous Tracking
Before identify is called, events are associated with an auto-generated anonymous ID stored in localStorage. After identify, the user ID replaces the anonymous ID. The anonymous ID is always included as _anonymous_id in event attributes for server-side identity stitching.
Data Flow
Full Example
A complete integration for a SaaS application.
React / Next.js Integration
Tracking Hook
Next.js Script Loading
Create /public/scripts/synapse-init.js with your init snippet:
TypeScript Types
The SDK exports TypeScript types for configuration and events.
Limits and Constraints
| Constraint | Value |
|---|---|
| Max queue size | 500 events |
| Max batch size per flush | 50 events |
| Max retries on failure | 3 |
| Default flush interval | 5 seconds |
| Default flush threshold | 10 events |
Method Reference
| Command | Arguments | Description |
|---|---|---|
synapse('init', config) | SynapseConfig | Initialize the SDK with your API key |
synapse('identify', userId, traits) | string, object? | Associate events with a known user |
synapse('track', eventName, props) | string, object? | Track a custom event |
synapse('page', props) | object? | Track a page view with automatic URL capture |
synapse('reset') | none | Clear user identity and generate new anonymous ID |
Related Resources
- Node.js Server SDK -- server-side integration with full API access
- Event Ingestion API -- raw REST API for event tracking
- Event Schema -- payload structure and validation rules
- API Keys & Scopes -- understanding API key permissions