Identity & sessions
How createIdentity manages anonymous ids, rolling session ids, and known user identity - and how they auto-fill analytics events.
createIdentity gives every visitor a stable anonymous id and a rolling session id. Those ids are picked up automatically by createAnalytics, so every event carries consistent identity without you managing it per-event. When a visitor is identified (for example after sign-in), subsequent events carry the known user id instead.
createIdentity
import { createIdentity } from '@kelpclick/client';
const identity = createIdentity({
sessionTtlMs: 30 * 60 * 1000, // rolling session timeout, default 30 minutes
keyPrefix: 'kelp', // storage key prefix, default 'kelp'
});Options
| Option | Default | Notes |
|---|---|---|
sessionTtlMs | 1800000 (30 min) | A new session id is issued after this period of inactivity. Every call to getSessionId() refreshes the timer. |
keyPrefix | 'kelp' | Prefix for all localStorage keys. Set a unique value per tenant when multiple Kelp tenants share a browser origin. |
storage | localStorage adapter | Override with any KelpStorage implementation. Falls back to in-memory storage in SSR and privacy mode. |
Reading ids
identity.getAnonymousId(); // string - stable per-browser anonymous id (e.g. 'anon_1a3b...')
identity.getSessionId(); // string - rolling session id; refreshes on each call
identity.getUserId(); // string | null - the identified user id, or nullgetSessionId() refreshes the activity timer on every call - calling it is enough to keep a session alive.
Identifying a user
Call identify to record a known user after sign-in:
identity.identify({ userId: 'user_123' });From this point, getUserId() returns 'user_123' and createAnalytics stamps that value on every subsequent event. Email subscribe also calls identity.identify({ email }) so later events link to the contact.
To clear a known user (e.g. on sign-out) without resetting the anonymous id:
identity.reset();SSR-safe storage
createIdentity falls back to in-memory storage when localStorage is not available - on the server, or in a browser privacy mode that blocks it. This means:
- On the server,
getAnonymousId()andgetSessionId()are stable within a request but not persisted across requests. - On the client, ids are written to
localStorageand persist across page loads.
If you need cross-request stability server-side (e.g. for cookie-based identity), implement KelpStorage and pass it as storage:
import { createIdentity, type KelpStorage } from '@kelpclick/client';
const cookieStorage: KelpStorage = {
get: (k) => readCookie(k),
set: (k, v) => writeCookie(k, v),
remove: (k) => deleteCookie(k),
};
const identity = createIdentity({ storage: cookieStorage });How identity auto-fills analytics
When you pass identity to createAnalytics, the tracker reads from it on every event:
| Analytics field | Source |
|---|---|
sessionId | identity.getSessionId() |
userId | identity.getUserId() if set; otherwise identity.getAnonymousId() (the anon_-prefixed stable id) |
You never need to pass sessionId or userId to individual tracker calls - they are filled for you.
The anon_-prefixed id is a stable, opaque identifier that is distinct from any real user id. It lets you track anonymous visitor journeys consistently without confusing them with identified users.
Security note
Anonymous and session ids are stored in localStorage in cleartext. They are not authentication credentials, but protect your origin against XSS with a strict Content-Security-Policy to prevent a compromised script from reading them.
Related
- Catalogue personalisation - per-shopper browse/search order (beta);
userId,sessionId,personalize - Merchandising - merchant-wide pin, boost, bury, hide (wins over personalisation)
- Analytics overview - how the tracker picks up identity automatically
- Email capture -
subscribecallsidentity.identify - Configuration -
tenantCodeand per-tenant key isolation