Skip to content

SDK API

The same API is available on window.cosembra (loader snippet) and as cosembra from @cosembra/sdk (npm).

cosembra.init(siteKey: string, options: InitOptions): void

Starts the SDK. Call it once per page load, before any other call. A second call is ignored.

Option Type Default Description
endpoint string required Full URL of the ingest endpoint: https://events.cosembra.com/v1/batch
flushAt number 20 Send when this many events are queued
flushIntervalMs number 5000 Send at least this often while events are queued
autoPageviews boolean false Send page_view on load and on route changes. See Single-page apps.
requireConsent boolean false Store and send nothing until consent(true). See Consent and privacy.
debug boolean false Log to the console. See Troubleshooting.
cosembra.track(name: string, properties?: object): string | undefined

Records an event. Returns the event’s id, or undefined if the event was dropped (invalid name, properties that can’t be serialized, consent denied, or not initialized). With the loader snippet, calls made before the SDK loads also return undefined.

See Tracking events.

cosembra.page(properties?: object): string | undefined

Records a page_view. Same as track('page_view', properties).

cosembra.identify(userId: string): void

Links this and later events to your user id. Call it on login. Ids longer than 256 characters are cut. See Identifying users.

cosembra.reset(): void

Forgets the user id and registered properties, and starts a new anonymous id and session. Call it on logout.

cosembra.register(properties: Record<string, string | number | boolean | null>): void
cosembra.unregister(key: string): void

Adds or removes properties attached to every later event. They’re kept across page loads. Up to 50 keys, with scalar values only.

cosembra.consent(granted: boolean): void

true allows storing and sending, and sends anything queued. false clears the queue and everything stored, and stops tracking.

cosembra.flush(): Promise<void>

Sends everything queued now. Safe to call any time. It resolves when the attempt finishes; events that failed stay queued for retry.

cosembra.debug(on?: boolean): void

Turns console logging on (default) or off.

Unless requireConsent is on and consent hasn’t been given, the SDK keeps these in localStorage:

  • an anonymous id and, after identify(), the user id
  • the current session id and the time of the last activity
  • properties set with register()
  • events waiting to be sent, up to 1,000

The SDK sets no cookies, and its requests carry no credentials.