User Attributes are pieces of information you know about a user which could be demographics like age or gender, account-specific like plan, or even things like whether a user has seen a particular A/B test variation. It's up to you! It is basically a customer identity that you can reference across the customer’s whole lifetime.
Implementing Login/Logout
- It's important to set the User Attribute Unique ID when a user logs into your app.
- This is to merge the new user with the existing user, if any exists, and will help prevent creation of unnecessary/stale users.
- Setting the Unique ID is a critical piece to tie a user across devices and installs/uninstalls as well across all platforms (i.e. iOS, Android, Windows, The Web). Set the USER_ATTRIBUTE_UNIQUE_ID attribute as soon as the user is logged in. Unique ID can be something like an email ID, a username (unique), or a database ID or any Backend generated ID.
- Do not set this for the user who not logged in.
Login
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_APP_ID);
_moengagePlugin.initialise();
_moengagePlugin.setUniqueId("Unique ID");
Note: The following values are not allowed in the UniqueID field: "unknown", "guest", "null", "0", "1", "true", "false", "user_attribute_unique_id", "(empty)", "na", "n/a", "", "dummy_seller_code", "user_id", "id", "customer_id", "uid", "userid", "none", "-2", "-1", "2"
Logout
The application needs to notify the MoEngage SDK whenever the user is logged out of the application. To notify the SDK, call the API whenever the user is logged out of the application.
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.logout();
In case the application is registering for push token it should pass the new push token to MoEngage SDK after user logout. For more information about passing push tokens, refer to Push Configuration for Android SDK.
Updating User Attribute Unique Id
Use the method setAlias() to update the user attribute unique id instead of setUniqueId() with a different value. Using the method setUniqueId() with a new value creates unintended users in MoEngage.
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.setAlias("Updated Unique ID");
warning |
Critical Please make sure that you use |
Tracking User Attributes
The SDK provides APIs to track commonly tracked user attributes like First Name, Last Name, Email-Id, etc. Please use the provided methods for tracking these attributes.
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.setUserName("User Name");
_moengagePlugin.setFirstName("FirstName");
_moengagePlugin.setLastName("LastName");
_moengagePlugin.setEmail("EmailID");
_moengagePlugin.setPhoneNumber("PhoneNumber");
_moengagePlugin.setGender(MoEGender.male); // Supported values also include MoEGender.female OR MoEGender.other
_moengagePlugin.setLocation(new MoEGeoLocation(23.1, 21.2)); // Pass coordinates with MoEGeoLocation instance
_moengagePlugin.setBirthDate("2000-12-02T08:26:21.170Z"); // date format - ` yyyy-MM-dd'T'HH:mm:ss.fff'Z'`
Tracking Custom User Attributes:
For setting other User Attributes, use the generic method setUserAttribute(key, value).
Supported types for attribute value are String, int, double, num, bool, List<int>, List<String>, List<double> & List<num>.
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.setUserAttribute("int-attr", 0);
_moengagePlugin.setUserAttribute("bool-attr", true);
_moengagePlugin.setUserAttribute("string-attr", "Some Value");
_moengagePlugin.setUserAttribute("double-attr", 10.0);
_moengagePlugin.setUserAttribute("int-arr-attr", [100,200,300]);
_moengagePlugin.setUserAttribute("string-arr-attr", ["a","b","c"]);
_moengagePlugin.setUserAttribute("double-arr-attr", [1.0,2.0,3.0]);
_moengagePlugin.setUserAttribute('product', {'item-id' : 123,'item-type' : 'books','item-cost' : {'amount' : 100,'currency' : 'USD'}});
_moengagePlugin.setUserAttribute('products', [{'item-id' : 123,'item-cost' : {'amount' : 100,'currency' : 'USD'}},{'item-id' : 323,'item-cost' : {'amount' : 90,'currency' : 'USD'}}]);
For more information, please refer to the API documentation.
Tracking Date as user attributes:
To track any date as user attributes use the setUserAttributeIsoDate(name, date). This API takes the attribute name and ISO Date as input.
Date Format - yyyy-MM-dd'T'HH:mm:ss.fff'Z'
Example:
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.setUserAttributeIsoDate("timeStamp", "2019-12-02T08:26:21.170Z")
Tracking Location as user attributes: (Not available for Web)
To track any location as user attributes use the setUserAttributeLocation()
. This API takes the attribute name and an instance of MoEGeoLocation for coordinates as input.
Example:
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_WORKSPACE_ID);
_moengagePlugin.initialise();
_moengagePlugin.setUserAttributeLocation("locationAttr", new MoEGeoLocation(72.8, 53.2));
info |
Note You can not use "moe_" as a prefix while naming events, event attributes, or user attributes. It is a system prefix and using it might result in periodic blacklisting without prior communication. |
Custom Boolean User Attribute
iOS (optional)
Starting from version 8.x.x of moengage_flutter, the default tracking for the custom boolean attribute will be changed to boolean(true/false) from double(0/1). To configure this, use AnalyticsConfig with shouldTrackUserAttributeBooleanAsNumber and pass true to track the boolean as double. By default this is set as false to track boolean as true/false.
Refer to the example below
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_Workspace_ID, moEInitConfig: MoEInitConfig(analyticsConfig: AnalyticsConfig(shouldTrackUserAttributeBooleanAsNumber:true)));
@override
void initState() {
super.initState();
initPlatformState();
_moengagePlugin.initialise();
}
Refer to the example below for tracking. boolean user attribute
import 'package:moengage_flutter/moengage_flutter.dart';
final MoEngageFlutter _moengagePlugin = MoEngageFlutter(YOUR_Workspace_ID, moEInitConfig: MoEInitConfig(analyticsConfig: AnalyticsConfig(shouldTrackUserAttributeBooleanAsNumber: false)));
_moengagePlugin.initialise();
// If shouldTrackUserAttributeBooleanAsNumber is passed as true then `bool-attr-false` will tracked with value 0 else false
_moengagePlugin.setUserAttribute("bool-attr-false", false);
// If shouldTrackUserAttributeBooleanAsNumber is passed as true then `bool-attr-true` will tracked with value 1 else true
_moengagePlugin.setUserAttribute("bool-attr-true", true);
Reserved keywords for User Attributes
Below is the list of keys that should not be used when tracking user attributes.
- USER_ATTRIBUTE_UNIQUE_ID
- USER_ATTRIBUTE_USER_EMAIL
- USER_ATTRIBUTE_USER_MOBILE
- USER_ATTRIBUTE_USER_NAME
- USER_ATTRIBUTE_USER_GENDER
- USER_ATTRIBUTE_USER_FIRST_NAME
- USER_ATTRIBUTE_USER_LAST_NAME
- USER_ATTRIBUTE_USER_BDAY
- MOE_TIME_FORMAT
- MOE_TIME_TIMEZONE
- USER_ATTRIBUTE_NOTIFICATION_PREF
- USER_ATTRIBUTE_OLD_ID
- MOE_TIME_FORMAT
- MOE_TIME_TIMEZONE
- USER_ATTRIBUTE_DND_START_TIME
- USER_ATTRIBUTE_DND_END_TIME
- MOE_GAID
- MOE_ISLAT
- status