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): voidStarts 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 | undefinedRecords 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 | undefinedRecords a page_view. Same as track('page_view', properties).
identify
Section titled “identify”cosembra.identify(userId: string): voidLinks this and later events to your user id. Call it on login. Ids longer than 256 characters are cut. See Identifying users.
cosembra.reset(): voidForgets the user id and registered properties, and starts a new anonymous id and session. Call it on logout.
register / unregister
Section titled “register / unregister”cosembra.register(properties: Record<string, string | number | boolean | null>): voidcosembra.unregister(key: string): voidAdds or removes properties attached to every later event. They’re kept across page loads. Up to 50 keys, with scalar values only.
consent
Section titled “consent”cosembra.consent(granted: boolean): voidtrue 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): voidTurns console logging on (default) or off.
What’s stored on the device
Section titled “What’s stored on the device”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.