Set User Attributes in Android

Learn about setting user attributes in Android

User attributes, also referred to as profile properties and custom properties, are a set of key/value pairs that are specific to your application domain.

The system stores these key/value pairs under the properties field of an appUser. The attributes may have values of type Number, String, or Boolean.

We strongly recommend setting user attributes on your app user as directly after login. Any properties set before logging in are set to an anonymous entity and merged after the login has occurred. This merging may introduce undesired results.

Key attributes to set include first name, last name, and email address.

Attributes are listed under userProperties and then to KBMuser.current() after they have been defined. In our example, we've laid out our recommended order and method of introducing these attributes.

BrandMessengerManager.login(context, "access_token", new KBMLoginHandler() {
    @Override
    public void onSuccess(@NonNull RegistrationResponse registrationResponse, @Nullable Context context) {
        User user = new User();
        Map<String, String> userMetadata= new HashMap<>();
        userMetadata.put("platform", "android");
        user.setMetadata(userMetadata);
        UserService.getInstance(context).updateUser(user, new KBMCallback() {
        @Override
        public void onSuccess(Object response) {
        // author properties updated successfully
        }

       @Override
       public void onError(Object error) {
        // author properties update failed
       }
    });
    }

    @Override
    public void onFailure(@Nullable RegistrationResponse registrationResponse, @Nullable Exception exception) {
    }
})

Metadata also needs to be defined in Metadata Visibility. You can find this within the Admin by navigating to Khoros Response > Admin and selecting Metadata Visibility in the sidebar.

1100

Then, simply scroll down to the Display Fields in Author Profile and add Brand Messenger if it's not already present. You can then define Metadata Fields and the display name they will appear within the conversation in Agent view.

Update Attributes Mid-session

In the event that user attributes need updating during a session, it can be achieved at any time using the example below. We recommend only doing this after login and first attribute set have occurred.

Map<String, Object> userProperties = new HashMap<>();
userProperties.put("kh_prop_2", "prop2");
user.addProperties(userProperties);