identify, every later event carries the ID from your own system, and the
dashboard shows the events of one user as one journey.
Methods
Grain Tag
Grain Tag has one method. Call it after the user logs in.Analytics Web
Set the user ID
identify and setUserId do the same thing for a string. Use the one that reads better in your
code.
userId in the configuration. The first
event then carries it.
Log in and log out
login sets the user ID and the authentication in one call. logout reverses both.
After
logout, authStrategy is NONE, the authProvider is removed, and events carry an
anonymous ID. Read Authentication.
Read the ID
getUserId returns null for an anonymous visitor. getEffectiveUserId always returns a string.
Use it when you need the ID that Grain stores on the next event.
Anonymous IDs
When no user ID is set, Analytics Web generates one. The consent mode decides which kind.
The daily rotating ID is computed from the date, the tenant alias, and a random seed. Two visits
on different days get different IDs. Grain Tag uses the same two kinds of ID. Its
auto consent
mode is cookieless. Read Privacy and consent.
Events before identify keep their anonymous ID. Only the events after the call carry your user
ID.
Page loads
The SDK does not store your user ID across page loads. Your authentication system owns the session, so Grain does not keep a second copy. On every load, set the ID from your own session.Servers
On a server, one client serves many users. Set the user ID before each request, and flush the event inside the request.React
GrainProvider takes userId in its config. When the value changes, the provider updates the
client.
Rules for the client
- Use an ID that does not change: a database ID or a UUID. An email address changes, and a session ID changes on every session.
- Set the user ID on every page load, before the first event. The SDK does not store it.
- If the user logs out, call
logout()orsetUserId(null). Otherwise the next user’s events go to the previous user. - Do not switch between many user IDs from one client. Grain rate-limits a source that sends many distinct IDs. Read Security.
- With
JWTauthentication, the user ID must equal the subject of the token. The API rejects an event with a different ID. - On a server, set the user ID before each request, or create one client per request.