tracker.setUser()
Update the user identity after initialization. Emits a session:end event for the previous identity and a session:start event for the new one, enabling the dashboard to reconstruct user sessions correctly.
typescript
tracker.setUser(
userId: string | null,
opts?: SetUserOptions
): voidParameters
userId
Type: string | null
The new user identifier.
- Pass a
stringto identify the user (e.g. after login). - Pass
nullto reset to an anonymous session-scoped ID (e.g. after logout).
opts (optional)
typescript
interface SetUserOptions {
attributes?: Record<string, unknown>
}opts.attributes
Arbitrary attributes associated with this user identity. Merged into the session:start event payload for this identity change.
Examples
typescript
import { tracker } from '@ndriadev/vite-plugin-monitor/client'
// After login
authService.onLogin(async (user) => {
tracker.setUser(user.id, {
attributes: {
plan: user.plan, // 'free' | 'pro' | 'enterprise'
role: user.role, // 'admin' | 'user'
orgId: user.orgId,
},
})
})
// After logout — resets to anonymous ID
authService.onLogout(() => {
tracker.setUser(null)
})Session Events Emitted
When setUser() is called:
session:endwithsource: 'userId-change'andpreviousUserId(the identity being closed)session:startwithsource: 'userId-change'and the newuserId
This creates a clean boundary in the event log between the two user identities.
