• Before using, please read the Data Model
  • Sensors Analytics Cocos2d-x SDK encapsulates commonly used APIs of Sensors Analytics Android & iOS SDK. Using this SDK, you can complete the statistical reporting of event tracking in games developed with Cocos2d-x SDK

  • For SDK release notes, please refer to the Release Notes on GitHub.
  • Cocos2d-x supports version 3.17 and above

1. Integrate Cocos2d-x Plugin

1.1. Import Cocos2d-x Plugin

Download the Cocos2d-x plugin and place the entire downloaded folder in the [COCOS2DX_ROOT]/Classes directory in your project.

Downloaded SensorsAnalytics directory is as follows:


SensorsAnalytics ├── android │   └── SensorsAnalytics.cpp ├── ios │   └── SensorsAnalytics.mm ├── common │   └── ObjectNode.cpp ├── include     └── SensorsAnalytics.h     └── FlushPolicy.h     └── ObjectNode.h


1.2. Integrate Cocos2d-x Plugin

In the [COCOS2DX_ROOT]/CMakeLists.txt file, add the Sensors Analytics plugin files based on the existing list(APPEND GAME_SOURCE) and list(APPEND GAME_HEADER):

list(APPEND GAME_SOURCE      ...      Classes/SensorsAnalytics/android/SensorsAnalytics.cpp      Classes/SensorsAnalytics/common/ObjectNode.cpp      )     list(APPEND GAME_HEADER      ...      Classes/SensorsAnalytics/include/ObjectNode.h      Classes/SensorsAnalytics/include/FlushPolicy.h      Classes/SensorsAnalytics/include/SensorsAnalytics.h )

As shown in the figure below:

Drag and drop the file into the project:

Add the Header Search Path for the plug-in file

The memory management mode of the SensorsAnalytics.mm file is changed to ARC

2. Integrate Sensors analytics SDK and initialize it

2.1.1. Integrated Sensors analytics SDK

Add SDK dependencies to app/build.gradle file under Android projects supported by Android Studio:

apply plugin: 'com.android.application'   dependencies {    // 添加 Sensors Analytics SDK 依赖    implementation 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.6.0' }
CODE

version requirement

  • Android SDK v4.4.0 and above

2.1.2. Initialize Sensors analytics SDK

Gets the project data receiving address

InApplication 的 onCreate() method, And called on the main threadSensorsDataAPI.startWithConfigOptions() to initialize SDK:

// 引入神策分析 SDK import com.sensorsdata.analytics.android.sdk.SensorsDataAPI; import com.sensorsdata.analytics.android.sdk.SAConfigOptions; import com.sensorsdata.analytics.android.sdk.SensorsAnalyticsAutoTrackEventType;   String SA_SERVER_URL = "数据接收地址";   // 初始化配置 SAConfigOptions saConfigOptions = new SAConfigOptions(SA_SERVER_URL); // 开启全埋点 saConfigOptions.setAutoTrackEventType(SensorsAnalyticsAutoTrackEventType.APP_START |                 SensorsAnalyticsAutoTrackEventType.APP_END)                 // 开启日志,线上可传入 false 关闭                 .enableLog(true);   // 需要在主线程初始化神策 SDK SensorsDataAPI.startWithConfigOptions(this, saConfigOptions);
CODE


2.1.3. CocoaPods method

  • In Podfile filr add pod 'SensorsAnalyticsSDK','~> 2.1.17'
  • Open the terminal and switch to the project directory
  • Execute pod install orpod update

Note: If executing pod update does not detect the latest version, you can first execute pod cache clean SensorsAnalyticsSDK to clear the local cache.


2.1.4. Source Code

2.1.4.1. Download SensorsAnalyticsSDK

Get the source code of the SDK from GitHub

SensorsAnalyticsSDK includes the following directories, which can be reduced according to the actual situation

  • WebView: If UIWebView is used in the project and needs to be integrated, this directory can be retained; at the same time, delete the WKWebView directory;
  • WKWebView: If UIWebView is not used in the project but WKWebView is used and needs to be integrated, this directory can be retained; at the same time, delete the WebView directory;
  • SensorsAnalyticsSDK.bundle: Contains some resource files required by the SDK, must be retained;
  • Location: If the project does not require collecting location-related information, this directory can be deleted;
  • Core: The core implementation of the SDK, must be retained.

2.1.4.2. Add source code files

After reducing the relevant functions, drag SensorsAnalyticsSDK into the Classes directory;

Open the project and refer to SensorsAnalyticsSDK

2.1.4.2.1. ADD Header Search Paths

在 target -> Build Settings -> Header Search Paths add file path


2.1.4.2.2. Change Memory Management

Since the project created by cocos2d-x defaults to MRC memory management, and SensorsAnalyticsSDK adopts ARC memory management, you can modify the memory management method of SensorsAnalyticsSDK.


2.1.4.3. Add System Dependencies

In the project Settings "Build Phase" -> "Link Binary With Libraries", add the dependency libraries libsqlite3, CoreTelephony.framework, SystemConffiguration.framework, CoreLocation.framework (Imported the Location folder), WebKit.frame (Imported the WKWebView folder).


Version Requirements

2.1.5. Initialize Sensors Analytics SDK

Get the project's data receiving URL

The initialization of SDK can be done by finding the class that implements UIApplicationDelegate protocol in the main function, and adding the initialization code in the - application: didFinishLaunchingWithOptions: method of that class:

// 引入神策分析 SDK #import <SensorsAnalyticsSDK/SensorsAnalyticsSDK.h>   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       // 初始化配置     SAConfigOptions *options = [[SAConfigOptions alloc] initWithServerURL:<#数据接收地址#> launchOptions:launchOptions];     // 开启全埋点     options.autoTrackEventType = SensorsAnalyticsEventTypeAppStart |                                  SensorsAnalyticsEventTypeAppEnd; #ifdef DEBUG     // 开启 Log     options.enableLog = YES; #endif       // 初始化 SDK     [SensorsAnalyticsSDK startWithConfigOptions:options];     ...     return YES; }
CODE