Setting custom attributes
Braze provides methods for assigning attributes to users. You’ll be able to filter and segment your users according to these attributes on the dashboard. This reference article shows how to set custom attributes in your Android or FireOS application.
Before implementation, be sure to review examples of the segmentation options afforded by custom events, custom attributes, and purchase events in our analytics overview, as well as our notes on event naming conventions.
Assigning user attributes
To assign attributes to your users, call the getCurrentUser()
method on your Braze instance to get a reference to the current user of your app. After you have a reference to the current user, you can call methods to set predefined or custom attributes.
Standard user attributes
Braze provides predefined methods for setting the following user attributes within the BrazeUser class. Refer to our KDoc for method specifications:
- First name
- Last name
- Country
- Language
- Date of birth
- Email
- Gender
- Home city
- Phone number
All string values such as first name, last name, country, and home city are limited to 255 characters.
Setting standard attribute value
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setFirstName("first_name");
}
}
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setFirstName("first_name")
}
|
Setting custom attribute values
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", "your_attribute_value");
}
}
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", "your_attribute_value")
}
|
1
2
3
4
5
6
7
8
9
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_INT_VALUE);
// Integer attributes may also be incremented using code like the following:
brazeUser.incrementCustomUserAttribute("your_attribute_key", YOUR_INCREMENT_VALUE);
}
}
|
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_INT_VALUE)
// Integer attributes may also be incremented using code like the following:
brazeUser.incrementCustomUserAttribute("your_attribute_key", YOUR_INCREMENT_VALUE)
}
|
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_BOOLEAN_VALUE);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_BOOLEAN_VALUE)
}
|
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_LONG_VALUE);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_LONG_VALUE)
}
|
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_FLOAT_VALUE);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_FLOAT_VALUE)
}
|
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_DOUBLE_VALUE);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_DOUBLE_VALUE)
}
|
1
2
3
4
5
6
7
8
9
10
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_DATE_VALUE);
// This method will assign the current time to a custom attribute at the time the method is called:
brazeUser.setCustomUserAttributeToNow("your_attribute_key");
// This method will assign the date specified by SECONDS_FROM_EPOCH to a custom attribute:
brazeUser.setCustomUserAttributeToSecondsFromEpoch("your_attribute_key", SECONDS_FROM_EPOCH);
}
});
|
1
2
3
4
5
6
7
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setCustomUserAttribute("your_attribute_key", YOUR_DATE_VALUE)
// This method will assign the current time to a custom attribute at the time the method is called:
brazeUser.setCustomUserAttributeToNow("your_attribute_key")
// This method will assign the date specified by SECONDS_FROM_EPOCH to a custom attribute:
brazeUser.setCustomUserAttributeToSecondsFromEpoch("your_attribute_key", SECONDS_FROM_EPOCH)
}
|
warning:
Dates passed to Braze with this method must either be in the ISO 8601 format (e.g 2013-07-16T19:20:30+01:00
) or in the yyyy-MM-dd'T'HH:mm:ss:SSSZ
format (e.g 2016-12-14T13:32:31.601-0800
).
The maximum number of elements in custom attribute arrays defaults to 25. The maximum for individual arrays can be increased to up to 100 in the Braze dashboard, under Data Settings > Custom Attributes. Arrays exceeding the maximum number of elements will be truncated to contain the maximum number of elements. For more information on custom attribute arrays and their behavior, see our documentation on Arrays.
1
2
3
4
5
6
7
8
9
10
11
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
// Setting a custom attribute with an array value
brazeUser.setCustomAttributeArray("your_attribute_key", testSetArray);
// Adding to a custom attribute with an array value
brazeUser.addToCustomAttributeArray("your_attribute_key", "value_to_add");
// Removing a value from an array type custom attribute
brazeUser.removeFromCustomAttributeArray("your_attribute_key", "value_to_remove");
}
});
|
1
2
3
4
5
6
7
8
| Braze.getInstance(context).getCurrentUser { brazeUser ->
// Setting a custom attribute with an array value
brazeUser.setCustomAttributeArray("your_attribute_key", testSetArray)
// Adding to a custom attribute with an array value
brazeUser.addToCustomAttributeArray("your_attribute_key", "value_to_add")
// Removing a value from an array type custom attribute
brazeUser.removeFromCustomAttributeArray("your_attribute_key", "value_to_remove")
}
|
Unsetting a custom attribute
Custom attributes can also be unset using the following method:
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.unsetCustomUserAttribute("your_attribute_key");
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.unsetCustomUserAttribute("your_attribute_key")
}
|
Custom attribute via the REST API
You can also use our REST API to set user attributes. To do so, refer to the User API documentation.
Setting up user subscriptions
To set up a subscription for your users (either email or push), call the functions setEmailNotificationSubscriptionType()
or setPushNotificationSubscriptionType()
, respectively. Both of these functions take the enum type NotificationSubscriptionType
as arguments. This type has three different states:
Subscription status |
Definition |
OPTED_IN |
Subscribed, and explicitly opted in |
SUBSCRIBED |
Subscribed, but not explicitly opted in |
UNSUBSCRIBED |
Unsubscribed and/or explicitly opted out |
important:
No explicit opt-in is required by Android to send users push notifications. When a user is registered for push, they are set to SUBSCRIBED
rather than OPTED_IN
by default. Refer to managing user subscriptions for more information on implementing subscriptions and explicit opt-ins.
Setting email subscriptions
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setEmailNotificationSubscriptionType(emailNotificationSubscriptionType);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setEmailNotificationSubscriptionType(emailNotificationSubscriptionType)
}
|
Setting push notification subscription
1
2
3
4
5
6
| Braze.getInstance(context).getCurrentUser(new IValueCallback<BrazeUser>() {
@Override
public void onSuccess(BrazeUser brazeUser) {
brazeUser.setPushNotificationSubscriptionType(pushNotificationSubscriptionType);
}
});
|
1
2
3
| Braze.getInstance(context).getCurrentUser { brazeUser ->
brazeUser.setPushNotificationSubscriptionType(pushNotificationSubscriptionType)
}
|