Skip to main content

C++

Last updated 10/05/2026
tip

Before you integrate the SDK, read Pre-integration preparation.

You can get the C++ SDK source code on GitHub.

The C++ SDK runs on Mac and Windows, supports C++ 11 and later, and is about 144KB in size

Latest version: v1.5.3

Update time: 2025-10-11

Downloads: SDK source code

1. Integrate the SDK​

1.1 Download the SDK​

Download the SDK source code, unzip it, and go to the cpp-client/cpp folder.

include contains the SDK header files, src contains the SDK source code files, and thirdparty contains the third-party libraries that the SDK depends on.

cpp-client
├── CMakeLists.txt
├── cpp
│ ├── include
│ ├── src
│ └── thirdparty
└── main.cpp

1.2 Add dependencies​

The C++ SDK depends on the sqlite, curl, zlib, and openssl libraries. Because platforms differ, the library formats also differ. You can use the libraries compiled by ThinkingAI or compile the libraries yourself.

1.2.1 Use the libraries compiled by ThinkingAI​

These libraries were generated by a compiler on the Windows platform. Configure the compiled libraries in the thirdparty folder in your project. (.dll files are dynamic libraries, and .lib files are static libraries)

thirdparty
├── x64
│ ├── curl
│ ├── openssl
│ ├── sqlite
│ └── zlib
└── x86

1.2.2 Compile them yourself​

The following uses the Windows platform as an example:

Download the curl library from https://github.com/curl/curl/releases/tag/curl-7_61_1. Unzip it, switch to the winbuild directory, and compile it with the following command. The output is in the builds directory under curl.

nmake /f Makefile.vc mode=static ENABLE_IDN=no

Download the zlib library from https://github.com/madler/zlib/releases/tag/v1.2.11. After you unzip it, run the compile command. The build output is in the zlib directory.

nmake -f win32/Makefile.msc

Download the sqlite library from https://www.sqlite.org/download.html and choose the library for your platform.

note

Besides the command line, you can also use an IDE (integrated development environment), such as Visual Studio or CLion, to generate the libraries for the corresponding platform.

Download the openssl library from https://github.com/openssl/openssl and compile the library for your platform yourself.

1.3 Integrate the SDK​

1.3.1 Integrate with CMake​

To integrate the C++ SDK with CMake, copy the cpp folder into your project, and add the following configurations for Windows and Mac respectively in the CMakeLists.txt file:

Set the C++ version:

set(CMAKE_CXX_STANDARD 11)

Include the header files:

include_directories(cpp/include)

Add the Windows platform configuration:

