User Hook
Updating the user ID
For signed in experiences, user ids should be provided to Frigade. If you don’t provide a user ID, Frigade will generate a guest user id for you.
User IDs can be provided at time of render in the <FrigadeProvider />
component or at any time using the setUserId
method:
import { useUser } from '@frigade/react';
const { setUserId } = useUser();
setUserId('1234');
<FrigadeProvider />
If you want to set properties in the same call, you can do so by using the setUserIdWithProperties
method:
import { useUser } from '@frigade/react';
const { setUserIdWithProperties } = useUser();
setUserIdWithProperties('1234', {
name: 'Christian Mathiesen',
email: 'chris@frigade.com'
});
Adding user properties
import { useUser } from '@frigade/react';
const { addPropertiesToUser } = useUser();
addPropertiesToUser({
name: 'Christian Mathiesen',
email: 'christian@frigade.com'
});
Tracking events for the user
import { useUser } from '@frigade/react';
const { trackEventForUser } = useUser();
trackEventForUser('user-registered', {
name: 'Christian Mathiesen'
});
Standardized fields
When setting user properties, Frigade automatically detects and stores the following fields on the user:
firstName
lastName
email
organization
Linking a guest session to a user
Frigade will generate a guest user ID for you if you do not provide a user id. This ID is stored in the user’s browser using localStorage
.
This allows unauthenticated users to have state in Frigade Flows and to be able to continue their session when they return to your application.
Often it is useful to link a guest session to a user when they register or log in and transfer all state and data. The linkExistingGuestSessionToUser(userId: string)
method enable this functionality for you:
import { useUser } from '@frigade/react';
const { linkExistingGuestSessionToUser } = useUser();
// This will merge all state from the guest session to the user with the ID '1234'
linkExistingGuestSessionToUser('1234');