SDK version needs to be >= v1.11.0
User Cross-linking
User Registration/Login
In the user cross-linking business, the user identity is bound through the Bind interface. During user login, call the bind() binding function, where the first parameter is obtained from the detailed predefined id key list to get the corresponding specific user ID. For example, bind the phone number to the user ("ABCDEF123456789"):
Interface description:
def bind(self, first_identity, second_identity, *other_identities): """ 绑定用户标识。至少需要提供两个用户标识信息。identity 的数据类型为 SensorsAnalyticsIdentity。 :param first_identity 待绑定的用户标识 :param second_identity 待绑定的用户标识 :param other_identities 其他需要绑定的用户标识 """
Usage example:
sa.bind(SensorsAnalyticsIdentity("$identity_mobile", "123"), SensorsAnalyticsIdentity("$identity_email", "a@a.com"))
User Unlinking
When canceling the association between multiple user IDs, call the unbind() function, where the first parameter is the key to cancel the association and the second parameter is the corresponding user ID to be unlinked.
Interface description:
def unbind(self, identity): """ 解除用户标识 :param identity """
Usage example:
sa.unbind(SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.EMAIL, "sv1"))
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 and gender. Users can use user properties as filtering criteria or dimensions for multidimensional analysis in retention analysis, distribution analysis, and other functions.
Record User Properties
Use the profile_set_by_id() function to set user properties:
Interface description:
def profile_set_by_id(self, properties, *identities): """ 设置用户的属性。如果要设置的 properties 的 key,之前在这个用户的 profile 中已经存在,则覆盖,否则,新创建 :param properties 用户属性 :param identities 用户标识,类型是 SensorsAnalyticsIdentity """
Example:
sa.profile_set_by_id({"$city":"武汉"}, SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.EMAIL, "sv1"))
For user attributes that are no longer needed, you can use profile_unset_by_id() Description The interface 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, see data format.
Records the properties that were first set
We can use properties that are only valid when first setprofile_set_once_by_id() to record these attributes. Different from profile_set_by_id() 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_by_id() is applicable to setting the first activation time and first registration time.
Interface description:
def profile_set_once_by_id(self, properties, *identities): """ 首次设置用户的属性。与 profile_set_by_id 接口不同的是:如果被设置的用户属性已存在,则这条记录会被忽略而不会覆盖已有数据,如果属性不存在则会自动创建 :param properties 用户属性 :param identities 用户标识,类型是 SensorsAnalyticsIdentity """
Example of use:
sa.profile_setonce_by_id({"$city":"武汉"}, SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.EMAIL, "sv1"))
Attributes of a numeric type
For numeric user properties, you can useprofile_increment_by_id() to add the property values. It is often used to record attributes such as the number of user payments, payment amount, and points.
Interface description:
def profile_increment_by_id(self, properties, *identities): """ 为用户的一个或多个数值类型的属性累加一个数值,若该属性不存在,则创建它并设置默认值为 0。属性取值只接受 Number类型。 :param properties 用户属性,类型是 dict,value 必须是 Number 类型 :param identities 用户标识,可以是 identity、list、tuple """
Example of use:
sa.profile_increment_by_id({"age": 2}, SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.EMAIL, "sv1"))
Properties of a list type
For the user's favorite movies, restaurants reviewed by the user, interests and other attributes, you can record the list of phenotypic attributes. 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 restrictions data format Attribute length restriction.
Interface description:
def profile_append_by_id(self, profiles, *identities): """ 追加一个用户的某一个或者多个集合类型的 Profile。 :param profiles 用户属性,其 key 必须是 str 类型,value 必须是 str 集合类型 :param identities 用户标识,类型是 SensorsAnalyticsIdentity
Example of use:
sa.profile_append_by_id({"interest": ["ball"]}, SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.EMAIL, "sv1"))
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 Sensors Analytics. 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
The user records events through the track_by_id() 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 in TrackById() to add custom event properties. For example, you can track a shopping behavior like this for e-commerce products:
Interface Description:
def track_by_id(self, event_name, properties, *identities): """ 使用用户标识 3.0 进行事件埋点 :param identity 用户标识 :param event_name 事件名 :param properties 事件属性,数据类型为 dict """
Usage Example:
sa.track_by_id("ViewProduct", {"ProductName":"XX手机"}, SensorsAnalyticsIdentity(SensorsAnalyticsIdentity.LOGIN_ID, "sv1"))