To set a default attribute for a user, call the getCurrentUser() method on your Braze instance to get a reference to the current user of your app. Then you can call methods to set a user attribute.
Braze provides predefined methods for setting the following user attributes within the BrazeUser class . For method specifications, refer to our KDoc .
First name
Last name
Country
Language
Date of birth
Email
Gender
Home city
Phone number
note:
All string values such as first name, last name, country, and home city are limited to 255 characters.
Custom user attributes
In addition to the default user attributes, Braze also allows you to define custom attributes using several different data types. For more information on each attribute’s segmentation option, see User data collection.
Braze.getInstance(context).getCurrentUser(newIValueCallback<BrazeUser>(){@OverridepublicvoidonSuccess(BrazeUserbrazeUser){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)}
To set a custom attribute with a long integer value:
Braze.getInstance(context).getCurrentUser(newIValueCallback<BrazeUser>(){@OverridepublicvoidonSuccess(BrazeUserbrazeUser){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.
Braze.getInstance(context).getCurrentUser(newIValueCallback<BrazeUser>(){@OverridepublicvoidonSuccess(BrazeUserbrazeUser){// Setting a custom attribute with an array valuebrazeUser.setCustomAttributeArray("your_attribute_key",testSetArray);// Adding to a custom attribute with an array valuebrazeUser.addToCustomAttributeArray("your_attribute_key","value_to_add");// Removing a value from an array type custom attributebrazeUser.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 valuebrazeUser.setCustomAttributeArray("your_attribute_key",testSetArray)// Adding to a custom attribute with an array valuebrazeUser.addToCustomAttributeArray("your_attribute_key","value_to_add")// Removing a value from an array type custom attributebrazeUser.removeFromCustomAttributeArray("your_attribute_key","value_to_remove")}
Unsetting a custom attribute
Custom attributes can also be unset using the following method:
You can also use our REST API to set or unset user attributes. For more information, refer to User Data Endpoints.
Setting 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.
To set user attributes, you need to set the appropriate field on the shared ABKUser object. The following is an example of setting the first name attribute:
The following attributes should be set on the Braze.User object:
firstName
lastName
email
dateOfBirth
country
language
homeCity
phone
gender
Custom user attributes
In addition to the default user attributes, Braze also allows you to define custom attributes using several different data types. For more information on each attribute’s segmentation option, see User data collection.
important:
Custom attribute values have a maximum length of 255 characters; longer values will be truncated. For more information, refer to Braze.User .
The maximum number of elements in custom attribute arrays defaults to 25. Arrays exceeding the maximum number of elements will be truncated to contain the maximum number of elements. The maximum for individual arrays can be increased to up to 100. If you would like this maximum increased, reach out to your customer service manager.
// Setting a custom attribute with an array valueAppDelegate.braze?.user.setCustomAttributeArray(key:"array_name",array:["value1","value2"])// Adding to a custom attribute with an array valueAppDelegate.braze?.user.addToCustomAttributeArray(key:"array_name",value:"value3")// Removing a value from an array type custom attributeAppDelegate.braze?.user.removeFromCustomAttributeArray(key:"array_name",value:"value2")
1
2
3
4
5
6
7
8
// Setting a custom attribute with an array value[AppDelegate.braze.usersetCustomAttributeArrayWithKey:@"array_name"array:@[@"value1",@"value2"]];// Adding to a custom attribute with an array value[AppDelegate.braze.useraddToCustomAttributeArrayWithKey:@"array_name"value:@"value3"];// Removing a value from an array type custom attribute[AppDelegate.braze.userremoveFromCustomAttributeArrayWithKey:@"array_name"value:@"value2"];// Removing an entire array and key[AppDelegate.braze.usersetCustomAttributeArrayWithKey:@"array_name"array:nil];
Incrementing or decrementing custom attributes
This code is an example of an incrementing custom attribute. You may increment the value of a custom attribute by any integer or long value:
You can also use our REST API to set or unset user attributes. For more information, refer to User Data Endpoints.
Setting user subscriptions
To set up a subscription for your users (either email or push), call the functions set(emailSubscriptionState:) or set(pushNotificationSubscriptionState:), respectively. Both of these functions take the enum type Braze.User.SubscriptionState as arguments. This type has three different states:
Subscription Status
Definition
optedIn
Subscribed, and explicitly opted in
subscribed
Subscribed, but not explicitly opted in
unsubscribed
Unsubscribed and/or explicitly opted out
Users who grant permission for an app to send them push notifications default to the status of optedIn as iOS requires an explicit opt-in.
Users will be set to subscribed automatically upon receipt of a valid email address; however, we suggest that you establish an explicit opt-in process and set this value to optedIn upon receipt of explicit consent from your user. Refer to Managing user subscriptions for more details.
To set a default attribute for a user, call the getCurrentUser() method on your Braze instance to get a reference to the current user of your app. Then you can call methods to set a user attribute.
Using Google Tag Manager, standard user attributes (such as a user’s first name), should be logged in the same manner as custom user attributes. Ensure the values you’re passing in for standard attributes match the expected format specified in the User class documentation.
For example, the gender attribute can accept any of the following as values: "m" | "f" | "o" | "u" | "n" | "p". Therefore to set a user’s gender as female, create a Custom HTML tag with the following content:
braze.getUser().setCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,YOUR_INT_VALUE);// Integer attributes may also be incremented using code like the followingbraze.getUser().incrementCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,THE_INTEGER_VALUE_BY_WHICH_YOU_WANT_TO_INCREMENT_THE_ATTRIBUTE);
To set a custom attribute with a date value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
braze.getUser().setCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,YOUR_DATE_VALUE);// This method will assign the current time to a custom attribute at the time the method is calledbraze.getUser().setCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,newDate());// This method will assign the date specified by secondsFromEpoch to a custom attributebraze.getUser().setCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,newDate(secondsFromEpoch*1000));
You can have up to 25 elements in custom attribute arrays. Individual arrays that are manually set (not automatically detected) for Data Type can be increased up to 100 in the Braze dashboard under Data Settings > Custom Attributes. If you want this maximum increased, contact your Braze account manager.
Arrays exceeding the maximum number of elements will be truncated to contain the maximum number of elements.
To set a custom attribute with an array value:
1
2
3
4
5
6
7
braze.getUser().setCustomUserAttribute(YOUR_ATTRIBUTE_KEY_STRING,YOUR_ARRAY_OF_STRINGS);// Adding a new element to a custom attribute with an array valuebraze.getUser().addToCustomAttributeArray(YOUR_ATTRIBUTE_KEY_STRING,"new string");// Removing an element from a custom attribute with an array valuebraze.getUser().removeFromCustomAttributeArray(YOUR_ATTRIBUTE_KEY_STRING,"value to be removed");
important:
Dates passed to Braze with this method must be JavaScript Date objects.
important:
Custom attribute keys and values can only have a maximum of 255 characters. For more information about valid custom attribute values, refer to the reference documentation .
Custom user attributes are not available due to a limitation in Google Tag Manager’s scripting language. To log custom attributes, create a Custom HTML tag with the following content:
1
2
3
4
5
<script>// Note: If using SDK version 3.x or below, use `window.appboy` instead of `window.braze`// Version 4 or greater should use `window.braze`window.braze.getUser().setCustomUserAttribute("attribute name","attribute value");</script>
important:
The GTM template does not support nested properties on events or purchases. You can use the preceding HTML to log any events or purchases that require nested properties.
Unsetting a custom attribute
Custom attributes can be unset by setting their value to null.
You can also use our REST API to set or unset user attributes. For more information, refer to User Data Endpoints.
Setting user subscriptions
To set up a subscription for your users (either email or push), call the functions setEmailNotificationSubscriptionType() or setPushNotificationSubscriptionType(), respectively. Both functions take the enum type braze.User.NotificationSubscriptionTypes as arguments. This type has three different states:
When a user is registered for push, the browser forces them to choose to allow or block notifications, and if they choose to allow push, they are set OPTED_IN by default.
To set user attributes, you need to call the appropriate method on the BrazeBinding object. The following is a list of built-in attributes that can be called using this method.
In addition to the default user attributes, Braze also allows you to define custom attributes using several different data types. For more information on each attribute’s segmentation option, see User data collection.
// Set Integer AttributeAppboyBinding.SetCustomUserAttribute("custom int attribute key",'integervalue');// Increment Integer AttributeAppboyBinding.IncrementCustomUserAttribute("key",increment(int))
AppboyBinding.SetCustomUserAttributeToNow("custom date attribute key");
1
AppboyBinding.SetCustomUserAttributeToSecondsFromEpoch("custom date attribute key",'integervalue');
note:
Dates passed to Braze must either be in the ISO 8601 format (such as 2013-07-16T19:20:30+01:00) or in the yyyy-MM-dd'T'HH:mm:ss:SSSZ format (such as2016-12-14T13:32:31.601-0800).
1
2
3
4
5
6
// Setting An ArrayAppboyBinding.SetCustomUserAttributeArray("key",array(List),sizeOfTheArray(int))// Adding to an ArrayAppboyBinding.AddToCustomUserAttributeArray("key","Attribute")// Removing an item from an ArrayAppboyBinding.RemoveFromCustomUserAttributeArray("key","Attribute")
important:
Custom attribute values have a maximum length of 255 characters; longer values will be truncated.
Unsetting custom attributes
To unset a custom user attribute, use the following method:
Both functions take Appboy.Models.AppboyNotificationSubscriptionType as arguments, which 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
note:
No explicit opt-in is required by Windows to send users push notifications. When a user is registered for push, they are set to SUBSCRIBED rather than OPTED_IN by default. To learn more, check out our documentation on implementing subscriptions and explicit opt-ins.
Subscription Type
Description
EmailNotificationSubscriptionType
Users will be set to SUBSCRIBED automatically upon receipt of a valid email address. However, we suggest that you establish an explicit opt-in process and set this value to OPTED_IN upon receipt of explicit consent from your user. Visit our Changing User Subscriptions doc for more details.
PushNotificationSubscriptionType
Users will be set to SUBSCRIBED automatically upon valid push registration. However, we suggest that you establish an explicit opt-in process and set this value to OPTED_IN upon receipt of explicit consent from your user. Visit our Changing User Subscriptions doc for more details.
note:
These types fall under Appboy.Models.AppboyNotificationSubscriptionType.
To set a default attribute for a user, call the GetCurrentUser() method on the shared UBrazeUser object to get a reference to the current user of your app. Then you can call methods to set a user attribute.
The following attributes should be set on the UBrazeUser object:
SetFirstName
SetLastName
SetEmail
SetDateOfBirth
SetCountry
SetLanguage
SetHomeCity
SetPhoneNumber
SetGender
Custom User Attributes
In addition to the default user attributes, Braze also allows you to define custom attributes using several different data types. For more information on each attribute’s segmentation option, see User data collection.
// Setting a custom attribute with an array valueTArray<FString>Values={TEXT("value1"),TEXT("value2")};UBrazeUser->SetCustomAttributeArray(TEXT("array_name"),Values);// Adding to a custom attribute with an array valueUBrazeUser->AddToCustomAttributeArray(TEXT("array_name"),TEXT("value3"));// Removing a value from an array type custom attributeUBrazeUser->RemoveFromCustomAttributeArray(TEXT("array_name"),TEXT("value2"));
important:
Custom attribute values have a maximum length of 255 characters; longer values will be truncated.
Setting user subscriptions
To set up an email or push subscription for your users, you can use the following methods.