iOS > Custom data

Attributes

Batch 1.5 introduces a new User module. In addition of overriding the language/region or setting a custom user ID, you can now assign attributes and tags to your users, allowing you to improve your Push targeting.

Custom attributes

IMPORTANT
- User IDs (email address, username, etc) must be managed using our custom user ID implementation.
- Region/language data must be managed using our custom region/language implementation.
- Never use an existing tagging plan. Read our guide on custom data before tagging your app.

Managing attributes

Before we get started on how to implement custom attributes, here are some rules you should know.

Naming

Attribute names are strings. They should be made of letters, numbers or underscores ([a-z0-9_]) and can't be longer than 30 characters (e.g. has_premium).

Values

Values must be any of the following types, or their native Swift equivalent:

NSString
Must not be longer than 64 characters and can be empty. For better results, you should make them upper/lowercase and trim the whitespaces.

NSNumber
- Anything bigger than a long long or a double will be rejected.
- Unsigned values will be rejected.
- Booleans are supported, but should be initialized with [NSNumber numberWithBool:<your value>] or @YES/@NO.

NSDate
Since timezones are not supported, this will typically represent UTC dates.

Using any unsupported type as a value (NSNull, NSObject, NSArray, NSDictionary for example) will NOT work. Be careful, as it may cause [editor save] to fail.

Methods

The custom attribute API is simple, and has only three methods:

BatchUserDataEditor *editor = [BatchUser editor];
[editor setAttribute:@26 forKey:@"age"]; // Set an attribute

[editor removeAttributeForKey:@"age"]; // Remove it
[editor setAttribute:nil forKey:@"age"]; // This also works

[editor clearAttributes]; // Removes all attributes

[editor save]; // Don't forget to save the changes
let editor = BatchUser.editor()
editor.setAttribute(26 as NSNumber, forKey:"age") // Set an attribute

editor.removeAttribute(forKey: "age") // Remove it
editor.setAttribute(nil, forKey:"age") // This also works

editor.clearAttributes() // Removes all attributes

editor.save() // Don't forget to save the changes

Changes made to a editor happen transactionally and won't be persisted until you call save.

Please test your implementation using our debug tool before releasing your app on the store. Make sure you're unwrapping your optionals!

Managing tag collections

You can attach multiple tags to different collections. Tags have some limitations:

  • They are strings.
  • Their size can't be greater than 64 characters and they can't be empty.

Collection names have the same limitations as attribute names.

Here are the available methods:

BatchUserDataEditor *editor = [BatchUser editor];
[editor addTag:@"has_bought" inCollection:@"actions"]; // Add a tag to the "actions" collection
[editor removeTag:@"has_bought" fromCollection:@"actions"]; // Remove it

[editor clearTagCollection:@"actions"]; // Removes all tags from that collection

[editor save]; // Don't forget to save the changes
let editor = BatchUser.editor()
editor.addTag("has_bought", inCollection: "actions") // Add a tag to the "actions" collection
editor.removeTag("has_bought", fromCollection: "actions") // Remove it

editor.clearTagCollection("actions") // Removes all tags from that collection

editor.save(); // Don't forget to save the changes