目录
此内容是否有帮助?

# Cpp

::: Tips

Before you begin, please read Preparation before Data Ingestion (opens new window)

You can get the C++ SDK Source Code (opens new window) on GitHub.

Supported platforms: Mac , Windows .

Supported C++ Version: 11 and above versions, and the size is about 144KB

:::

Latest version: 1.3.4

Update time: March 30, 2023

Resource download: SDK Source Code (opens new window)

# 1. SDK Integration

# 1.1 Download SDK

Download the SDK source code, unzip it and enter cpp-client/cppfolder.

include is the SDK header file.

src is the SDK source code file.

thirdparty is the dependent third-party library.

# 1.2 Dependencies

The C++ SDK depends on sqlite, curl, and zlib libraries. Due to differences in platform feature, the formats of the libraries are also different. You can use the compiled library, or you can compile the library yourself.

# 1.2.1 Libraries compiled using TE

The library is generated by the MGWin compiler on the x64 platform of Windows, and the compiled library in the thirdparty folder is configured into the project. (.dll is a dynamic library, .lib is a static library)

# 1.2.2 Libraries Compile by yourself

Here take the windows platform as an example:

Download the curl library, https://github.com/curl/curl/releases/tag/curl-7_61_1. After decompression, switch to the winbuild directory and compile. The compilation command is as follows. The compilation result is under the curl directory builds.

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

Download the zlib library, https://github.com/madler/zlib/releases/tag/v1.2.11, after decompression, execute the compilation command, the compilation result is in the zlib directory.

nmake -f win32/Makefile.msc

Download the sqlite library, https://www.sqlite.org/download.html, select the library of the specified platform.

# 1.3 SDK Integration

# 1.3.1 CMake

Use CMake to integrate the C++ SDK, copy the cpp folder to the project, and add the following configurations for Windows and Mac in the CMakeLists.txt file:

Set version of C++:

set(CMAKE_CXX_STANDARD 11)

Import header file:

include_directories(cpp/include)

Windows platform configuration:

if(WIN32)
    include_directories(cpp/thirdparty/curl/include cpp/thirdparty/zlib/include cpp/thirdparty/sqlite/include)
    link_directories(cpp/thirdparty/curl/lib cpp/thirdparty/zlib/lib cpp/thirdparty/sqlite/lib cpp/thirdparty/tasqlite/lib)
    add_library(thinkingdata 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)
    target_link_libraries(thinkingdata libcurl zlib libsqlite_share)
endif()

Mac platform configuration:

if (CMAKE_HOST_APPLE)
    find_library(COCOA Cocoa)
    find_library(IOKIT IOKit)
    add_library(thinkingdata 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)
    target_link_libraries(thinkingdata curl z sqlite3 ${COCOA} ${IOKIT})
endif()

# 1.3.2 Libraries Compile by yourself

You can use IDE (Integrated Development Environment) to compile TE library by yourself, for example, use Clion to open cpp-client, and after running the project, find a library named libthinkingdata in the cmake-build-release directory. Configure the libthinkingdata library into the project.

# 1.3.3 Using source code integration

After completing the three-party dependency configuration in step 1 and 2, add the include and src folders to the project.

# 2. Initialization

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

using namespace thinkingdata;

ThinkingAnalyticsAPI::Init(SERVER_URL, APPID);

Instruction on parameters:

  • APPID: The APPID of your project, which can be found on the project management page of the TE.
  • SERVER_URL:
    • If you are using a SaaS version, please check the receiver URL on this page.
    • If you are using a privatized deployment version, please bind the data tracking URL with a domain name and configure it with an HTTPS certificate: https://bind the domain name with the data tracking URL.

# 3. Common Features

We suggested that you read User identification rules (opens new window) before using common features; SDK would generate a random number that would be used as the distinct ID, and save the ID locally. Before the user logs in, the distinct ID would be used as the identification ID. Note: The distinct ID would change after the user reinstalled the APP or used the APP with a new device.

# 3.1 Login

When the users log in , login could be called to set the account ID of the user. TE platform would use the account ID as the identification ID, and this ID would be saved before logout is called. The previous account ID would be replaced if login has been called multiple times.

// The login unique identifier of the user, corresponding to the #account_id in data tracking. #Account_id now is TE
ThinkingAnalyticsAPI::Login("TE");

Login events wouldn't be uploaded in this method.

# 3.2 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:

TDJSONObject event_properties;
event_properties.SetString("name1", "name1"); //string
event_properties.SetNumber("test_number_int", 3); //number
event_properties.SetBool("test_bool", true); //boolean
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 string type. It could only start with a character and could contain figures, characters, and an underline "_", with a maximum length of 50 characters.

# 3.3 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. The data type of newly-created user properties must be the same as the uploaded properties. User name setting is taken as the example here:

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

# 4. Best Practice

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

#include "ta_analytics_sdk.h"
using namespace thinkingdata;

if (privacy policy is authorized) {
    ThinkingAnalyticsAPI::Init(SERVER_URL, APPID);
    ThinkingAnalyticsAPI::Login("TE");
    TDJSONObject event_properties;
    event_properties.SetString("name1", "name1");//string
    event_properties.SetNumber("test_number_int", 3);//number
    event_properties.SetBool("test_bool", true);//boolean
    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);;
    
    TDJSONObject userProperties;
    userProperties1.SetString("user_name", "TE");
    ThinkingAnalyticsAPI::UserSet(userProperties);
}

#

#

#