changeUser

open override fun changeUser(userId: String?)

When you first start using Braze on a device, the user is considered "anonymous". You can use this method to optionally identify a user with a unique ID. These User IDs should be private and not easily obtained (e.g. not a plain email address or username). This enables the following:

If the same user is identified on another device, their user profile, usage history and event history will be shared across devices.

If your app is used by multiple people, you can assign each of them a unique identifier to track them separately. Only the most recent user on a particular device will receive push notifications and in-app messages.

When you request a user switch (which is any call to changeUser where the new user ID is not the same as the existing user ID), the current session for the previous user (anonymous or not) is automatically sealed and a new session is started. Similarly, following a call to changeUser, any events which fire are guaranteed to be for the new user -- if an in flight server request completes for the old user after the user switch no events will fire, so you do not need to worry about filtering out events from Braze for old users.

Additionally, if you identify a user which has never been identified on another device, the entire history of that user as an "anonymous" user on this device will be preserved and associated with the newly identified user. However, if you identify a user which has been identified in another app, any history which was already flushed to the server for the anonymous user on this device will become orphaned and will not be associated with any future users. These orphaned users are not considered in your user counts and will not be messaged.

As part of the user change process, if you switch from an identified user to another identified user, Braze attempts to flush the previous user's data with a background task. If that background task fails, Braze will not attempt to re-flush the previous user's data until the next time you switch back to that user ID, instead preferring to minimize network traffic and free up any resources being used by the previous user's operations.

When this method returns, all subsequent calls to your Braze instance will operate on the new user.

Note: Once you identify a user, you cannot revert to the "anonymous" user. The transition from anonymous to identified tracking is only allowed once because the initial anonymous user receives special treatment to allow for preservation of their history. As a result, we recommend against changing the user ID just because your app has entered a "logged out" state because it makes you unable to target the previously logged out user with re-engagement campaigns. If you anticipate multiple users on the same device, but only want to target one of them when your app is in a logged out state, we recommend separately keeping track of the user ID you want to target while logged out and switching back to that user ID as part of your app's logout process.

Note that an BrazeUser can be retrieved at any time using IBraze.getCurrentUser.

See also

Parameters

userId

An external user identifier. May not be longer than Constants.USER_ID_MAX_LENGTH_BYTES.


open override fun changeUser(userId: String?, sdkAuthSignature: String?)

Identical to .changeUser but provides an sdk auth signature for the user before any networking begins, even if the existing external id is used.

See also

.changeUser