Braze JavaScript SDK
    Preparing search index...

    Function changeUser

    • When a user first uses Braze on a device they are considered "anonymous". Use this method to identify a user with a unique ID, which 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 on the same browser by multiple people, you can assign each of them a unique identifier to track them separately. Only the most recent user on a particular browser 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 ended 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 be merged into the identified user. Any fields that already exist on the identified user will not be overwritten, and the merge is limited to the fields listed here.

      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.

      Parameters

      • userId: string

        A unique identifier for this user. Limit 997 bytes. These User IDs should be private and not easily guessable (e.g. not a plain email address or username).

      • Optionalsignature: string

        An encrypted signature to be used to authenticate the current user. You can update the signature using the [[setSdkAuthenticationSignature]] method. This signature will only have an effect if the enableSdkAuthentication initialization option is set to true.

      Returns Promise<void>

      • Returns a promise that the user change has been completed. You must wait for the promise to resolve before logging events or setting attributes or the events and attributes may not be associated with the correct user. The promise is rejected if the SDK is not initialized.