Menu

User Cross-Platform Association API (C#)

SDK version needs to be >= v2.1.0

User Association

User Registration/Login

In the user cross-platform association business, user identification is bound through the Bind interface. The Bind() function is called when the user logs in, and the first parameter is obtained from the detailed preset ID key list, and the second parameter is the corresponding specific user ID. For example, binding the phone number to the user ("ABCDEF123456789"):

Interface description:

/// <summary> /// 绑定用户标识 /// </summary> /// <param name="identities">用户标识 List,至少需要两个 identity</param> public void Bind(List<SensorsAnalyticsIdentity> identities) 


Usage Example:

List<SensorsAnalyticsIdentity> identities = SensorsAnalyticsIdentityHelper.CreateBuilder() .AddIdentityProperty("$identity_mobile", "123") .AddIdentityProperty("$identity_email", "a@a.com") 		 .Build(); sa.bind(identities)

User Unbinding

When multiple user IDs are unassociated, the UnBind() function is called to unbind the association. The first parameter is the key to cancel the association, and the second parameter is the corresponding user ID to be unassociated.

Interface description:

/// <summary> /// 解绑用户标识 /// </summary> /// <param name="identity">用户标识</param> public void Unbind(SensorsAnalyticsIdentity identity)


Usage Example:

SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("$identity_mobile", "123") sa.unbind(identity);

Set User Properties

In order to provide more accurate analysis services for targeted audiences, the Sensors Analytics SDK can set user properties such as age, gender, etc. Users can use user properties as filtering conditions or use user properties as dimensions for multi-dimensional analysis in retention analysis, distribution analysis, and other functions.

Record User Properties

Use the ProfileSetById() function to set user properties:

Interface description:

/// <summary> /// 设置用户的属性。如果要设置的 properties 的 key,之前在这个用户的 profile 中已经存在,则覆盖,否则,新创建 /// </summary> /// <param name="identities">用户标识 List</param> /// <param name="properties">用户属性</param> public void ProfileSetById(List<SensorsAnalyticsIdentity> identities, Dictionary<String, Object> properties) /// <summary> /// 设置用户的属性。如果要设置的 properties 的 key,之前在这个用户的 profile 中已经存在,则覆盖,否则,新创建 /// </summary> /// <param name="identity">用户标识</param> /// <param name="properties">用户属性</param> public void ProfileSetById(SensorsAnalyticsIdentity identity, Dictionary<String, Object> properties) 


Example:

//1. 用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); //2.用户注册时,填充了一些个人信息,可以用Profile接口记录下来 Dictionary<string, Object> userRecord = new Dictionary<string, object>(); userRecord.Add("$city", "武汉"); userRecord.Add("$province", "湖北"); userRecord.Add("$name", "昵称123"); userRecord.Add("$signup_time", DateTime.Now); userRecord.Add("Gender", "male"); userRecord.Add("age", 20); sa.ProfileSetById(identity, userRecord);

For user attributes that are no longer needed, you can delete the attributes using the ProfileUnsetById() API. The constraint conditions for attribute names and values are the same as event properties, for more information please refer to the data format.

Record the initially set attributes.

For attributes that are only valid when first set, we can use the ProfileSetOnceById() API to record these attributes. Different from the ProfileSetById() API, if the user attribute being set already exists, this record will be ignored instead of overwriting the existing data. If the attribute does not exist, it will be automatically created. Therefore, ProfileSetOnceById() is more suitable for setting attributes such as the first activation time and the first registration time for users.

API description:

/// <summary> /// 首次设置用户的属性。与 ProfileSetById 接口不同的是:如果要设置的 properties 的 key,在这个用户的 profile 中已经存在,则不处理,否则,新创建 /// </summary> /// <param name="identities">用户标识 List</param> /// <param name="properties">用户属性</param> public void ProfileSetOnceById(List<SensorsAnalyticsIdentity> identities, Dictionary<String, Object> properties) /// <summary> /// 首次设置用户的属性。与 ProfileSetById 接口不同的是:如果要设置的 properties 的 key,在这个用户的 profile 中已经存在,则不处理,否则,新创建 /// </summary> /// <param name="identity">用户标识</param> /// <param name="properties">用户属性</param> public void ProfileSetOnceById(SensorsAnalyticsIdentity identity, Dictionary<String, Object> properties)

Example:

//1. 用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); //2.设置首次访问时间 Dictionary<string, Object> firstVisitRecord = new Dictionary<string, object>(); firstVisitRecord.Add("$first_visit_time", DateTime.Now); // $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false firstVisitRecord.Add("$is_login_id", true); sa.ProfileSetOnceById(identity, firstVisitRecord);

Numeric type attributes

For numeric type user attributes, you can use the ProfileIncrementById() API to increment the attribute value. This is commonly used to record user payment frequency, payment amount, loyalty points, etc.

API description:

/// <summary> /// 为用户的一个或多个数值类型的属性累加一个数值,若该属性不存在,则创建它并设置默认值为0。属性取值只接受Number类型 /// </summary> /// <param name="identities">用户标识 List</param> /// <param name="properties">用户属性</param> public void ProfileIncrementById(List<SensorsAnalyticsIdentity> identities, Dictionary<String, Object> properties) /// <summary> /// 为用户的一个或多个数值类型的属性累加一个数值,若该属性不存在,则创建它并设置默认值为0。属性取值只接受Number类型 /// </summary> /// <param name="identity">用户标识</param> /// <param name="properties">用户属性</param> public void ProfileIncrementById(SensorsAnalyticsIdentity identity, Dictionary<String, Object> properties) /// <summary> /// 为用户的数值类型的属性累加一个数值,若该属性不存在,则创建它并设置默认值为0 /// </summary> /// <param name="identities">用户标识 List</param> /// <param name="property">属性名称</param> /// <param name="value">属性值</param> public void ProfileIncrementById(List<SensorsAnalyticsIdentity> identities, String property, long value) /// <summary> /// 为用户的数值类型的属性累加一个数值,若该属性不存在,则创建它并设置默认值为0 /// </summary> /// <param name="identity">用户标识</param> /// <param name="property">属性名称</param> /// <param name="value">属性值</param> public void ProfileIncrementById(SensorsAnalyticsIdentity identity, String property, long value)


Usage example:

//1. 用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); //2.给属性加值 Dictionary<string, Object> incrementRecord = new Dictionary<string, object>(); incrementRecord.Add("age", 2); sa.ProfileIncrementById(identity, incrementRecord);

List type attributes

For attributes such as user favorite movies, reviewed restaurants, and hobbies, you can record list type attributes. It is important to note that the elements in the list type attribute must be of the string type, and the values of the elements will be automatically deduplicated. For the restrictions on list types, please see the data format and the attribute length limit.

API description:

/// <summary> /// 为用户的一个或多个数组类型的属性追加字符串. /// </summary> /// <param name="identities">用户标识 List</param> /// <param name="properties">用户属性</param> public void ProfileAppendById(List<SensorsAnalyticsIdentity> identities, Dictionary<String, Object> properties) /// <summary> /// 为用户的一个或多个数组类型的属性追加字符串. /// </summary> /// <param name="identity">用户标识</param> /// <param name="properties">用户属性</param> public void ProfileAppendById(SensorsAnalyticsIdentity identity, Dictionary<String, Object> properties)


Usage example:

//1.用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); //2.追加属性,这里追加兴趣爱好 List<String> newInterest = new List<String>(); newInterest.Add("ball"); Dictionary<string, Object> appendRecord = new Dictionary<string, object>(); appendRecord.Add("interest", newInterest); sa.ProfileAppendById(identity, appendRecord);

Event tracking collection

After SDK initialization is complete, you can use the following API for event tracking.

Track events

When first integrating with Sensors Analytics, it is recommended to track 3-5 key events, which only requires a few lines of code to experience the analysis functions of Sensors Analytics. For example:

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

The user records events through the TrackById() interface. For any event, it must include the user identifier (Identity) and the event name (event). Users can pass a map[string]interface{} object as the third parameter to the TrackById() method to add custom event properties. Taking an e-commerce product as an example, you can track a shopping behavior like this:

Interface Description:

/// <summary> /// 使用用户标识 3.0 方式进行时间埋点 /// </summary> /// <param name="identity">用户标识</param> /// <param name="eventName">事件名</param> /// <param name="properties">事件属性</param> public void TrackById(SensorsAnalyticsIdentity identity, String eventName, Dictionary<String, Object> properties)


Example:

//1.用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); //2.搜索商品 Dictionary<string, Object> searchRecord = new Dictionary<string, object>(); searchRecord.Add("KeyWord", "XX手机"); sa.TrackById(identity, "SearchProduct", searchRecord); //3.浏览商品 Dictionary<string, Object> lookRecord = new Dictionary<string, object>(); lookRecord.Add("ProductName", "XX手机"); lookRecord.Add("ProductType", "智能手机"); lookRecord.Add("ShopName", "XX官方旗舰店"); sa.TrackById(identity, "ViewProduct", lookRecord);
Previous
Basic API Introduction(C#)
Next
Easy User Association API (C#)
Last modified: 2024-12-27