Menu

User Association API (Java)

SDK version should be >= v3.4.3

User Association

User Registration/Login

After calling the API, related binding events will be sent. The corresponding key and value will not be cached locally, and subsequent collected events will not include the bound ID information.

Java SDK

SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() .addIdentityProperty("$identity_mobile", "123") 		.addIdentityProperty("$identity_email", "a@a.com") .build(); sa.bind(identity);
JAVA

User Unbinding

Called when the user ID is unbound, the parameter "key" is the key to unbind, and the "value" is the corresponding unbound business ID.

After calling the API, related unbinding events will be sent.

Java SDK

SensorsAnalyticsIdentity identity = SensorsAnalyticsIdentity.builder() .addIdentityProperty("$identity_mobile", "123") .build(); sa.unbind(identity);
JAVA

User Attribute

To provide more accurate analysis services for specific audience, Sensors Analytics SDK can set user attributes, such as age, gender, etc. Users can use user attributes as filtering conditions or dimensions for multidimensional analysis in Retention Analysis, Distribution Analysis, etc.

Setting User Attributes

Set user attributes using the profileSetById() API:

String distinctId = "ABCDEF123456789"; IDMUserRecord userRecord = IDMUserRecord.starter() .setDistinctId("distinctId") //手动指定外层 distinct_id .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, loginId) //用户维度标识 .addIdentityProperty(SensorsAnalyticsIdentity.EMAIL, email) //用户维度标识 .addProperty("Sex", "Male") // 设置用户性别属性(Sex)为男性 .addProperty("UserLv", "Elite VIP") // 设置用户等级属性(Level)为 VIP .build(); sa.profileSetById(userRecord);

For user attributes that are no longer needed, you can delete them using the profileUnsetById() API.

The attribute name and value constraints in User Attribute are the same as Event Attribute. For detailed instructions, please refer to the data format.

Record Initial Properties

We can use properties that are only valid when first set profileSetOnceById() record these attributes. Different from profileSetById() interface, If the set user property already exists, the record is ignored without overwriting existing data, and is automatically created if the property does not exist. Therefore,profileSetOnceById() is applicable to setting the first activation time and first registration time. For example:

String distinctId = "ABCDEF123456789"; IDMUserRecord userRecord = IDMUserRecord.starter() .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, distinctId) .addProperty("AdSource", "App Store") .build(); sa.profileSetOnceById(userRecord);

Properties of a list type

For attributes such as the user's favorite movie and the restaurant that the user has reviewed, the column phenotype attributes can be recorded. Note that the element in the column phenotype attribute must be of type String, and the value of the element is automatically de-duplicated. See list type restrictionsdata format  Attribute length restriction.

String distinctId = "ABCDEF123456789"; // 电影列表 List<String> movies = new ArrayList<String>(); movies.add("Sicario"); movies.add("Love Letter"); // 游戏列表 List<String> games = new ArrayList<String>(); games.add("Call of Duty"); games.add("Halo"); // 用户属性 Map<String, Object> properties = new HashMap<String, Object>(); properties.put("movies", movies); properties.put("games", games); // 传入properties,设置用户喜欢的电影属性(movies)和喜欢的游戏属性(games) // 设置成功后,"movies" 属性值为 ["Sicario", "Love Letter"];"games" 属性值为 ["Call of Duty", "Halo"] IDMUserRecord userRecord = IDMUserRecord.starter() .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, distinctId)           .addProperty("movies", movies) .addProperty("games", games)         .build(); sa.profileAppendById(userRecord); // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(movies) // 设置成功后 "movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] IDMUserRecord userRecord = IDMUserRecord.starter() .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, distinctId)            .addProperty("movies", "Dead Poets Society")        .build(); sa.profileAppendById(userRecord);

Data Collection for Tracking Events

After the SDK initialization is completed, you can track events using the following interface.

Tracking Events

When first implementing Sensdata Analytics, it is recommended to track 3-5 key events. With just a few lines of code, you can experience the analysis capabilities of Sensdata Analytics. For example:

  • For a photo social product, you can track events such as browsing photos and commenting.
  • For an e-commerce product, you can track events such as user registration, browsing products, and placing orders.

After successful initialization of the Sensdata Analytics SDK, you can use the trackById() method to record events. The method requires three parameters: user ID, event name (eventName), and can accept an IDMEventRecord object to add custom event properties. Using an e-commerce product as an example, you can track a purchase event like this:

// 使用 ConcurrentLoggingConsumer 初始化 SensorsAnalytics final SensorsAnalytics sa = new SensorsAnalytics(new SensorsAnalytics.ConcurrentLoggingConsumer("您的日志文件路径")); // 用户的 Distinct ID String distinctId = "ABCDEF123456789"; // 用户浏览商品   IDMEventRecord eventRecord = IDMEventRecord.starter() .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, distinctId) //用户维度标识 .setEventName("ViewProduct") //设置埋点事件名称 .addProperty("ProductId", "123456") // 设置埋点事件属性 .build(); sa.trackById(eventRecord); // 用户订单付款 	// 订单中的商品 ID 列表 List<String> productIdList = new ArrayList<String>(); productIdList.add("123456"); productIdList.add("234567"); productIdList.add("345678"); IDMEventRecord eventRecord = IDMEventRecord.starter() .addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, distinctId) //用户维度标识 .setEventName("PaidOrder") //设置埋点事件名称 .addProperty("OrderId", "123456") // 设置埋点事件属性 .addProperty("ProductIdList", productIdList) .build(); sa.trackById(eventRecord);   

With the Debug Mode , you can verify the accuracy of tracked events and properties. In normal mode, after data import, you can see the tracking results in Sensdata Analytics after a short wait.

Previous
Basic API Introduction(Java)
Next
Easy User Association API (Java)
Last modified: 2024-12-27