Menu

Simple User Association API (IDM2.0-Golang)

User Association

In the server-side application, Senser Analysis also requires setting the Distinct ID of each event for the user, which helps to provide more accurate retention rate and other data. For registered users, it is recommended to use the user ID in the system as the Distinct ID, and it is not recommended to use information that can be modified, such as username, email, mobile phone number, etc.

It is recommended to specify the isLoginId parameter for all Track and Profile methods to inform Senser Analysis of the type of user ID.

User Registration/Login

When the distinct ID of the same user changes (generally caused by anonymous user registration), the old and new distinct IDs can be associated through TrackSignup() to ensure the accuracy of user analysis.

  • Interface
// distinctId 为新的 ID(注册后的 ID),originId 为旧 ID(匿名 ID) func (sa *SensorsAnalytics) TrackSignup(distinctId, originId string) error 
  • Example
// 匿名 ID 由前端传过来 anonymous_id := "9771C579-71F0-4650-8EE8-8999FA717761" register_id := "0012345678" // 用户注册/登录时,将用户注册 ID 与 匿名 ID 关联 err := sa.TrackSignup(register_id, anonymous_id)

Note that for the same user, it is generally recommended to call TrackSignup() only once (usually called during user registration), and the association of user behaviors before and after login is recommended to be implemented on the business side. Before version 1.13 of Sensics analytics, when TrackSignup() was called multiple times, only the first association behavior was effective. After version 1.13, Sensics analytics provides methods for associating multiple device IDs. For more detailed explanation, please refer to Identify Users and contact our technical support personnel when necessary.

Set User Properties

To provide more accurate analysis services for targeted audiences, Sensors analytics SDK can set user properties such as age, gender, etc. Users can use user properties as filter conditions or use them as dimensions for multidimensional analysis in retention analysis, distribution analysis, and other functions.

Record User Properties

Use ProfileSet() to set user properties:

  • Interface
// distinctId 为用户标识,properties 为要设置的用户属性,isLoginId 标示 distinctId 是否为登录后的 ID func (sa *SensorsAnalytics) ProfileSet(distinctId string, properties map[string]interface{}, isLoginId bool) error
  • Example
