Skip to main content
Complete reference for @datalyr/api 1.3.1, which posts events to https://ingest.datalyr.com/track from a server.

Requirements

Run this SDK on Node 18 or later. It calls global fetch and AbortController, which Node 16 and earlier don’t provide.

Install

Initialize

The constructor throws Error('Datalyr API key is required') on a missing key. This is the only throw in the SDK. A key that does not begin with dk_ logs a warning and continues. The SDK reads no environment variable. Pass the key yourself.
Use the object form when you want failure callbacks. The string shorthand new Datalyr('dk_...') accepts no onError or onDrop.

Authentication

The SDK sends no workspace ID. The key alone selects the workspace.

Track an event

timestamp accepts an ISO 8601 string, a Date, epoch seconds, or epoch milliseconds.

Identity

Pass userId, anonymousId, or both on every call. With neither, the SDK mints a fresh anon_<uuid> for that one call. Every such call becomes a separate person with no journey.
Take anonymousId from the browser. Call datalyr.getVisitorId() in the Web SDK and send the value to your server.

What goes on the wire

The SDK posts one event object per request. There is no envelope and no events array.
This SDK sends no distinct_id and no session_id. It has no session concept.

Events the SDK sends for you

group() records a plain $group event. Datalyr builds no account or group association from it. Do not use it to model organizations.

Idempotency

Pass the source event ID as eventId for every webhook.
An eventId over 256 characters is replaced with its first 191 characters, a hyphen, and a SHA-256 hex digest of the original.

Complete method list

PurchaseProperties requires a finite value: number. currency is optional and uppercased.
getAnonymousId() mints a new anon_<uuid> on every call and stores nothing. Never call it to look up an existing person. Read datalyr.getVisitorId() in the browser instead.

Shut down cleanly

close() drains the queue within closeTimeout, then stops the timer. After close(), track() logs an error and drops the event with reason closed. flush() returns immediately when the queue is empty. When a drain is already running, it awaits that drain rather than starting a second one.

Configuration

Every numeric option is clamped. A value outside the range becomes the nearest bound. host carries its own /track path. An override must include a full path.

Limits

Error behavior

Only the constructor throws. Every other method resolves. An event that survives its retries returns to the front of the queue. After 10 failed flush cycles the SDK drops it.

Drop reasons

onDrop receives one of these six values. Validation failures log a warning and call onDrop. They never throw.

Serverless functions

Call flush() at the end of each invocation. Call close() only when the process exits. A closed instance accepts no more events.

Verify the install

  1. Send one event with a userId you can recognize.
  2. Open Events in Datalyr.
  3. Confirm the event arrives within 30 seconds.
If nothing arrives, add an onDrop callback and log the reason. A permanent_client_error means the API key is wrong. See No events.