Basic API Introduction(Unity)
|
Collect
1. Set User ID
When using the SDK, the SDK uses the device ID as the anonymous ID of each user by default. This ID will be used as the ID of the user when the user is not logged in.
- Android:The SDK uses AndroidId as the device ID by default, and if AndroidId cannot get it, it gets a random UUID.
- iOS:If the App introduces the AdSupport library, the SDK uses IDFA as the anonymous ID. If IDFV or UUID is used, the device ID changes when the user uninstalls and reinstalls the App. You can also configure IDFA (for example, 1E2DFA89-496A-47FD-9941-DF1FC4E6484A). If IDFA is enabled, the SDK will obtain IDFA first. If IDFA fails to be obtained, the SDK will try to obtain IDFV again. Using IDFA prevents the user from changing the device ID after reinstalling the App.
1.1. User login
When the user successfully registers or logs in, the SDK needs to be called Login method:
using SensorsAnalytics; try { SensorsDataAPI.Login("登录 ID"); } catch (Exception ex) { //handle exception }
To accurately record the behavior information of the login user, you are advised to call the user once at each of the following times Login method:
· 用户在注册成功时 · 用户登录成功时 · 已登录用户每次启动 App 时
1.2. Setting a Custom anonymous ID (Optional)
If your game has its own anonymous ID management system for each user, you can callIdentify to set visit ID:
using SensorsAnalytics; try { SensorsDataAPI.Identify("匿名 ID"); } catch (Exception ex) { //handle exception }
2. Event acquisition
2.1. Record activation event
You can call - trackAppInstallWithProperties: method to record activation events $AppInstall, Calling this method multiple times will only trigger an activation event on the first call:
// 激活事件,可以添加自定义属性 Dictionary<string, object> dictionary = new Dictionary<string, object>(); dictionary.Add("DownloadChannel", "游戏下载渠道"); SensorsDataAPI.TrackAppInstall(dictionary);
2.2. Set the event public properties
Attributes that need to be added for all events can be passed after initializing the SDKRegisterSuperProperties to register a property as a public property:
using SensorsAnalytics; try { Dictionary<string, object> properties = new Dictionary<string, object>(); // 将应用名称作为事件公共属性,后续所有 Track() 追踪的事件都会自动带上 "AppName" 属性 properties.Add("AppName", "Dota 传奇"); SensorsDataAPI.RegisterSuperProperties(properties); } catch (Exception ex) { //handle exception }
2.3. Event duration
You can count the duration of an event with a timer. First of all,TrackTimerStart("EventName") , This method does not actually send the event; At the end of the event, call TrackTimerEnd("EventName", properties),SDK will trigger "Event" event, And automatically records the event duration in the event properties "$event_duration". After the event starts, you can use TrackTimerPause("EventName") to pause the event timing and then useTrackTimerResume("EventName") to restore event timing. For example, record the time users spend browsing product pages:
using SensorsAnalytics; try { // 进入商品页面 // 调用 TrackTimerStart("ViewProduct") 标记事件启动时间 SensorsDataAPI.TrackTimerStart("ViewProduct"); // ... 用户浏览商品 // 离开商品页 Dictionary<string, object> properties = new Dictionary<string, object>(); // 在属性中记录商品 ID properties.Add("country", "中国"); // 计时结束,触发 ViewProduct 事件 SensorsDataAPI.TrackTimerEnd("ViewProduct", properties); } catch (Exception ex) { //handle exception }
2.4. Statistics on the duration of intersections of events with the same name
By default, the duration of the statistics is identified by the event name. The same event name will automatically match start-end. If two events with the same name cross part in time, an incorrect match will be caused. Developers need to save the return value of trackTimerStart() so that they can pause, resume, or stop later.
using SensorsAnalytics; var result = SensorsDataAPI.TrackTimerStart("BuyGoods"); ... SensorsDataAPI.TrackTimerEnd("BuyGoods" or result); // 开始第一个事件计时 var timer1 = SensorsDataAPI.TrackTimerStart("testTimer"); // 开始第二个事件计时 var timer2 = SensorsDataAPI.TrackTimerStart("testTimer"); //如果需要暂停第一个事件计时 SensorsDataAPI.TrackTimerPause(timer1); //如果需要恢复第一个事件计时 SensorsDataAPI.TrackTimerResume(timer1); // 结束第一个事件计时 SensorsDataAPI.TrackTimerEnd(timer1); // 结束第二个事件计时 SensorsDataAPI.TrackTimerEnd(timer2);
3. User Attributes
3.1. Profile Set
Profile Set method can set user attributes, and when the same key is set multiple times, the value will be overridden:
using SensorsAnalytics; try { Dictionary<string, object> properties = new Dictionary<string, object>(); // 设定用户性别属性 "Sex" 为 "Male" properties.Add("Sex", "Male"); // 设定用户年龄属性 "Age" 为 18 properties.Add("Age", 18"); // 设定用户属性 SensorsDataAPI.ProfileSet(properties); } catch (Exception ex) { //handle exception }
3.2. Profile Set Once
For attributes that need to be effectively set only for the first time, such as the user's first reset amount or initial nickname, you can use the Profile Set Once method to set them. Different from the Profile Set method, if the user attribute being set already exists, the record will be ignored and it won't override existing data. If the attribute doesn't exist, it will be created automatically:
using SensorsAnalytics; try { Dictionary<string, object> properties = new Dictionary<string, object>(); // 设定用户 AdSource 渠道为为 "App Store" properties.Add("AdSource", "App Store"); // 设定用户属性 SensorsDataAPI.ProfileSetOnce(properties); } catch (Exception ex) { //handle exception }
3.3. Other APIs
Other APIs can be found in the Sample project for usage, or you can directly refer to the method descriptions in the SensorsDataAPI.cs file.
Note: The content of this document is a technical document that provides details on how to use the Sensors product and does not include sales terms; the specific content of enterprise procurement products and technical services shall be subject to the commercial procurement contract.