Install
Initialize
Track an event
eventId for webhook events. If the source retries the webhook, Datalyr can recognize the duplicate.
Identify a user
Core methods
| Method | Use it to |
|---|---|
track(options) | Track a backend event. |
identify(userId, traits?) | Attach traits to a user. |
alias(newUserId, previousId?) | Link two IDs for the same user. |
page(userId, name?, properties?) | Track a server-rendered page view. |
trackPurchase(userId, properties) | Track revenue with a required finite value. |
flush() | Send queued events immediately. |
close() | Flush events and stop timers before shutdown. |
Shut down cleanly
close() in short-lived scripts, workers, and graceful shutdown handlers so queued events have time to send.
Configuration
| Option | Default | What it controls |
|---|---|---|
apiKey | Required | Workspace API key. |
flushAt | 20 | Events queued before an automatic send. |
flushInterval | 10000 | Automatic flush interval in milliseconds. |
timeout | 10000 | Request timeout in milliseconds. |
retryLimit | 3 | Retries during a send attempt. |
maxQueueSize | 1000 | Maximum queued events. |
closeTimeout | 30000 | Time allowed for close() to drain the queue. |
debug | false | Development logging. |
onError | — | Called for every delivery failure. |
onDrop | — | Called when events are permanently dropped. |
Event identity
Prefer the object form oftrack:
userId, anonymousId, or both. If you omit both, the SDK creates a new anonymous ID for that call, so separate calls will not form a continuous journey.
Idempotency and timestamps
For webhooks, pass the source event ID and original timestamp:timestamp accepts an ISO 8601 string, a Date, epoch seconds, or epoch milliseconds.
Identity methods
Track revenue
value must be a finite number. Currency values are normalized to uppercase.
Serverless functions
Useflush() at the end of an invocation when the SDK instance may be reused:
close() only when the process is shutting down. Once closed, the instance cannot accept more events.