Integrated Sensors analytics SDK
SDK source file can be got from https://github.com/sensorsdata/sa-sdk-cpp. When used, the code can be integrated directly into the target project, or compiled into a library before being introduced, the SDK source code includes:
version abovev1.0.0
./include/sensors_analytics_sdk.h ./include/sensors_network.h ./include/sensors_utils.h ./src/sensors_analytics_sdk.cpp ./src/sensors_network.cpp ./src/sensors_utils.cpp
version below v1.0.0
./include/sensors_analytics_sdk.h ./src/sensors_analytics_sdk.cpp
Third-party libraries that the SDK relies on include:
1. curl:For network requests;
2. zlib:For gzip compression.
Other notes:
- from github vs2005 of the branch code supports compilation and use in Visual Studio 2005 version of C++.
- If you import a source code file directly and use Visual Studio to compile an error, see if the initial error is "warning C4819: This file contains characters that cannot be represented in the current code page (936). Please save the file in Unicode format to prevent data loss ", you can introduce several code files "file -> Advanced Save options -> Code -> Unicode-code page 1200 "then save and try compiling again. VS2017 and above can find this feature by searching for "Visual Studio 2017 Hide Advanced save options".
- The attribute value of the string type (mainly when the Chinese value is used) must be a valid UTF-8 encoding. Otherwise, stderr will report an error and cannot import the field. In this case, please check whether GBK encoding is used (for example, the source code file where the string is stored is stored in GBK encoding). To convert GBK encoded string to UTF-8, Windows can use MultiByteToWideChar, WideCharToMultiByte; Linux can use iconv, the specific method can be searched.
SDK dependency under Windows
You can try using our compiled dependency files directly, or compile them yourself.
Use curl and zlib that we compiled
We compiled curl and zlib for Windows XP on Visual Studio 2005. The method of use is as follows:
- Download curl source code,Right-click on the "Resource File" of the project -> stats -> C/C++ -> routine -> "Additional include Directory" adds the include directory under the curl source directory;
- Download zlib source code,right-click on the project name. Attribute -> C/C++ -> Conventional -> "Additional Include Directory" adds the zlib source directory;
- Download compiled curl and zlib lib file, Right-click on the "Resource File" of the project -> Add -> Existing item, select libcurl.dll、libcurl.lib、zlib.lib;
Later versions of Visual Studio can also introduce and use this precompiled library.
self-compilation curl
- Download curl source code: https://curl.se/download.html;
- Open the VS2017 developer command prompt in the Visual Studio directory in the Start menu and switch to the winbuild directory in the curl extraction directory;
- Execute the command to start compiling, see the code snippet below:
- The compiled output is in the curl directory builds.
nmake /f Makefile.vc mode=dll
If static compilation is required:
nmake /f Makefile.vc mode=static ENABLE_IDN=no
If you need to statically link curl, you need to:
- Linker -> General -> Additional Library Dependencies, add libcurl_a.lib;Ws2_32.lib;Wldap32.lib;winmm.lib;Crypt32.lib
- In C/C++ -> Preprocessor -> Preprocessor Definitions, add CURL_STATICLIB
In addition, if you want to encrypt the transmitted data using HTTPS, it is recommended to configure the use of OpenSSL when compiling curl for better compatibility, or use the pre-compiled curl provided by us.
Compile zlib on your own
- Download the zlib source code from the zlib official website https://zlib.net/
- Open the "VS2017 developer command prompt" in the Visual Studio directory in the start menu, and switch to the zlib extraction directory;
- Execute the command to start compiling:
- The compiled output is in the zlib directory.
nmake -f win32/Makefile.msc
Initialize the Sens Analytics SDK
Use it in your program
// 初始化 SDK // data_file_path: 暂存文件路径,用于将未发送的数据临时保存在磁盘 // server_url: 数据接收地址 // distinct_id: 标识一个用户的 ID // is_login_id: distinct_id 参数传值是否是一个“登录 ID” // max_staging_record_count: 在发送队列中最多保存的数据条数,若当前未发送数据条数达到该值, // 新埋点记录将淘汰最早的一个记录 sensors_analytics::Sdk::Init(staging_file_path, server_url, distinct_id, is_login_id, max_staging_record_size);
distinct_id is the ID that identifies the user. If there is no available ID when initializing the SDK, you can generate a random UUID as the distinct_id and set is_login_id to false. Save this ID and pass it in the next SDK initialization. If there is an available login ID, you can pass the value of the login ID and set is_login_id to true.