Identify users
Tie a session to a real account so you can tell who hit an error, follow one customer's journey, and split identified from anonymous traffic. One call does it.
Tie a session to an account #
Call amb('setUser', id) with your own stable account identifier once you know who the
visitor is. From that point on, every event the collector sends carries the user id alongside the
long-lived anonymous id.
// Call after a successful sign-in. The id is your own stable
// account identifier, a user UUID, not an email.
amb('setUser', 'user-123'); A SaaS example #
In a SaaS app you typically know the account on first render after authentication. Set the user there, and re-set it on every login so a shared device attributes each session to the right person.
// Wire it into your auth flow so every authenticated session
// carries the account id.
// On login (or on first load if the user is already signed in):
function onSignedIn(account) {
amb('setUser', account.id); // e.g. 'usr_8f2c'
}
// On logout, pass an empty string to anonymise the session again:
function onSignedOut() {
amb('setUser', '');
} The call queues if you make it before the loader resolves, so you can wire it directly into your bootstrap without guarding for readiness.
Sign out and anonymise #
On logout, call amb('setUser', '') with an empty string. The session keeps collecting, but
subsequent events are no longer tied to the account. This matters on shared machines, where the next
visitor should not inherit the previous user's identity.