# Cpp
TIP
Before you begin, please read Preparation before Data Ingestion
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.9
Update time: November 01, 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/cpp
folder.
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, zlib and openssl 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.
Download the openssl library, https://github.com/openssl/openssl, select the library of the specified platform.
WARNING
In addition to the command line, you can also use IDE (Integrated Development Environment), such as Visual Studio, Clion, etc. to generate libraries for the corresponding 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)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # x64
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
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()
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 Libraries Compile by yourself
- Download the SDK code and cd to the cmakelist file directory
- Execute the following command
//Windows platform uses visual studio to compile
//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
// arm64 m1
arch -arm64 cmake -B build
arch -arm64 cmake --build build
//x86_64 intel
arch -x86_64 cmake -B build
arch -x86_64 cmake --build build
# 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.
# 1.3.4 Use compiled library
SDK Download (opens new window)
After downloading and decompressing, you can find the corresponding dll files for x64 and x86 platforms, which can be used directly.
# 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 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);
}
#
#