JavaScript SDK
Lightweight client-side tracking SDK for sending user events, identifying users, and tracking page views to the Synapse event ingestion API. Approximately 5.3 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 SDK is designed for browser environments. It uses localStorage for queue persistence and sendBeacon for reliable delivery on page unload.
Initialization
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey | string | required | Your Synapse API key |
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 - 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.
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.
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 |
The SDK sends the API key in request headers and query parameters (for sendBeacon). Use a data-scoped key to limit the blast radius. Never use a full or management scoped key in client-side code.
Server-Side Event Ingestion
For server-side event ingestion (Node.js, Python, etc.), use the REST API directly rather than this client-side SDK. See the API Reference and Python SDK for server-side integration patterns.