目录
此内容是否有帮助?

# C++

TIP

Before you begin, please read Preparation before Data Ingestion.

Latest version: 1.2.1

Update time: 2023-01-11

Resource download: Source Code (opens new window)

# SDK Integration

1.1 Use the static library compiled by ThinkingData (compiled in windows10)

Download the source code, go to the libs folder and copy the include folder to the project. It contains the header file.

If logconsumer is used, import the libthinkingdata.a file.

If batchconsumer is used, import the libthinkingdata_batch.a file.

If debugconsumer is used, import the libthinkingdata_debug.a file.

1.2 Compile the static library manually

Download C++ SDK source code, open the cpp folder by IDE.

(1). Modify the CMakeLists.txt file, then compile logconsumer.

cmake_minimum_required(VERSION 3.12)
project(cpp-server-sdk)

set(CMAKE_CXX_STANDARD 11)

include_directories(include)

# log consumer
add_library(thinkingdata src/ThinkingAnalyticsAPI.cpp src/TAUtils.cpp src/TALoggerConsumer.cpp)
add_executable(program example/program.cpp)
target_link_libraries(program thinkingdata)

Run program.cpp in the example folder and get the libthinkingdata.a file from the cmake-build folder.

(2). Modify the CMakeLists.txt file, then compile batch consumer.

cmake_minimum_required(VERSION 3.12)
project(cpp-server-sdk)

set(CMAKE_CXX_STANDARD 11)

include_directories(include)

# batch consumer
add_library(thinkingdata_batch src/ThinkingAnalyticsAPI.cpp src/TAUtils.cpp src/TABatchConsumer.cpp src/TANetwork.cpp)
if(WIN32)
    include_directories(thirdParty/curl/include)
    link_directories(thirdParty/curl/lib)
    target_link_libraries(thinkingdata_batch libcurl)
else ()
    target_link_libraries(thinkingdata_batch curl)
endif()
add_executable(batchConsmerDemo example/batchConsmerDemo.cpp)
target_link_libraries(batchConsmerDemo thinkingdata_batch)

Run batchConsmerDemo.cpp in the example folder and get the libthinkingdata_batch.a file from the cmake-build folder.

(3). Modify the CMakeLists.txt file, then compile debug consumer.

cmake_minimum_required(VERSION 3.12)
project(cpp-server-sdk)

set(CMAKE_CXX_STANDARD 11)

include_directories(include)

# debug consumer
add_library(thinkingdata_debug src/ThinkingAnalyticsAPI.cpp src/TAUtils.cpp src/TADebugConsumer.cpp src/TANetwork.cpp)
if(WIN32)
    include_directories(thirdParty/curl/include)
    link_directories(thirdParty/curl/lib)
    target_link_libraries(thinkingdata_debug libcurl)
else ()
    target_link_libraries(thinkingdata_debug curl)
endif()
add_executable(debugConsmerDemo example/debugConsumerDemo.cpp)
target_link_libraries(debugConsmerDemo thinkingdata_debug)

Run debugConsumerDemo.cpp in the example folder and get the libthinkingdata_debug.a file from the cmake-build folder.

1.3 Logbus Integration

We recommend using SDK+LogBus to track and report data on server. You can refer to the following documents to complete the installation of Logbus: LogBus User Guide

# Initialization

The following is the sample code for SDK initialization:

// Import header file (adjust the path yourself)
#include "thirdPark/tacpp/include/tacpp/ThinkingDataAnalyticsSDK.h"
string LOG_DIRECTORY = "../trackLog";
TaSDK::LoggerConsumer::Config config = TaSDK::LoggerConsumer::Config(LOG_DIRECTORY);
TaSDK::LoggerConsumer logConsumer = TaSDK::LoggerConsumer(config);
TaSDK::ThinkingDataAnalytics te = TaSDK::ThinkingDataAnalytics(logConsumer);

LOG_DIRECTORY is the local folder path.

# Common Features

In order to ensure that the distinct ID and account ID can be bound smoothly, if your game uses the distinct ID and account ID, we strongly recommend that you upload these two IDs at the same time, otherwise the account will not match, causing users to double count. For specific ID binding rules, please refer to the chapter on User identification rules (opens new window).

# 3.1 Sending Events

You can call track to upload events. It is suggested that you set event properties based on the data tracking plan drafted previously. Here is an example of a user buying an item:

TaSDK::PropertiesNode event_properties;
event_properties.SetString("name1", "XZ_debug");
event_properties.SetString("name2", "logbugs");
event_properties.SetString("name3", "name3");
event_properties.SetString("#uuid", "1234567890");
event_properties.SetNumber("test_number_int", 3);
event_properties.SetNumber("test_number_double", 3.14);
event_properties.SetBool("test_bool", true);
std::string test_string = "test_string";
event_properties.SetString("test_stl_string1", test_string);
event_properties.SetDateTime("test_time1", time(nullptr), 0);
timeb t = {};
ftime(&t);
event_properties.SetDateTime("#time", t.time, t.millitm);
std::vector<std::string> test_list;
test_list.push_back("item11");
test_list.push_back("item21");
event_properties.SetList("test_list1", test_list);

// track event
te.track(accountId, distincId, eventName, event_properties);
  • Key is the name of the property and refers to the string type. It must start with a character, and contain numbers, characters (insensitive to case, and upper cases would be transformed into lower cases by TE) and underscores "_", with a maximum length of 50 characters.
  • Value, the value of the property, supports string, numbers, Boolean, time, object, array object, and array

The requirements for event properties and user properties are the same as that for super properties

# 3.2 User Properties

You can set general user properties by calling user_set API. The original properties would be replaced by the properties uploaded via this API. If no user properties are set before, user properties will be newly created. The type of newly-created user properties must conform to that of the uploaded properties. User name setting is taken as the example here:

// user_set
TaSDK::PropertiesNode userSet_properties;
userSet_properties.SetString("userName", "test");

te.user_set(accountId, distincId, userSet_properties);

# Best Practice

The following sample code covers all the above-mentioned operations. It is recommended that the codes be used in the following steps:

// Import header file (adjust the path yourself)
#include "thirdPark/tacpp/include/tacpp/ThinkingDataAnalyticsSDK.h"
string LOG_DIRECTORY = "../trackLog";
TaSDK::LoggerConsumer::Config config = TaSDK::LoggerConsumer::Config(LOG_DIRECTORY);
TaSDK::LoggerConsumer logConsumer = TaSDK::LoggerConsumer(config);
TaSDK::ThinkingDataAnalytics te = TaSDK::ThinkingDataAnalytics(logConsumer);

// track event
TaSDK::PropertiesNode event_properties;
event_properties.SetString("name1", "XZ_debug");;
event_properties.SetNumber("test_number_int", 3);
event_properties.SetBool("test_bool", true);
te.track(accountId, distincId, eventName, event_properties);

// upload user properties
TaSDK::PropertiesNode userSet_properties;
userSet_properties.SetString("userName", "test");
te.user_set(accountId, distincId, userSet_properties);

// "flush()" is immediately written to the file.
// call "flush()" API is not necessary
te.flush();