User Association
In server-side applications, Sensot Analysis also requires setting a distinct ID for each event, which helps provide more accurate data such as retention rate. 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 usernames, email addresses, and phone numbers.
It is recommended to specify the parameter isLoginId for all Track and Profile series methods, in order to explicitly inform Sensot Analysis of the type of user ID.
User Registration/Login
When the distinct ID of the same user changes (usually due to anonymous user registration behavior), the old distinct ID and the new distinct ID can be associated through track_signup() to ensure the accuracy of user analysis.
- Interface
# distinct_id 为新的 ID(注册后的 ID),origin_distinct_id 为旧 ID(匿名 ID),properties 为携带的属性 def track_signup(distinct_id, origin_distinct_id, properties={}) end
- Example
# 匿名 ID 由前端传过来 anonymous_id = '9771C579-71F0-4650-8EE8-8999FA717761' register_id = '0012345678' # 用户注册/登录时,将用户注册 ID 与 匿名 ID 关联 sa.track_signup(register_id, anonymous_id)
Note that for the same user, track_signup() is generally recommended to be called only once (usually during user registration), and the association of behavior before and after user login is recommended to be implemented on the business side. Before Sensot Analysis 1.13 version, when multiple calls to track_signup(), only the behavior associated with the first call was valid. Starting from Sensot Analysis 1.13 version, a method of associating multiple device IDs is provided. For more detailed explanations, please refer to Identifying Users and contact our technical support personnel if necessary.
Set User Attributes
In order to provide more accurate analysis services for targeted users, Sensot Analysis SDK can set user attributes such as age and gender. Users can use user attributes as filtering conditions or analyze multidimensional data based on user attributes in functions such as retention analysis and distribution analysis.
Record User Attributes
Use profile_set() to set user attributes:
- Interface
# distinctId 为用户标识,properties 为要设置的用户属性 def profile_set(distinct_id, properties) end
- Example
distinct_id = 'ABCDEF123456789' properties = { # 用户性别属性(Sex)为男性 'Sex' => 'Male', # 用户等级属性(Level)为 VIP 'UserLevel' => 'Elite VIP', # $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false '$is_login_id' => true, } # 设置用户属性 sa.profile_set(distinct_id, properties)
For user attributes that are no longer needed, you can delete them through the profile_unset interface. The naming and value constraints of attributes in user attributes are the same as for event attributes. For detailed explanations, please refer to Data Format.
Record Initially Set Attributes
We can use properties that are only valid when first set profile_set_once() to record these attributes. Different from profile_set()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,profile_set_once() is applicable to setting the first activation time and first registration time.
- Interface
# distinct_id 为用户标识,properties 为要设置的用户属性 def profile_set_once(distinct_id, properties) end
- Example
distinct_id = 'ABCDEF123456789' properties = { # 设置用户渠道属性(AdSource)为 "App Store" 'AdSource' => 'App Store', # $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false '$is_login_id' => true, } sa.profile_set_once(distinct_id, properties) # 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" properties['AdSource'] = 'Search Engine' sa.profile_set_once(distinct_id, properties)
Numerical attribute
For numerical user attributes, you can use the profile_increment() function to accumulate the attribute values. It is commonly used to record user payment frequency, payment amount, points, and other attributes.
- Interface
# distinctId 为用户标识,properties 为要设置的用户属性,这个属性中应该只包含 value 为 int 的属性,isLoginId 标示 distinctId 是否为登录后的 ID def profile_increment(distinct_id, properties) end
- Example
distinct_id = 'ABCDEF123456789' properties = { # 设置用户游戏次数属性(GamePlayed),将次数累加1次 'GamePlayed' => 1, } sa.profile_increment(distinct_id, properties)
List attribute
For attributes such as user's favorite movies or restaurants they have reviewed, you can record them as list-type attributes. Please note that the elements in the list-type attribute must be of String or Symbol type, and the values of the elements will be automatically deduplicated. For restrictions on list-type attributes, please refer to the Data Format attribute length limitations.
- Interface
# distinct_id 为用户标识,properties 为要设置的用户属性,这个属性中应该只包含 value 为 [string] 的属性 def profile_append(distinct_id, properties) end
- Example
distinct_id = 'ABCDEF123456789' properties = { # 电影列表 'Movies' => ['Sicario', 'Love Letter'], # 游戏列表 'Games' => ['Call of Duty', 'Halo'], } # 传入properties,设置用户喜欢的电影属性(movies)和喜欢的游戏属性(games) # 设置成功后,"Movies" 属性值为 ["Sicario", "Love Letter"];"Games" 属性值为 ["Call of Duty", "Halo"] sa.profile_append(distinct_id, properties) # 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) # 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] sa.profile_append(distinct_id, {'Movie' => ['Dead Poets Society']})
Event tracking
After the SDK initialization is completed, you can perform data tracking/collecting using the following interfaces.
Track events
When you first integrate with Sensors Analytics, it is recommended to track 3-5 key events. It only takes a few lines of code to experience the analysis capabilities of Sensors Analytics. Examples:
- For a picture social product, you can track events such as browsing pictures and leaving comments.
- For e-commerce products, you can track events such as user registration, browsing products, and placing orders.
The user records events through the track() interface. For any event, it must include two parameters: the user identifier (Distinct ID) and the event name (event_name). At the same time, users can pass a dict object as the third parameter to track() to add custom event properties. In the custom properties, the $is_login_id property is used to indicate whether the Distinct ID is a login ID. Taking an e-commerce product as an example, you can track a purchase behavior like this:
- Interface
# distinctId 是用户标识,event 是事件名,properties 是自定义事件属性 def track(distinct_id, event_name, properties={}) end
- Example
distinct_id = 'ABCDEF123456' properties = { # '$time' 属性是系统预置属性,传入 datetime 对象,表示事件发生的时间,如果不填入该属性,则默认使用系统当前时间 '$time' => Time.now(), # '$ip' 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 '$ip' => '123.123.123.123', # 商品 ID 'ProductId' => '123456', # 商品类别 'ProductCatalog' => 'Laptop Computer', # 是否加入收藏夹,Boolean 类型的属性 'IsAddedToFav' => true, # $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false '$is_login_id' => true, } # 记录用户浏览商品事件 sa.track(distinct_id, 'ViewProduct', properties) properties = { # 用户 IP 地址 '$ip' => '123.123.123.123', # 商品 ID 列表,list<str> 类型的属性 'ProductIdList' => ['123456', '234567', '345678'], # 订单价格 'OrderPaid' => 12.10, # $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false '$is_login_id' => true, } # 记录用户订单付款事件 sa.track(distinct_id, 'PaidOrder', properties)