Web SDK User Attributes Tracking
info

Note

MoEngage highly recommends using the optional step for MoEngage Web SDK integration.

User attributes are specific traits of a user such as an email, username, mobile, gender and so on. User Attributes helps target users based on these attributes across devices or installs or to personalize the messages.

Pre-defined Attributes

Use the following helper functions to make use of the MoEngage reserved user attributes.

JavaScript
Moengage.add_first_name("Dominick");
Moengage.add_last_name("Cobb");
Moengage.add_email("dom@level5.com");
Moengage.add_mobile("+12399999999");
Moengage.add_user_name("Dominick (Dom) Cobb"); // Full name for user
Moengage.add_gender("M");
Moengage.add_birthday(new Date(1980, 2, 31));

Custom Attributes

JavaScript
// string
Moengage.add_user_attribute("ATTRIBUTE_NAME_1", "value");
// Integer - Numeric
Moengage.add_user_attribute("ATTRIBUTE_NAME_2", 1);
// Double - Numeric
Moengage.add_user_attribute("ATTRIBUTE_NAME_3", 5.99);
// Date
Moengage.add_user_attribute("ATTRIBUTE_NAME_4", new Date(2021, 2, 10));
// Boolean
Moengage.add_user_attribute("ATTRIBUTE_NAME_5", false);
// Arrays can be only in string or in number format.
// Array (all items should be of same data type)
Moengage.add_user_attribute('colors', ['blue', 'green', 'red']);
// Array (all items should be of same data type)
Moengage.add_user_attribute('ids', [77892, 1123, 3311]);

User Login and Logout

UNIQUE_ID is used to uniquely identify a user within the MoEngage dashboard.

Ensure log in and log out of users are implemented correctly during the visit to your website and users are authenticated. 

warning

Warning

If the user log in in and log out is not handled correctly, user data may get corrupted.

When you go live with MoEngage web SDK for the first time, ensure that you are setting the unique id of your existing website users on page load.

As soon as the user is authenticated, ensure that the user id is passed on to the MoEngage SDK using the login method. After the user logs out of your app, ensure to call the logout method of MoEngage. If two different unique id information are passed without calling the logout method in between, previous user information is overwritten by the new user.

Use the following function on user login, user log out and update user.

Log In

Use this function to create and log in a user.

JavaScript
Moengage.add_unique_user_id(UNIQUE_ID); // UNIQUE_ID is used to uniquely identify a user.

Log Out

Use this function to forcefully log out the current user.

JavaScript
Moengage.destroy_session();

Update User

Use this function to update the Unique ID of the user.

JavaScript
Moengage.update_unique_user_id(NEW_UNIQUE_ID);
warning

Warning - Unique ID Restrictions

  • Using Moengage.add_unique_user_id(NEW_UNIQUE_ID);:
    • Creates a new unique id for the user and this user is added as a new user
    • If you do not use log out and use Moengage.add_unique_user_id(NEW_UNIQUE_ID); the current user is forcefully logged out and a new user with a new unique id is created.
    • New user creation resets the current user and creates unintended users in our system.
  • Use Moengage.update_unique_user_id(NEW_UNIQUE_ID); to update the existing user Unique ID User Attribute.
warning

Critical

All the three methods mentioned above add_unique_user_id, destroy_session and update_unique_user_id should be called in proper order. So make sure, you track other attributes after these methods are executed completely. Since it returns a promise, you can use. For example:

JavaScript
Moengage.add_unique_user_id("123").then(() => {
  Moengage.add_email("abc@xyz.com")
})

Track User Attributes

Ensure that you are tracking user attributes in the following cases:

  1. When a new attribute is set for a user.
    For example, set the Email Id or Mobile No. attribute for the user after a user logs in or signs up on the website.
  2. When the value of an existing attribute is updated.
  3. When you go live for the first time with MoEngage web SDK integration, ensure that you are passing the user attributes set for your existing website users after page load (if they are not already sent to MoEngage).
  4. For more information on supported data types and data tracking policies, please refer to Data Tracking Policies.
info

Note

Make sure that you are not using a single unique id for all the users, this is possible only when the unique id value is hardcoded, instead of retrieving from your servers.

Attribute tracking via Google Tag Manager (GTM)

You can follow the documentation at Google Tag Manager(GTM) to track attribute via GTM.

Tracking at the time of Redirection

In case of redirection to another page immediately after tracking attribute or events, make sure that the tracking is completed and then you redirect.

JavaScript
Moengage.add_unique_user_id("123").then(() => {
window.open('https://www.moengage.com')
})

If you cannot wait for the tracking to complete and need to redirect immediately, then it is suggested to track the data on the next page after redirection.

 

User Attribute Caching

Duplicate User Attributes won't be tracked for 6 hours.

 


For more information about the default attributes collected by MoEngage SDK, refer to Web SDK Data Collection.

Reach out to us directly from your MoEngage Dashbaord -> Need Help? -> Contact Support or send an email to support@moengage.com.

 

Previous

Next

Was this article helpful?
0 out of 2 found this helpful

How can we improve this article?