Identifying Users and Creating a User (For Automation Service)
After initializing the SDK, call authorizeUser to identify the user currently interacting with your website. This associates subsequent SDK data with that user and creates or retrieves the user in Metrix User Lookup.
import { authorizeUser, init } from '@metrixorg/websdk';
init('APP_ID', 'API_KEY');
authorizeUser('CUSTOM_USER_ID');authorizeUser(username: string): void| Name | Type | Description | Required |
|---|---|---|---|
username | string | A stable, unique identifier assigned to the user on your website. | Yes |
Call authorizeUser after init and before sending user or event data that should belong to the identified user. The SDK uses the identifier to associate data with the user; it does not need to be a display name.
To return the SDK to an anonymous state during logout, call:
import { deauthorizeUser } from '@metrixorg/websdk';
deauthorizeUser();deauthorizeUser clears the current user identity and stored user profile data from the SDK.
Updating User Information
After calling authorizeUser, use the following methods to update the user profile in Metrix User Lookup. Each setter accepts an optional callback that runs after the update succeeds.
setFirstName(firstName: string) : voidsetLastName(lastName: string) : voidsetEmail(email: string) : voidsetHashedEmail(hashedEmail: string) : voidsetPhoneNumber(phoneNumber: string) : voidsetHashedPhoneNumber(hashedPhoneNumber: string) : voidsetCountry(country: string) : voidsetCity(city: string) : voidsetRegion(region: string) : voidsetLocality(locality: string) : voidsetGender(gender: string) : voidsetBirthday(birthday: string) : voidsetCustomAttribute(key: string, value: string) : voidExample:
import {
setFirstName,
setEmail,
setGender,
setBirthday,
setCustomAttribute,
} from '@metrixorg/websdk';
setFirstName('Ali');
setEmail('ali@example.com');
setGender('male');
setBirthday(Date.parse('1990-05-20'));
setCustomAttribute('plan', 'premium');Use setHashedEmail and setHashedPhoneNumber when your integration requires hashed contact data. Do not send sensitive values as custom attributes unless your data policy allows it.
setFcmToken stores an FCM token for integrations that use Firebase Cloud Messaging. Web push subscriptions are configured separately through the Web Push API.
Retrieving User IDs
Metrix generates a metrixUserId and, for identified users, an automationUserId. Both values are available through promises after the corresponding user has been created on Metrix servers.
Metrix user ID
import { onMetrixUserIdReceived } from '@metrixorg/websdk';
onMetrixUserIdReceived().then((metrixUserId: string) => {
console.log(metrixUserId);
});Automation user ID
import { onAutomationUserIdReceived } from '@metrixorg/websdk';
authorizeUser('CUSTOM_USER_ID');
onAutomationUserIdReceived().then((automationUserId: string) => {
console.log(automationUserId);
});onMetrixUserIdReceived(): Promise<string>
onAutomationUserIdReceived(): Promise<string>These promises remain pending until the SDK receives the ID. In most cases, you do not need to retrieve or store either ID unless another integration requires it.
API Summary
| Method | Description |
|---|---|
authorizeUser(username) | Identifies the current user. |
deauthorizeUser() | Removes the current user identity and clears stored profile data. |
setFirstName(value) | Sets the user’s first name. |
setLastName(value) | Sets the user’s last name. |
setEmail(value) | Sets the user’s email address. |
setHashedEmail(value) | Sets the user’s hashed email address. |
setPhoneNumber(value) | Sets the user’s phone number. |
setHashedPhoneNumber(value) | Sets the user’s hashed phone number. |
setCountry(value) | Sets the user’s country. |
setCity(value) | Sets the user’s city. |
setRegion(value) | Sets the user’s region. |
setLocality(value) | Sets the user’s locality. |
setGender(value) | Sets the user’s gender to male, female, or other. |
setBirthday(value) | Sets the user’s birthday as a number, typically a timestamp. |
setFcmToken(value) | Sets the user’s FCM token. |
setCustomAttribute(key, value) | Sets a custom user attribute. |
onMetrixUserIdReceived() | Resolves with the Metrix user ID. |
onAutomationUserIdReceived() | Resolves with the automation user ID. |