Setting user IDs
Learn how to set user IDs through the Braze SDK. These are unique identifiers that let you track users across devices and platforms, import their data through the user data API, and send targeted messages through the messaging API. If you don’t assign a unique ID to a user, Braze will assign them an anonymous ID instead—however, you won’t be able to use these features until you do.
For wrapper SDKs not listed, use the relevant native Android or Swift method instead.
About anonymous users
After you integrate the Braze SDK, users who launch your app for the first time will be considered “anonymous” until you call the changeUser
method and assign them an external_id
. Once assigned, you can’t make them anonymous again. However, if they uninstall and reinstall your app, they will become anonymous again until changeUser
is called.
If a previously-identified user starts a session on a new device, all of their anonymous activity will automatically sync to their existing profile after you call changeUser
on that device using their external_id
. This includes any attributes, events, or history collected during the session on the new device.
Setting a user ID
To set a user ID, call the changeUser()
method after the user initially log ins. IDs should be unique and follow our naming best practices.
If you’re hashing a unique identifier instead, be sure to normalize the input of your hashing function. For example, when hashing an email address, remove any leading or trailing spaces and account for localization.
1
Braze.getInstance(context).changeUser(YOUR_USER_ID_STRING);
1
Braze.getInstance(context).changeUser(YOUR_USER_ID_STRING)
1
AppDelegate.braze?.changeUser(userId: "YOUR_USER_ID")
1
[AppDelegate.braze changeUser:@"YOUR_USER_ID_STRING"];
For a standard Web SDK implementation, you can use the following method:
1
braze.changeUser(YOUR_USER_ID_STRING);
If you’d like to use Google Tag Manager instead, you can use the Change User tag type to call the changeUser
method. Use it whenever a user logs in or is otherwise identified with their unique external_id
identifier.
Be sure to enter the current user’s unique ID in the External User ID field, typically populated using a data layer variable sent by your website.
1
BrazePlugin.changeUser("YOUR_USER_ID");
1
m.Braze.setUserId(YOUR_USER_ID_STRING)
1
AppboyBinding.ChangeUser("YOUR_USER_ID_STRING");
1
UBraze->ChangeUser(TEXT("YOUR_USER_ID_STRING"));
Do not assign a static default ID or call changeUser()
when a user logs out. Doing so will prevent you from re-engaging any previously logged-in users on shared devices. Instead, keep track of all user IDs separately and ensure your app’s logout process allows for switching back to a previously logged-in user. When a new session starts, Braze will automatically refresh the data for the newly-active profile.
User aliases
How they work
Although anonymous users don’t have external_ids
, you can assign them a user alias instead. You should assign a user alias when you want to add other identifiers to the user but don’t know what their external_id
is (for example, they aren’t logged in). With user aliases, you also can:
- Use the Braze API to log events and attributes associated with anonymous users
- Use the External User ID is blank segmentation filter to target anonymous users in your messaging
Setting a user alias
A user alias consists of two parts: a name and a label. The name refers to the identifier itself, while the label refers to the type of identifier it belongs to. For example, if you have a user in a third-party customer support platform with the external ID 987654
, you can assign them an alias in Braze with the name 987654
and the label support_id
, so you can track them across platforms.
1
Braze.getInstance(context).getCurrentUser().addAlias(ALIAS_NAME, ALIAS_LABEL);
1
Braze.getInstance(context).currentUser?.addAlias(ALIAS_NAME, ALIAS_LABEL)
1
Appboy.sharedInstance()?.user.addAlias(ALIAS_NAME, ALIAS_LABEL)
1
[[Appboy sharedInstance].user addAlias:ALIAS_NAME withLabel:ALIAS_LABEL];
1
braze.getUser().addAlias(ALIAS_NAME, ALIAS_LABEL);
1
2
3
4
{
"alias_name" : (required, string),
"alias_label" : (required, string)
}
ID Naming best practices
We recommend that you create user IDs using the Universally Unique Identifier (UUID) standard, meaning they are 128-bit strings that are random and well distributed.
Alternatively, you can hash an existing unique identifier (such as a name or email address) to generate your user IDs instead. If you do so, be sure to implement SDK authentication, so you can prevent user impersonation.
While its essential that you correctly name your user IDs from the start, you can always rename them in the future using the /users/external_ids/rename
endpoint.
Recommended | Not Recommended |
---|---|
123e4567-e89b-12d3-a456-836199333115 | JonDoe829525552 |
8c0b3728-7fa7-4c68-a32e-12de1d3ed2d5 | [email protected] |
f0a9b506-3c5b-4d86-b16a-94fc4fc3f7b0 | CompanyName-1-2-19 |
2d9e96a1-8f15-4eaf-bf7b-eb8c34e25962 | jon-doe-1-2-19 |
Avoid sharing details about how you create user IDs, as this may expose your organization to malicious attacks or data removal.