SDK version required > = v2.1.0
User association
User registration/login
In the global User association service, user ids are bound through the Bind interface.Called when the user logs inBind() bind,the first parameter is got fromDetailed list of preset id keys, The second parameter is the corresponding specific user ID.For example, bind a phone number to a user ("ABCDEF123456789") :
- interface
// identity 为用户标识 func (sa *SensorsAnalytics) Bind(identity Identity) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "ABCDEF123456789", sdk.EMAIL: "test@sensorsdata.cn", }, } err = sa.Bind(identity)
User unbind
Called when multiple user ids are disassociated UnBind() unbind, the first parameter is the key of the disassociation, and the second parameter is the ID of the corresponding disassociation user.
- interface
// identity 为用户标识 func (sa *SensorsAnalytics) UnBind(identity Identity) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "ABCDEF123456789", sdk.EMAIL: "test@sensorsdata.cn", }, } err = sa.UnBind(identity)
set user attributes
In order to provide more accurate analysis services for the population, the Sensors 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.
record user attributes
Use ProfileSetById() set user attributes:
- Interface
// identity 为用户标识,properties 为要设置的用户属性 func (sa *SensorsAnalytics) ProfileSetById(identity Identity, properties map[string]interface{}) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "ABCDEF123456789", }, } properties := map[string]interface{}{ // 用户性别属性(Sex)为男性 "Sex" : "Male", // 用户等级属性(Level)为 VIP "UserLevel" : "Elite VIP", } // 设置用户属性 err := sa.ProfileSetById(identity, properties)
For user attributes that are no longer needed, you can useProfileUnsetById() 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, seedata format.
Records the properties that were first set
We can use properties that are only valid when first setProfileSetOnceById() to record these attributes. Different from ProfileSetById() 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,ProfileSetOnceById() is applicable to setting the first activation time and first registration time.
- Interface
// identity 为用户标识,properties 为要设置的用户属性 func (sa *SensorsAnalytics) ProfileSetOnceById(identity Identity, properties map[string]interface{}) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "ABCDEF123456789", }, } // 设置用户渠道属性(AdSource)为 "App Store" properties := map[string]interface{}{ "AdSource" : "App Store", } err = sa.ProfileSetOnceById(identity, properties) properties = map[string]interface{}{ "AdSource" : "Search Engine", } // 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" err = sa.ProfileSetOnceById(identity, properties)
Numerical type attribute
For numerical user attributes, you can use ProfileIncrementById() to accumulate the attribute values. It is commonly used for recording attributes such as the number of user payments, payment amount, points, etc.
- Interface
// identity 为用户标识,properties 为要设置的用户属性 func (sa *SensorsAnalytics) ProfileIncrementById(identity Identity, properties map[string]interface{}) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "ABCDEF123456789", }, } properties := map[string]interface{}{ "GamePlayed": 1, } // 设置用户游戏次数属性(GamePlayed),将次数累加1次 err := sa.ProfileIncrementById(identity, properties)
List type attribute
For attributes such as movies that users like or restaurants that users have reviewed, you can record them as list-type attributes. Please note that the elements in the list-type attribute must be of string type, and the values of the elements will be automatically deduplicated. For restrictions on list type, please refer to the Data Format attribute length limit.
- Interface
// identity 为用户标识,properties 为要设置的用户属性,这个属性中应该只包含 value 为 [string] 的属性 func (sa *SensorsAnalytics) ProfileAppendById(identity Identity, properties map[string]interface{}) error
- Example
identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_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.ProfileAppendById(identity, properties) // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) // 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] properties = map[string]interface{}{ "Movies" : []string{"Dead Poets Society"}, } err = sa.ProfileAppendById(identity, properties) // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies), // 但属性值 "Love Letter" 与已列表中已有元素重复,操作无效, // "Movies" 属性值仍然为 ["Sicario", "Love Letter", "Dead Poets Society"] properties = map[string]interface{}{ "Movies" : []string{"Love Letter"}, } err = sa.ProfileAppendById(identity, properties)
Event Tracking
After the SDK is initialized, you can perform event tracking through the following interface.
Track events
When you first integrate Sensors Analytics, it is recommended to track 3-5 key events. It only takes a few lines of code to experience the analysis function of Sensors Analytics. For example:
- For a photo social product, you can track events such as browsing photos and commenting by users.
- For an e-commerce product, you can track events such as user registration, browsing products, and placing orders.
User useTrackById() interface records events and, for any event, must contain the user identifier (Identity)and event name(event)two parameters. Usrs can useTrackById() third parameter to pass map[string]interface{} object, to add custom event properties to events. Taking e-commerce products as an example, you can track a purchase like this:
- Interface
// identity 是用户标识,event 是事件名,properties 是自定义事件属性 func (sa *SensorsAnalytics) TrackById(identity Identity, event string, properties map[string]interface{}) error
- Example
event := "ViewProduct" properties := map[string]interface{}{ "$ip": "2.2.2.2", "ProductId": "123456", "ProductCatalog": "Laptop Computer", "IsAddedToFav": true, } identity := sdk.Identity{ Identities: map[string]string{ sdk.LOGIN_ID: "test@sensorsdata.cn", }, } err = sa.TrackById(identity, event, properties)