if(WIN32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # x64 platform
include_directories(cpp/thirdparty/x64/curl/include cpp/thirdparty/x64/zlib/include cpp/thirdparty/x64/sqlite/include cpp/thirdparty/x64/openssl/include)
link_directories(cpp/thirdparty/x64/curl/lib cpp/thirdparty/x64/zlib/lib cpp/thirdparty/x64/sqlite/lib cpp/thirdparty/x64/openssl/lib)
else() # Win32 platform
include_directories(cpp/thirdparty/x86/curl/include cpp/thirdparty/x86/zlib/include cpp/thirdparty/x86/sqlite/include cpp/thirdparty/x86/openssl/include)
link_directories(cpp/thirdparty/x86/curl/lib cpp/thirdparty/x86/zlib/lib cpp/thirdparty/x86/sqlite/lib cpp/thirdparty/x86/openssl/lib)
endif()
add_library(thinkingdata SHARED cpp/src/ta_analytics_sdk.cpp cpp/src/ta_cpp_helper.cpp cpp/src/ta_cpp_network.cpp cpp/src/ta_cpp_utils.cpp cpp/src/ta_sqlite.cpp cpp/src/ta_timer.cpp cpp/src/ta_event_task.cpp cpp/src/ta_cpp_send.cpp cpp/src/ta_json_object.cpp cpp/src/ta_cJSON.c cpp/src/ta_encrypt.cpp cpp/src/ta_calibrated_time.cpp cpp/src/ta_flush_task.cpp)
target_link_libraries(thinkingdata libcurl sqlite3 zlibwapi libssl libcrypto)
endif()

Add the Mac platform configuration:

if (CMAKE_HOST_APPLE)
find_library(COCOA Cocoa)
find_library(IOKIT IOKit)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
link_directories(${OPENSSL_LIBRARIES})
add_library(thinkingdata SHARED cpp/src/ta_analytics_sdk.cpp cpp/src/ta_cpp_helper.cpp cpp/src/ta_cpp_network.cpp cpp/src/ta_cpp_utils.cpp cpp/src/ta_sqlite.cpp cpp/src/ta_timer.cpp cpp/src/ta_event_task.cpp cpp/src/ta_cpp_send.cpp cpp/src/ta_json_object.cpp cpp/src/ta_cJSON.c cpp/src/ta_mac_tool.mm cpp/src/ta_encrypt.cpp cpp/src/ta_calibrated_time.cpp cpp/src/ta_flush_task.cpp)
target_link_libraries(thinkingdata curl z sqlite3 ${OPENSSL_LIBRARIES} ${COCOA} ${IOKIT})
endif()

1.3.2 Compile it yourself​

You can use an IDE (integrated development environment) to compile the ThinkingAI libraries yourself

  1. Download the SDK code and cd to the directory that contains the CMakeLists file
  2. Run the following commands
//Windows platform: compile with Visual Studio
//x86
cmake -S . -B build -G "Visual Studio 17 2022" -A Win32
//x64
cmake -S . -B build -G "Visual Studio 17 2022" -A x64
//Mac platform
// arm64 platform m1
arch -arm64 cmake -B build
arch -arm64 cmake --build build

//x86_64 platform intel
arch -x86_64 cmake -B build
arch -x86_64 cmake --build build

1.3.3 Integrate from source​

After you complete the third-party dependency configuration in step 1.2, add the include and src folders to your project.

.
├── cmake-build-debug
├── cmake-build-release
├── CMakeLists.txt
├── cpp
│ ├── include
│ ├── src
│ └── thirdparty
└── main.cpp

1.3.4 Use the libraries prebuilt by ThinkingAI​

SDK download

After you download and unzip it, you can find the DLL files for the x64 and x86 platforms and use them directly.

2. Initialize the SDK​

#include "ta_analytics_sdk.h"
#include "ta_json_object.h"

using namespace thinkingdata;

ThinkingAnalyticsAPI::Init(SERVER_URL, APPID);

Parameters:

  • APPID: The APPID of your project, which you can find on the Project Settings page in the AE backend

  • 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

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
ThinkingAnalyticsAPI::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 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 event_properties;
event_properties.SetString("name1", "name1");//String
event_properties.SetNumber("test_number_int", 3);//Number
event_properties.SetBool("test_bool", true);//bool
event_properties.SetDateTime("test_time1", time(NULL), 0);//Time
std::vector<std::string> test_list;
test_list.push_back("item11");
test_list.push_back("item21");
event_properties.SetList("test_list1", test_list);//Array
ThinkingAnalyticsAPI::Track("CPP_event", event_properties);
  • 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.

  • Event properties are of the TDJSONObject type, where each element represents a property;

    • The event property Key is the property name and 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. It is not case-sensitive;
    • Property values support the string, number, Boolean, array, and time types.

User properties have the same requirements as event properties

3.3 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:

TDJSONObject userProperties;
userProperties.SetString("user_name", "TA");
ThinkingAnalyticsAPI::UserSet(userProperties);

4. Best practices​

The following sample code includes all of the operations above. We recommend using them in the following order:

#include "ta_analytics_sdk.h"
using namespace thinkingdata;
//Initialize the SDK
ThinkingAnalyticsAPI::Init(SERVER_URL, APPID);
//If the user has logged in, you can set the user's account ID as the unique identifier
ThinkingAnalyticsAPI::Login("TA");
//Send an event
TDJSONObject event_properties;
event_properties.SetString("name1", "name1");//String
event_properties.SetNumber("test_number_int", 3);//Number
event_properties.SetBool("test_bool", true);//bool
event_properties.SetDateTime("test_time1", time(NULL), 0);//Time
std::vector<std::string> test_list;
test_list.push_back("item11");
test_list.push_back("item21");
event_properties.SetList("test_list1", test_list);//Array
ThinkingAnalyticsAPI::Track("CPP_event", event_properties);
//Set user properties
TDJSONObject userProperties;
userProperties.SetString("user_name", "TA");
ThinkingAnalyticsAPI::UserSet(userProperties);
Was this page helpful?