Cocos2d-x
Before you integrate the SDK, read Pre-integration preparation.
The Cocos2d-x SDK runs on iOS, Android, Mac, and Windows, and is about 6.8M in size
Latest version: v2.0.4
Update time: 2026-02-04
Downloads: Source code, SDK download
This document applies to v2.0.0 and later. For earlier versions, see Cocos2d-x Integration Guide (V1) and SDK download (v1.3.5)
1. Integrate the SDK
- Download the Cocos2d-x SDK resource files, unzip them, and put the ThinkingAnalytics folder into the Classes folder
- Add the following configuration to
CMakeLists.txt
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Common/TDJSONObject.cpp
)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Common/TDJSONObject.h
Classes/ThinkingAnalytics/Common/TDAnalytics.h
)
if(ANDROID)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Android/TDAnalytics.cpp
)
elseif(WINDOWS)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Other/ThinkingSDKObject.h
)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Other/TDAnalytics.cpp
)
elseif(MACOSX)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Other/ThinkingSDKObject.h
)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Other/TDAnalytics.cpp
)
endif()
- Add the Android configuration
- Add a libs folder under the app directory in proj.android, and copy TDAnalytics.aar, TDCore.aar, and TDThirdparty.aar into the libs folder
- Under the app/src directory in proj.android, create the cn/thinkingdata/analytics folders level by level, and copy TDAnalyticsCocosAPI.java into the cn/thinkingdata/analytics folder
- Add the following configuration to build.gradle under the app directory in proj.android
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
}
- Add the iOS configuration
Compile the iOS project with the ios.toolchain.cmake tool
- Add the iOS-related configuration to
CMakeLists.txt
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Common/TDJSONObject.cpp
)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Common/TDJSONObject.h
Classes/ThinkingAnalytics/Common/TDAnalytics.h
)
if(ANDROID)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Android/TDAnalytics.cpp
)
elseif(WINDOWS)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Other/ThinkingSDKObject.h
)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Other/TDAnalytics.cpp
)
elseif(IOS)
# iOS configuration
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/iOS/TDAnalyticsCocosAPI.h
)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/iOS/TDAnalytics.mm
Classes/ThinkingAnalytics/iOS/TDAnalyticsCocosAPI.m
)
elseif(MACOSX)
list(APPEND GAME_HEADER
Classes/ThinkingAnalytics/Other/ThinkingSDKObject.h
)
list(APPEND GAME_SOURCE
Classes/ThinkingAnalytics/Other/TDAnalytics.cpp
)
endif()
if(IOS)
# iOS configuration
set(CMAKE_EXE_LINKER_FLAGS -ObjC)
endif()
- Open the iOS project in Xcode, and drag
ThinkingSDK.frameworkandTAThirdParty.frameworkfrom the iOS folder under ThinkingAnalytics into the Classes/ThinkingAnalytics/iOS directory of the Xcode project, as shown below
- After the configuration is complete, if your version is earlier than 2.0.2, manually change the header import in Classes/ThinkingAnalytics/iOS/TDAnalyticsCocosAPI.m
#import "ThinkingAnalyticsSDK.h"
//Change it to the following
#import <ThinkingSDK/ThinkingSDK.h>
Open the Xcode project in proj.ios_mac directly
- Open the iOS project in Xcode, and drag the Common and iOS folders under ThinkingAnalytics into the Classes directory of the Xcode project, as shown below
- Add
-ObjCtoBuild Setting->Other Linker Flags
- Add the following to
Build Setting-Framework Search Paths
"$(SRCROOT)/../Classes/ThinkingAnalytics/iOS"
Build Phases-Link Binary With Libraries
WebKit.framework, GameController.framework, MediaPlayer.framework
- After the configuration is complete, if your version is earlier than 2.0.2, manually change the header import in Classes/ThinkingAnalytics/iOS/TDAnalyticsCocosAPI.m
#import "ThinkingAnalyticsSDK.h"
//Change it to the following
#import <ThinkingSDK/ThinkingSDK.h>
2. Initialization
#include "./ThinkingAnalytics/Common/TDAnalytics.h"
using namespace thinkingdata::analytics;
// Initialize the SDK on the main thread
//Method 1
TDAnalytics::init(TA_APP_ID, TA_SERVER_URL);
//Method 2
TDConfig config(TA_APP_ID, TA_SERVER_URL);
TDAnalytics::init(config);
Parameters:
-
APPID: The APPID of your project, which you can find on the Project Settings page in AE -
SERVER_URL: The URL that data is uploaded to- If you use the cloud service, enter: https://global-receiver-ta.thinkingdata.cn
- If you use an on-premises deployment, bind a domain name to the data collection URL and configure an HTTPS certificate: https://your-domain-for-data-collection
Android 9.0 and later restrict HTTP requests by default, so make sure you use HTTPS.
3. Common features
Before you use the common features, we recommend that you read the user identification rules. By default, the SDK generates a random number as the distinct ID and stores it locally. Before a user logs in, the distinct ID is used as the user's identifier. Note: The distinct ID changes when the user reinstalls the app or switches devices.
3.1 Set the account ID
When a user logs in, you can call Login to set the user's account ID. The AE platform uses the account ID as the identifier, and the account ID you set is kept until Logout is called. Calling Login multiple times overwrites the previous account ID.
// The unique login identifier of the user, which corresponds to #account_id in the reported data. In this case, the value of #account_id is TA
TDAnalytics::login("TA");
You can call Login multiple times. Each call checks whether the account ID passed in matches the previously saved ID. If they match, the call is ignored; otherwise, the previous ID is overwritten.
This method does not upload a login event
3.2 Set super properties
Super properties are properties that every event carries. You can call setSuperProperties to set super properties. We recommend setting super properties before you send events. Some important properties, such as a user's membership level and source channel, need to be set in every event; in this case, you can set them as super properties.
TDJSONObject superProperties;
superProperties.setString("channel", "ta");//String
superProperties.setNumber("age",1);//Number
superProperties.setBool("isSuccess",true);//Boolean
superProperties.setDateTime("birthday","2020-01-02 10:52:52.290");//Time
TDJSONObject object;
object.setString("key", "value");
superProperties.setJsonObject("object", object);// Object
TDJSONObject object1;
object1.setString("key", "value");
vector<TDJSONObject> arr;
arr.push_back(object1);
superProperties.setList("object_arr", arr); // Object group
vector<string> arr1;
arr1.push_back("value");
superProperties.setList("arr",arr1);//Array
TDAnalytics::setSuperProperties(superProperties);
Super properties are saved in the cache, so you don't need to call this every time the app starts. If you call setSuperProperties to upload a super property that was set before, the new value overwrites the previous one.
- Event properties are of the
TDJSONObjecttype, where each element represents a property - Key is the name of the property and is of the string type. It must start with a letter, can contain only digits, letters, and underscores "_", and can be up to 50 characters long. Keys are not case-sensitive; AE converts all letters to lowercase
- Value is the value of the property. Supported types are string, number, Boolean, time, object, object group, and array
Event properties and user properties have the same requirements as super properties
3.3 Enable auto-tracking
The following code example enables the install, start, and end events. To learn more about the SDK's auto-tracking capabilities, see Auto-tracking in detail
// Enable auto-tracking
TDAnalytics::enableAutoTrack();
3.4 Send events
You can call Track to upload events. We recommend setting event properties and the conditions for sending them according to the tracking plan you prepared earlier. The following example tracks a user purchasing a product:
TDJSONObject eventProperties;
eventProperties.setString("product_name", "Product Name");
TDAnalytics::track("product_buy", eventProperties);
The event name is of the string type. It must start with a letter, can contain digits, letters, and underscores "_", and can be up to 50 characters long.
3.5 Set user properties
For general user properties, you can call userSet to set them. Properties uploaded through this API overwrite the existing property values. If the user property did not exist before, it is created with the same type as the value passed in. The following example sets the user name:
//username is now TA
TDJSONObject properties;
properties.setString("username", "TA");
TDAnalytics::userSet(properties);
//userName is now AE
TDJSONObject newProperties;
newProperties.setString("username", "TE");
TDAnalytics::userSet(newProperties);
Property format requirements are the same as for event properties.
4. Best practices
The following sample code includes all of the operations above. We recommend using them in the following order:
#include "./ThinkingAnalytics/Common/TDAnalytics.h"
using namespace thinkingdata::analytics;
if (privacy policy authorized) {
//Initialize the SDK
TDAnalytics::init(TA_APP_ID, TA_SERVER_URL);
//Enable auto-tracked events
TDAnalytics::enableAutoTrack();
//If the user has logged in, you can set the user's account ID as the unique identifier
TDAnalytics::login("TA");
//After you set super properties, every event carries them
TDJSONObject superProperties;
superProperties.setString("channel", "ta");//String
superProperties.setNumber("age",1);//Number
superProperties.setBool("isSuccess",true);//Boolean
superProperties.setDateTime("birthday","2020-01-02 10:52:52.290");//Time
TDJSONObject object;
object.setString("key", "value");
superProperties.setJsonObject("object", object);// Object
TDJSONObject object1;
object1.setString("key", "value");
vector<TDJSONObject> arr;
arr.push_back(object1);
superProperties.setList("object_arr", arr); // Object group
vector<string> arr1;
arr1.push_back("value");
superProperties.setList("arr",arr1);//Array
TDAnalytics::setSuperProperties(superProperties);
//Send an event
TDJSONObject eventProperties;
eventProperties.setString("product_name", "Product Name");
TDAnalytics::track("product_buy",eventProperties);
//Set user properties
TDJSONObject userProperties;
userProperties.setString("username", "TE");
TDAnalytics::userSet(userProperties);
}

