Consent and lifecycle
Control collection, subscribe to consent changes, and understand session teardown.
@askdepth/corev0.3.0On this page
Consent defaults to unknown. Initialize once, then connect your consent interface to setConsent. The SDK provides collection controls; your application determines the appropriate purposes and visitor choices.
Consent states
| State | Behavior |
|---|---|
'unknown' | Collection is inactive; explicit events are dropped. |
'denied' | Collection is inactive; explicit events are dropped. |
'granted' | Collectors start if the runtime was sampled in and configuration is valid. |
import {
Askdepth,
init,
setConsent,
revokeConsent,
} from '@askdepth/core';
init({
writeKey: 'YOUR_BROWSER_WRITE_KEY',
endpoint: 'https://YOUR_COLLECTOR/v1/telemetry',
consent: 'unknown',
});
const unsubscribe = Askdepth.onConsentChange((state) => {
console.log('Collection consent changed:', state);
});
// Call from the corresponding choices in your consent interface.
setConsent('granted');
revokeConsent(); // Equivalent to setConsent('denied').
// Remove the observer when its owner is disposed.
unsubscribe();The sequential calls illustrate the API; an application should call each method only when the corresponding visitor choice occurs.
What revocation clears
Changing to denied or unknown removes core interaction listeners, stops replay, removes the SDK's tracing wrappers where possible, cancels dead-click checks, clears queued and retry data, and sets the session ID to null.
Requests already sent can still reach the collector. Revocation does not delete previously collected server data or promise cancellation of every in-flight upload. Handle any data deletion request through your application's data lifecycle process.
Granting consent again starts a new session when sampled in. Changing consent does not reroll sampling. Consent choices are not persisted by the SDK; restore them from your application's consent store after a page load.
Subscribe to transitions
Askdepth.onConsentChange(listener) returns () => void. It is an object method, not a named package export in 0.3.0. It runs listeners when setConsent changes an existing runtime's state. It does not immediately replay the current state, and it is not an observer for all init reconfiguration. Listener errors do not interrupt collection.
Readiness is separate from consent
isInitialized() indicates that collectors are listening. It is false during server rendering, before initialization, without consent, for sampled-out runtimes, and after a collector kill signal.
getSessionId() returns the runtime's session ID or null. A runtime initialized with granted consent can have an ID even when sampling excludes it, so a non-null session ID alone does not mean events are being collected.
There is no general-purpose public disposal API in this version. Keep a single application-level runtime and use consent changes for the visitor's collection choice. React provider cleanup removes adapter listeners but does not shut down the core singleton.
