Set User Attributes in iOS

Learn about setting user attributes in iOS

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.

The system limits user attributes to 4KB per user -- each key limited to 100B and each value limited to 800B. Exceeding characters are truncated. The system returns an error if an appUser the sum of all custom attributes exceeds the 4KB limit while an appUser is in the process of being created or updated.

We strongly recommend setting user attributes on your app user as directly after login. Any properties set prior to log in are set to an anonymous entity and are then 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.

BrandMessenger.login(userId, jwt: jwtString) { ( error:Error? , userInfo:[AnyHashable : Any]?) in
    var userProperties = [String : Any]()
    userProperties["scope"] = "appUser"
    userProperties["platform"] = "ios"
    userProperties["email"] = loggedInUser.email
    userProperties["kh_prop_1"] = "prop 1"

    let user = KBMUser.current()
    user?.firstName = loggedInUser.login
    user?.lastName = ""
    user?.signedUpAt = Date(timeIntervalSince1970: 1420070400)
    user?.addMetadata(userProperties)
}
[BrandMessenger login:userId jwt:jwtString completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable userInfo) {
    NSMutableDictionary* properties = [[NSMutableDictionary alloc] init];
    [properties setObject:@"appUser" forKey:@"scope"];
    [properties setObject:@"ios" forKey:@"platform"];
    [properties setObject:loggedInUser.email forKey:@"email"];
    [properties setObject:@"prop 1" forKey:@"kh_prop_1"];

  KBMUser* user = [KBMUser currentUser];
    [user setFirstName:loggedInUser.login]
    [user setLastName:@""];
    [user setSignedUpAt:[NSDate dateWithTimeIntervalSince1970:1420070400]];
    [user addProperties:properties];
}];

Metadata needs to also 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 anytime using the examples below. We recommend only doing this after a login and first attribute set has occurred.

var userProperties = [String : Any]()
userProperties["kh_prop_1"] = "prop 2"

let user = KBMUser.current()
user?.addMetadata(userProperties)
NSMutableDictionary* properties = [[NSMutableDictionary alloc] init];
[properties setObject:@"prop 2" forKey:@"kh_prop_1"];

KBMUser* user = [KBMUser currentUser];
[user addProperties:properties];