Menu

Integration Documentation (CocosCreator SDK)

Integration SDK

Integration

Get the CocosCreator SDK source code from GitHub, and integrate the corresponding js file according to the CocosCreator version you are using.

CocosCreator 3.x

If you are using CocosCreator version 3.x, please put the cocoscreator-game.cjs.min.js file in the assets/Script directory of your project.

CocosCreator 2.x

If you are using CocosCreator version 2.x, please put the cocoscreator-game.min.js file in the assets/Script directory of your project.

Referencing

Referencing the js header file in the pages where data collection is needed.

// CocosCreator 2.x import sensors from './cocoscreator-game.min.js' // CocosCreator 3.x import sensors from './cocoscreator-game.cjs.min.js' 

Configuring Initialization Parameters

After importing the SDK, you can configure the SDK initialization parameters through setPara().

// 以 CocosCreator 2.x 为例 import sa from './cocoscreator-game.js'; sa.setPara({ 	server_url: '数据接收地址', show_log: true });

Optional Initialization Parameters

The optional parameters in sensors.init are as follows:

Configuration Name

Default Value

Meaning

server_urlEmpty stringData receiving URL
show_logfalseShow logs

Initialize SDK

Call the init() method to initialize the SDK

// 以 CocosCreator 2.x 为例 import sa from './cocoscreator-game.js'; sa.setPara({ 	server_url: '数据接收地址', show_log: true }); // 初始化 SDK sensors.init();

SDK Configuration

Configure project data receiving URL

Sensors Analytics v2.2 and above can obtain the data receiving URL as shown in the following figure. For obtaining the data receiving URL in other versions, you can switch to the corresponding document version for viewing:

Track events with code tracking

After initializing the SDK, you can track user behavior events using the - track method and add custom properties to the events:

import sa from './cocoscreator-game.js' sa.track('click',{ 	name: '点击' });

Set event common properties

For properties that need to be added to all events, you can register the properties as common properties using the - registerApp: method after initializing the SDK:

import sa from './cocoscreator-game.js' // 调用自定义埋点 API var p = {"AppName":"cocos"} sa.registerApp(p);

The common properties will be stored in the local cache of the app. You can use - clearAppRegister: to delete the set common properties.

Set user properties

- setProfile: method can be used to set user properties. When the same key is set multiple times, the value will be overwritten:

var p = {"age":5} sa.setProfile(p)

Preserve initial user properties

For properties that need to be valid only when first set, such as the initial recharge amount and the initially set nickname of the user, you can use the -setOnceProfile: interface to record them. Different from the - setProfile: method, if the set user property already exists, this record will be ignored and not overwrite the existing data. If the property does not exist, it will be automatically created:

// 设定用户 AdSource 渠道为为 "App Store" var p = {"AdSource":"App Store"} sa.setOnceProfile(p)

User login

When a user registers successfully or logs in, the SDK's - login: interface should be called:

sa.login("用户登录 ID");

To accurately record the behavior information of logged-in users, it is recommended to call the - login: interface at the following times:

· When a user registers successfully · When a user logs in successfully · Each time a logged-in user starts the app

Native Support

Android Native Support

According to the version of CocosCreator you are using, follow the steps below to configure the Android project.

CocosCreator 2.x Version

Integrate Native SDK

Please integrate the Sensors Analytics native SDK dependency in the  /build/jsb-link/frameworks/runtime-src/proj.android-studio/app/build.gradle file in the project directory.

dependencies { // 添加 Sensors Analytics SDK 依赖 implementation 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.0.0' }

The CocosCreator SDK uses delayed initialization and requires the native SDK version 6.0.0 or above to be referenced.

Configure the Bridging File

In the java working directory of /build/jsb-link/frameworks/runtime-src/proj.android-studio/app/src in the project directory, copy the bridging file CCSensorsDataAPI.java.

And in the proguard-rules.pro file at the same level as the scr directory, add obfuscation.

-keep public class com.cocos.game.CCSensorsDataAPI {*;}

CocosCreator 3.x Version

Integrate Native SDK

Please integrate the Sensors Analytics native SDK dependency in the native/engine/android/app/build.gradle file in the project directory.

dependencies { // 添加 Sensors Analytics SDK 依赖 implementation 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.0.0' }

The CocosCreator SDK uses delayed initialization and requires the native SDK version 6.0.0 or above to be referenced.

Configure the Bridging File

In the java working directory of native/engine/android/app/src in the project directory, copy the bridging file CCSensorsDataAPI.java.

And add obfuscation to the proguard-rules.pro file at the same level as the scr directory

-keep public class com.cocos.game.CCSensorsDataAPI {*;}

iOS Native Support

Configure the iOS project according to the following steps based on the version of CocosCreator you are using.

CocosCreator 2.x

Add iOS plugin files

After building the iOS project, the corresponding iOS project will be generated. Open the iOS project with Xcode and add the Sensors Analytics iOS plugin files.

Configure ARC compilation flag

CocosCreator 3.x

Add iOS plugin files

After the first build of the iOS project, the ./native/engine/ directory will be generated. Copy the Sensors Analytics iOS plugin file to the ./native/engine/ios/ directory.

Configure CMakeLists.txt

Open the ./native/engine/ios/CMakeLists.txt file and start the configuration.

Add source file references and ARC compilation flag

# 找到这行代码 list(APPEND PROJ_SOURCES ${CMAKE_CURRENT_LIST_DIR}/AppDelegate.mm ${CMAKE_CURRENT_LIST_DIR}/AppDelegate.h ${CMAKE_CURRENT_LIST_DIR}/service/SDKWrapper.m ${CMAKE_CURRENT_LIST_DIR}/service/SDKWrapper.h ${CMAKE_CURRENT_LIST_DIR}/main.m ${CMAKE_CURRENT_LIST_DIR}/ViewController.mm ${CMAKE_CURRENT_LIST_DIR}/ViewController.h ${UI_RESOURCES} ) # 1. 添加源文件引用 list(APPEND PROJ_SOURCES ${CMAKE_CURRENT_LIST_DIR}/CCSensorsDataAPI.h ${CMAKE_CURRENT_LIST_DIR}/CCSensorsDataAPI.mm ${CMAKE_CURRENT_LIST_DIR}/SensorsAnalyticsSDK.framework ) # 2. 添加 ARC 编译标记 set_source_files_properties( ${CMAKE_CURRENT_LIST_DIR}/CCSensorsDataAPI.mm PROPERTIES COMPILE_FLAGS -fobjc-arc )

Link libraries

# 找到这行代码 target_link_libraries(${LIB_NAME} cocos2d) # 添加如下代码(链接 framework) target_link_libraries(${LIB_NAME} ${CMAKE_CURRENT_LIST_DIR}/SensorsAnalyticsSDK.framework)

After the configuration is complete, you need to rebuild with Cocos Creator for the configuration to take effect.

Enable Native support

Enable Native support by adding enable_native: true in setPara during SDK initialization.

sa.setPara({ server_url: '数据接收地址', show_log: true, app_start:true, //Android、iOS 支持启动事件 app_end:true, //Android、iOS 支持退出事件 enable_native:true, // 支持 native 数据采集 }); sa.init();
Last modified: 2025-01-02