distinct_id := "ABCDEF123456789" properties := map[string]interface{}{ // 用户性别属性(Sex)为男性 "Sex" : "Male", // 用户等级属性(Level)为 VIP "UserLevel" : "Elite VIP", } // 设置用户属性 err := sa.ProfileSet(distinct_id, properties, true)

For user properties that are no longer needed, you can delete them using the ProfileUnset() interface. For user properties, the constraints on property names and values are the same as event properties. For detailed instructions, please refer to Data Format.

Record Initially Set Properties

For properties that are only valid at the first setting, we can use ProfileSetOnce() to record these properties. Unlike the ProfileSet() interface, if the user property being set already exists, this record will be ignored and will not overwrite the existing data; if the property does not exist, it will be automatically created. Therefore, ProfileSetOnce() is more suitable for setting properties such as the first activation time and the first registration time for users.

  • Interface
// distinctId 为用户标识,properties 为要设置的用户属性,isLoginId 标示 distinctId 是否为登录后的 ID func (sa *SensorsAnalytics) ProfileSetOnce(distinctId string, properties map[string]interface{}, isLoginId bool) error
  • Example
distinct_id := "ABCDEF123456789" // 设置用户渠道属性(AdSource)为 "App Store" properties := map[string]interface{}{ "AdSource" : "App Store", } err := sa.ProfileSetOnce(distinct_id, properties, true) properties = map[string]interface{}{ "AdSource" : "Search Engine", } // 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" err = sa.ProfileSetOnce(distinct_id, properties, true)

Numerical type attribute

For numerical user attributes, you can use the ProfileIncrement() to accumulate attribute values. It is commonly used to record the number of payments, payment amounts, points, and other attributes.

  • Interface
// distinctId 为用户标识,properties 为要设置的用户属性,这个属性中应该只包含 value 为 int 的属性,isLoginId 标示 distinctId 是否为登录后的 ID func (sa *SensorsAnalytics) ProfileIncrement(distinctId string, properties map[string]interface{}, isLoginId bool) error
  • Example
distinct_id := "ABCDEF123456789" properties := map[string]interface{}{ "GamePlayed" : 1, } // 设置用户游戏次数属性(GamePlayed),将次数累加1次 err := sa.ProfileIncrement(distinct_id, properties, true)

List type attribute

For attributes like user's favorite movies or reviewed restaurants, you can record list-type attributes. It is important to note that elements in the list must be of string type, and duplicate values will be automatically removed. For information on the restrictions of list type attributes, please see Data Format and attribute length limitations.

  • Interface
// distinctId 为用户标识,properties 为要设置的用户属性,这个属性中应该只包含 value 为 [string] 的属性,isLoginId 标示 distinctId 是否为登录后的 ID func (sa *SensorsAnalytics) ProfileAppend(distinctId string, properties map[string]interface{}, isLoginId bool) error
  • Example
distinct_id := "ABCDEF123456789" properties := map[string]interface{}{ // 电影列表 "Movies" : []string{"Sicario", "Love Letter"}, // 游戏列表 "Games" : []string{"Call of Duty", "Halo"}, } // 传入properties,设置用户喜欢的电影属性(movies)和喜欢的游戏属性(games) // 设置成功后,"Movies" 属性值为 ["Sicario", "Love Letter"];"Games" 属性值为 ["Call of Duty", "Halo"] err := sa.ProfileAppend(distinct_id, properties, true) // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) // 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] properties = map[string]interface{}{ "Movies" : []string{"Dead Poets Society"}, } err = sa.ProfileAppend(distinct_id, properties, true) // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies), // 但属性值 "Love Letter" 与已列表中已有元素重复,操作无效, // "Movies" 属性值仍然为 ["Sicario", "Love Letter", "Dead Poets Society"] properties = map[string]interface{}{ "Movies" : []string{"Love Letter"}, } err = sa.ProfileAppend(distinct_id, properties, true)

Event tracking

After the SDK initialization is completed, you can use the following interfaces for event tracking.

Track events

When you first start using Sensors Analytics, it is recommended to track 3 to 5 key events. It only requires a few lines of code to experience the analysis capabilities of Sensors Analytics. For example:

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

Users can use the Track() interface to record events. For any event, it must include the user identifier (Distinct ID) and the event name (event). Users can pass a map[string]interface{} object as the third parameter in Track() to add custom event properties. For example, for an e-commerce product, you can track a shopping behavior like this:

  • Interface
// distinctId 是用户标识,event 是事件名,properties 是自定义事件属性,isLoginId 表示是否登录 func (sa *SensorsAnalytics) Track(distinctId, event string, properties map[string]interface{}, isLoginId bool) error
  • Example
distinct_id := "ABCDEF123456" properties := map[string]interface{}{ // "$time" 属性是系统预置属性,取值为 int64 类型,表示事件发生的时间,单位毫秒,如果设置该属性,则替换 SDK 内部获取的系统当前时间。如果不填入该属性,则默认使用系统当前时间 "$time" : time.Now().UnixMilli(), // "$ip" 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 "$ip" : "123.123.123.123", // 商品 ID "ProductId" : "123456", // 商品类别 "ProductCatalog" : "Laptop Computer", // 是否加入收藏夹,Boolean 类型的属性 "IsAddedToFav" : True, } // 记录用户浏览商品事件 err := sa.Track(distinct_id, "ViewProduct", properties, true) properties:= map[string]interface{}{ // 用户 IP 地址 "$ip" : "123.123.123.123", // 商品 ID 列表,[]string 类型的属性 "ProductIdList" : []string{"123456", "234567", "345678"}, // 订单价格 "OrderPaid" : 12.10, } // 记录用户订单付款事件 err = sa.Track(distinct_id, "PaidOrder", properties, true) 
Previous
Global User Association API (IDM3.0-Golang)
Next
PHP SDK
Last modified: 2024-12-27