Storage and consent

Last updated @userreason/js 0.5.4
On this page

Your site controls when UserReason initializes and whether it persists data on the device. Storage mode controls persistence; it does not disable requests or erase server data.

Storage modes

Mode Visitor identity Session and event summaries
'local' (default) First-party localStorage localStorage on the current origin
'cookie' First-party cookie, optionally shared across subdomains localStorage on each origin
'memory' Memory for the current page Memory for the current page

Anonymous visitor tokens can persist for up to 400 days in persistent modes. Cookie storage uses SameSite=Lax, path /, and Secure on HTTPS. A cookieDomain, such as .example.com, shares the visitor token across matching subdomains; it does not make their localStorage shared.

A page reload in memory mode starts without a persisted anonymous token. An identified account can still be recognized by the server when you identify it again.

setStorage(mode): void

Initialize with storage: 'memory' when your consent flow permits requests but should not yet persist UserReason data. Call setStorage('local') or setStorage('cookie') after your app has obtained the relevant choice.

ts
import UserReason from '@userreason/js';

UserReason.init({ siteId: 'YOUR_SITE_ID', storage: 'memory' });

// Call from your application's consent-change handler.
export function applyUserReasonStorage(allowPersistence: boolean): void {
  UserReason.setStorage(allowPersistence ? 'local' : 'memory');
}

Replace the site ID before use. If your site's policy requires no UserReason requests before consent, defer init() itself until that decision.

Switching to memory removes persisted UserReason visitor, session and event summary data for this site from the device while retaining the current page's in-memory state. Switching back to persistent storage starts persistence and can recognize a returning visitor with a stored token. The method requires initialization and accepts only the three modes above.

Changing storage does not delete server responses or history, stop an active session, or revoke an approved management grant. reset() changes the current visitor; destroy() tears down the instance.

Data sent with managed requests

Alongside the IDs, traits, events and answers you explicitly supply, the SDK sends coarse context such as SDK version, language, timezone, viewport category, external referrer hostname, selected UTM attribution and optional appVersion. The server can derive context such as country, browser and device from the request.

Choose the traits and properties your integration needs. debug logs lifecycle and request status without printing visitor values or tokens.