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 system is recommended as a Distinct ID. Modified information such as the user name, Email address, and mobile phone number are not recommended. For unregistered anonymous users, the server also needs an ID to mark the user (this ID is generally the ID generated by the front-end SDK).
All of the track and profile family methods are recommended to be explicitly specifiedis_login_id parameter, in order to clearly inform the Shenze analysis of the type of user ID. Not called on this user track_signup()before(login/register),is_login_id of the parameter should be False,after calling track_signup() , is_login_id of the parameter should be True.
Note: Please in event properties or user properties do not pass$is_login_id parameter
User registration/login
When the Distinct Id of the same user changes (usually due to anonymous user registration behavior), you can use track_signup() to associate the old Distinct ID with the new Distinct ID to ensure the accuracy of user analysis. For 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() In general, it is recommended to call only once (usually at the userregistercall), userloginIt is recommended to implement the association of the preceding and following behaviors on the business side. Prior to version 1.13, when track_signup() 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 Divine Analysis. Please refer to more detailed instructionsIdentifying Users - Easy User Association (IDM 2.0 & IDM 1.0), and contact our technical support staff if necessary.
Set user properties
In order to provide more accurate analysis services for the population, the 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.
Use profile_set() set user properties:
distinct_id = 'ABCDEF123456789' properties = { # 用户性别属性(Sex)为男性 'Sex' : 'Male', # 用户等级属性(Level)为 VIP 'UserLevel' : 'Elite VIP', } # 设置用户属性 sa.profile_set(distinct_id, properties, is_login_id=True)
For user attributes that are no longer needed, you can useprofile_unset() interface to deleted the attribute.
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 profile_set_once() to record these attributes. Different from profile_set() interface, If the set user property already exists, the record is ignored and does not overwrite existing data. , the property is automatically created if it does not exist. Therefore,profile_set_once() is applicable to setting the first activation time and first registration time. For example:
distinct_id = 'ABCDEF123456789' # 设置用户渠道属性(AdSource)为 "App Store" sa.profile_set_once(distinct_id, {'AdSource' : 'App Store'}) # 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" sa.profile_set_once(distinct_id, {'AdSource' : 'Search Engine'})
Attributes of a numeric type
For numeric user properties, you can useprofile_increment() to add the property values. It is often used to record attributes such as the number of user payments, payment amount, and points. For example:
distinct_id = 'ABCDEF123456789' # 设置用户游戏次数属性(GamePlayed),将次数累加1次 sa.profile_increment(distinct_id, {'GamePlayed' : 1}, is_login_id=True)
Properties of a list type
For attributes such as movies that users love and restaurants that users have reviewed, you can record list-type attributes. It should be noted that the elements in the list-type attribute must be of type str, and the values of the elements will not be automatically deduplicated (only in versions before 1.12 of the Saasage system). For the restrictions on list types, please refer to Data Format for attribute length restrictions.
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, is_login_id=True) # 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) # 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] sa.profile_append(distinct_id, {'Movies' : ['Dead Poets Society']}, is_login_id=True) # 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies), # 属性值 "Love Letter" 与已列表中已有元素重复,操作仍有效, # 设置成功后, "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society", "Love Letter"] sa.profile_append(distinct_id, {'Movies' : ['Love Letter']}, is_login_id=True)
Event tracking
After the SDK initialization is complete, you can use the following interface for event tracking.
Tracking events
When you first access Sensort Tower, it is recommended to track 3~5 key events. With just a few lines of code, you can experience the analysis functions of Sensort Tower. For example:
- For a photo social product, you can track user's browsing of photos and commenting events.
- For an e-commerce product, you can track user's registration, product browsing, and ordering events.
Users record events through the track() interface. For any event, you must include the user identifier (Distinct ID) and event name (event_name) as two parameters. At the same time, users can pass in a dict object as the third parameter of track() to add custom event properties. For example, for an e-commerce product, you can track a shopping behavior like this:
distinct_id = 'ABCDEF123456' properties = { # '$time' 属性是系统预置属性,传入 datetime 对象,表示事件发生的时间,如果不填入该属性,则默认使用系统当前时间 '$time' : datetime.datetime.now(), # '$ip' 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 '$ip' : '123.123.123.123', # 商品 ID 'ProductId' : '123456', # 商品类别 'ProductCatalog' : 'Laptop Computer', # 是否加入收藏夹,Boolean 类型的属性 'IsAddedToFav' : True, } # 记录用户浏览商品事件 sa.track(distinct_id, 'ViewProduct', properties, is_login_id=True) properties = { # 用户 IP 地址 '$ip' : '123.123.123.123', # 商品 ID 列表,list<str> 类型的属性 'ProductIdList' : ['123456', '234567', '345678'], # 订单价格 'OrderPaid' : 12.10, } # 记录用户订单付款事件 sa.track(distinct_id, 'PaidOrder', properties, is_login_id=True)