Menu

Easy User Association API (Java)

User association

In the server-side application, the Distinct ID of the user is also required to be set for each event, which helps the analytics provide more accurate data such as retention rates.

For registered users, a user ID in the customer service system is recommended as a Distinct ID. Information that can be modified, such as the user name, Email address, and mobile phone number, is not recommended as a Distinct ID.

For unregistered anonymous users, the method of obtaining the anonymous ID of the user is as follows:

  1. How the back end gets the anonymous ID generated by the front-end JavaScript SDK:
    You can find in the Cookie that the key is sensorsdata2015jssdkcross  value  decodeURIComponent to decode, finally, use JSON.parse method get an object whose distinct_id value is the required ID
    (Note that if the login method has already been called on the front end, then distinct_id is the login ID, so you need to get the value of the first_id field first. If it is not available, then get the distinct_id value).
  2. If the Login method is embedded in the App, the client needs to use the interface provided by Sensors to obtain an anonymous ID and send the anonymous ID to the server. The server uses the anonymous ID transmitted from the client as a Distinct ID.

All track and profile The user ID must also be specified for each of the series methods(distinctId)和用户 ID 是否为登录 ID (isLoginId) 这两个参数,以便明确告知神策分析用户 ID 的类型。

User registration/login

Use trackSignUp() associate the anonymous ID with the login ID to ensure the accuracy of user analysis. For example:

// 前端的匿名 ID,获取匿名 ID 的方式参考上文 String anonymousId = "9771C579-71F0-4650-8EE8-8999FA717761"; String registerId = "0012345678"; // 用户注册/登录时,将用户登录 ID 与匿名 ID 关联 sa.trackSignUp(registerId, anonymousId);

Be aware of the following:

  • trackSignUp() It is recommended to call during user registration/login. If the client also collects any events, it is also necessary to invoke the association method login() on the client to associate the anonymous ID with the login ID during registration/login. During registration/login, the client and server each call the association method once for the following reasons:
    • In the one-to-one association mechanism, if the client's association information $SignUp event is not sent successfully or is delayed to the Shenze system during user registration/login, the event triggered by the server ($is_login_id=true) is sent to the Shenze system first, resulting in a self-association of the login ID. As a result, the login ID cannot be associated with the anonymous ID. The anonymous behavior of the client and the behavior after login identify the behaviors of two users.
    • A client-side call to the association method login(), in addition to associating the anonymous ID with the login ID, switches the client-side distinctId value for a user from the anonymous ID to the login ID. When viewing the distinctId value, it is a good way to determine whether the distincTID behavior is post-login or anonymous.Therefore, it is highly recommended that users log in/register once on the client side login() at the same time, trackSignUp() is also called once on the server.
  • If the server collects related events or sets user attributes only after the user logs in successfully, ensure that the track event /profileSet sets user attributes($is_login_id set true) code is called after the trackSignUp() method to ensure that the anonymous ID is associated with the login ID before collecting the logged-in user's behavior events/setting user properties.
  • Prior to version 1.13, when trackSignUp() /login() was called multiple times, only the first association behavior was valid. The method for multi-device ID association was provided after the 1.13 version of Sensors Analytics. Please refer to more detailed instructionsIdentify user, and contact our technical support staff if necessary.

user attributes

In order to provide more accurate analysis services for the population, the Sensors Analytics SDK can set user attributes, such as age, gender, and so on. Users can use user attributes as filtering criteria or user attributes as dimensions for multi-dimensional analysis in retention analysis, distribution analysis and other functions.

record user attributes

Use profileSet() to set user properties:

String distinctId = "ABCDEF123456789"; UserRecord userRecord = UserRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.TRUE) .addProperty("Sex", "Male") // 设置用户性别属性(Sex)为男性 .build(); sa.profileSet(userRecord); userRecord = UserRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.TRUE) .addProperty("UserLv", "Elite VIP") // 设置用户等级属性(Level)为 VIP .build(); sa.profileSet(userRecord);

For user attributes that are no longer needed, you can use profileUnset() interface to delete the attributes.

In user attributes, the constraints on the attribute name and attribute value are the same as those on event attributes. For details, seedata format.

Records the properties that were first set

We can use properties that are only valid when first set profileSetOnce() record these attributes. Different from profileSet() , 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,profileSetOnce()  is applicable to setting the first activation time and first registration time. For example:

String distinctId = "ABCDEF123456789"; UserRecord firstVisitRecord = UserRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.TRUE) .addProperty("AdSource", "App Store") .build(); sa.profileSetOnce(firstVisitRecord);

Properties of the record 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 limit.

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"] UserRecord appendRecord = UserRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.TRUE) .addProperty("movies", movies) .addProperty("games", games) .build(); sa.profileAppend(appendRecord); // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(movies) // 设置成功后 "movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] UserRecord appendRecord = UserRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.TRUE) .addProperty("movies", "Dead Poets Society") .build(); sa.profileAppend(appendRecord); 

track event acquisition

After SDK initialization is complete, you can bury data through the following interface.

Trace event

For the first time, it is recommended to track 3 to 5 key events, and it only takes a few lines of code to experience the analysis function of Divine Analysis. For example:

  • Photo social products that track users browsing pictures and commenting on events
  • E-commerce products can track events such as user registration, browsing products and placing orders

After the SDK is successfully initialized, you can use track() record event,must contain User ID(distinctId)、User ID Specifies whether the user ID is a login ID (isLoginId)、event name(eventName)these three parameters, And can pass in one at the same time EventRecord object, Add custom event properties to events. Taking e-commerce products as an example, you can track a purchase like this:

// 使用 ConcurrentLoggingConsumer 初始化 SensorsAnalytics final SensorsAnalytics sa = new SensorsAnalytics(new SensorsAnalytics.ConcurrentLoggingConsumer("您的日志文件路径")); // 用户的 Distinct ID String distinctId = "ABCDEF123456789"; // 用户浏览商品 { EventRecord lookRecord = EventRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.FALSE) .setEventName("ViewProduct") .addProperty("$time", new Date()) // '$time' 属性是系统预置属性,表示事件发生的时间,如果不填入该属性,则默认使用系统当前时间 // '$ip' 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 .addProperty("$ip", "123.123.123.123") .addProperty("ProductId", "123456") .build(); // 记录用户浏览商品事件 sa.track(lookRecord); } // 用户订单付款 { 	// 订单中的商品 ID 列表 List<String> productIdList = new ArrayList<String>(); productIdList.add("123456"); productIdList.add("234567"); productIdList.add("345678"); EventRecord lookRecord = EventRecord.builder().setDistinctId(distinctId).isLoginId(Boolean.FALSE) .setEventName("PaidOrder") // '$ip' 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 .addProperty("$ip", "123.123.123.123") .addProperty("OrderId", "123456") .addProperty("ProductIdList", productIdList) .build(); // 记录用户订单付款事件 sa.track(lookRecord); }

Use debug mode , you can verify that the traced events and attributes are correct. In normal mode, after the data is imported, wait for a moment in the strategy analysis to see the tracking results.

Previous
User Association API (Java)
Next
Java SDK Demo
Last modified: 2024-12-27