Tracking User Attributes 7.x.x

User attributes are specific traits of a user, like an email, username, mobile, gender, etc.
This information is helpful in order to target users based on these attributes across devices or installs.



We track certain user attributes by default in our SDK, and also we have predefined attribute names for certain common user info tracked by these SDK methods. Make sure to use these and not track the same info with the different attribute names. Find the list of default and predefined user attributes tracked by SDK here.

User Login / Logout

It is important that you handle user login and logout as mentioned below. There is a definite possibility that your data gets corrupted if this is not done properly.

Make sure to get hold of a unique id for your app users and pass that information to our SDK using the setUserUniqueID: method. We use this unique id to identify a user and also to merge user profiles across installs and platforms.

And also, once the user logs out of your app, it is necessary to call resetUser method of SDK so that we create a new anonymous user and track the events following this to the new user's profile.

Kindly make sure that you call the following methods on user login/logout.


Swift Objective-C
MoEngage.sharedInstance().setUserUniqueID(UNIQUE_ID) // UNIQUE_ID is used to uniquely identify a user.


  • When you go live with MoEngage iOS SDK for the first time, please ensure that you are setting the Unique ID of the already logged-in user along with other user attributes.
  • Kindly make sure that you are not using a single unique id for all the users, this can happen if you hard code the value, instead of fetching it from your servers.
  • If you pass 2 different unique id information without calling resetUser method in between, the SDK will internally force logout the existing user.


Swift Objective-C

Updating User Attribute Unique ID



Please make sure that you use setAlias: for updating the User Attribute Unique ID and not setUserUniqueID: as calling setUserUniqueID: with a new value will reset the current user and lead to the creation of unintended users in our system.

In a scenario where you have to update the existing user's Unique ID value make use of setAlias: method as shown below with the updated Unique ID value:

Swift Objective-C

Default User Attributes

Some default SDK User Attributes can be set for eg. email-id, mobile number, gender, user name, birthday. The default attributes tracked by SDK can be set as shown below :

Swift Objective-C
MoEngage.sharedInstance().setUserGender(MALE) //Use UserGender enumerator for this
MoEngage.sharedInstance().setUserDateOfBirth(userBirthdate)//userBirthdate should be a Date instance
MoEngage.sharedInstance().setUserLocationLatitude(userLocationLat, andLongitude: userLocationLng) //userLocationLat and userLocationLng are double values of the location coordinates


We support strings and numbers as values for UserAttributes . NSURL is not supported. If you wish to use it, please convert them to string.

User Phone No / Mobile Number must be tracked as a string to work properly in MoEngage systems.

Custom User Attributes

To set custom attributes just provide custom keys different to the ones present in MoEHelperConstants. Following is an example :

Swift Objective-C
MoEngage.sharedInstance().setUserAttribute("Bengaluru", forKey: "Current_City")

User Attributes have the following restrictions :

  • User Attribute Names should not contain dot(.)
  • User Attribute Names should not start with dollar sign($)
  • User Attribute Values should be a String or Number

Date and Time User Attributes

Date and time attributes can be set as user attributes. For this refer to the methods in the code block below:

Swift Objective-C
//1. Track UserAttribute using Date instance
MoEngage.sharedInstance().setUserAttributeDate(Date(), forKey: "DateAttr1")

//2. Track UserAttribute using ISO Date String in format "yyyy-MM-dd'T'HH:mm:ss'Z'"
MoEngage.sharedInstance().setUserAttributeISODateString("2020-01-12T18:45:59Z", forKey: "DateAttr2")

//3. Track UserAttribute using Epoch value
MoEngage.sharedInstance().setUserAttributeTimestamp(NSDate().timeIntervalSince1970, forKey:"DateAttr3")

Location Attributes

The location of a user or any location can be set as a user attribute. For this use setUserAttributeLocationLatitude:longitude:forKey: method and pass lat, the long value of the location as shown in the following example :

Swift Objective-C
MoEngage.sharedInstance().setUserAttributeLocationLatitude(12.98798, longitude: 34.98789, forKey:"location_attribute_name")

Tracking User Locale

For tracking the locale settings of the user device, use trackLocale method as shown below:

Swift Objective-C
Was this article helpful?
0 out of 0 found this helpful