Identity & Login Integration
Device identity is automatic (see How Events Are Enriched). For a logged-in site, pair it with your own login system using b2mSetUserId — this is what lets B2Metric merge a visitor's activity across devices and keep A/B test assignment consistent per account.
// On successful login:
window.b2mSetUserId(String(user.id));
// On an explicit user-initiated logout:
window.b2mSetUserId(null);
Never call b2mSetUserId from code that runs on every page load or route change (e.g. a pattern like user?.id ?? null). The SDK already remembers the logged-in ID in its own storage and restores it automatically on reload — no boot-time call is needed. Calling it with null before your auth state has resolved briefly drops the visitor back to an anonymous identity and can fire experiment assignment on the wrong ID.
null, undefined, "", and the strings "undefined"/"null" are all treated as a logout; repeating the same value again is a no-op.
Optional Profile Properties
window.b2m.setUserProperties({ plan: 'pro' }); // sets/overwrites
window.b2m.setUserPropertiesOnce({ signup_date: '2026-01-01' });
// only if not already set
Reading the Current Device ID
window.b2mGetDeviceId();
// the device-level identifier, independent of login