Version Compatibility

  • Unity Editor 5.5.5 and above
  • Xcode 12.1 and above (iOS)

1. Integrated SDK

  1. Download the latest Unity SDK resource package from GitHub (unitypackage file)

  2. Add the above file to the project via Assets → Import Package → Custom Package 
  3. Drag the SensorsAnalytics.prefab prefab under the SensorsAnalytics directory to the desired loading position
  4. Configure SDK

Server URL: Data Receiving URL

  • Each project has a separate Data Receiving URL
  • Please use the administrator account to obtain the Data Receiving URL for the corresponding project

Enable Log: Whether to enable logging. After enabling it, the SDK's log output function will be turned on:

  • When a tracking event is triggered successfully, the SDK outputs an event data that starts with [track event];
  • When the event triggering fails, the SDK will output the corresponding error reason
  • When the event data is successfully reported, the SDK will output the event data starting with 【valid message】
  • When the event data fails to be reported, the SDK will output the event data starting with 【invalid message】 and output the error reason

AutoTrackTypes: Full-track configuration, not enabled by default. Check to enable full-track event collection for App start and App exit

NetworkTypes: Set the network conditions for reporting data to the server, only supports Android & iOS, default value is 3G, 4G, 5G and WIFI.

2. Configure Scheme (only supports Android & iOS)

When using Debug real-time viewing and other features, you need to configure the scheme. If it is already configured in the exported native project, it does not need to be configured in Unity, and this item can be ignored. If you need to configure the scheme in Unity, you can follow the instructions below.

2.1. Get Scheme

  • The scheme of the project needs to be obtained by an administrator account
  • Multiple project schemes can be configured in the App project

2.2. Configure Android Scheme

Add the AndroidManifest.xml file of UnityPlayerActivity in the Assets/Plugins/Android directory of the Unity Editor. The content is as follows, replace Scheme with the value obtained in the previous step.

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="您项目的 Scheme 值" /> </intent-filter> </activity> </application> </manifest>
XML

2.3. Configure iOS Scheme

In Edit →  Project Setting →  Player →  iOS tab →  Other Settings → Supported URL schemes , configure the obtained value.


2.4. Add Script Code

In the script (such as the script class name ExampleBehaviour), add the following code to process SchemeUrl.

// 声明 static 示例脚本 static ExampleBehaviour saInstance; void Awake(){ 	//判断是否需要 SDK 处理,防止重复添加回调 if (saInstance == null){ DontDestroyOnLoad(gameObject); saInstance = this; Application.deepLinkActivated += handSchemeUrl; }else{ Destroy(gameObject); return; } } private void handSchemeUrl(string url) { 	//SDK 处理 URl SensorsDataAPI.HandleSchemeUrl(url); }
C#

Application.deepLinkActivated currently only supports Unity Editor 2020.1, 2020.2, and 2019.4.

3. Code Tracking Event

After you have configured the SDK, you can use the Track method to track user behavior events and add custom properties to the events:

using SensorsDataAnalytics; try { 	Dictionary<string, object> properties = new Dictionary<string, object>(); 	// 设置商品 ID 	properties.Add("ProductID", 123456); 	// 设置商品类别 	properties.Add("ProductCatalog", "Laptop Computer"); 	// 触发 BuyProduct 事件 	SensorsDataAPI.Track("BuyProduct", properties); } catch (Exception ex) { 	//handle exception }
C#