-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy from f55df51 @https://github.com/sensorsdata/sa-sdk-cpp/tree/vs2005
- Loading branch information
nielyd
committed
Jun 24, 2020
1 parent
174eda8
commit 03e5cb6
Showing
3 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// Created by Feng Jiajie on 2018/9/6. | ||
// | ||
// https://www.sensorsdata.cn/_manual/cpp_sdk.html | ||
|
||
#include <string> | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include "sensors_analytics_sdk.h" | ||
|
||
using std::string; | ||
|
||
void SensorsDatTest(); | ||
|
||
std::string GenerateId(); | ||
int main() { | ||
SensorsDatTest(); | ||
return 0; | ||
} | ||
|
||
void SensorsDatTest() { | ||
// 暂存文件路径,该文件用于进程退出时将内存中未发送的数据暂存在磁盘,下次发送时加载 | ||
const string staging_file_path = "./staging_file"; | ||
|
||
// 服务端数据接收地址 | ||
const string server_url = "https://sdkdebugtest.datasink.sensorsdata.cn/sa?project=default&token=cfb8b60e42e0ae9b"; | ||
|
||
// 随机生成 UUID 作为 distinct_id | ||
// 注意:这里只是作为 demo 演示,随机生成一个 ID,如果正式使用,可以生成使用其他格式的设备 ID,并且自己保存下来,下次构造 SDK 时使用之前相同的 ID 以标识同一设备。 | ||
const string distinct_id = GenerateId(); | ||
std::cout << "distinct_id: " << distinct_id << std::endl; | ||
// 神策 ID 分为 “设备 ID” 和 “登录 ID” 两种,随机生成的是 “设备 ID” | ||
const bool is_login_id = false; | ||
|
||
// 本地最多暂存(未调用 Flush 发送)的数据条数,超过该数值时,将从队首淘汰旧的数据 | ||
const int max_staging_record_size = 200; | ||
|
||
// 初始化 SDK | ||
sensors_analytics::Sdk::Init(staging_file_path, server_url, distinct_id, is_login_id, max_staging_record_size); | ||
sensors_analytics::Sdk::EnableLog(true); | ||
// 设置公共属性,这些属性将自动设置在每个行为事件的属性里 | ||
sensors_analytics::PropertiesNode super_properties; | ||
super_properties.SetString("app_name", "myapp"); | ||
super_properties.SetString("platform", "PC"); | ||
sensors_analytics::Sdk::RegisterSuperProperties(super_properties); | ||
|
||
// 如果是 App 新安装第一次启动,若需要渠道追踪模糊匹配(请见文档),可以调用 | ||
sensors_analytics::PropertiesNode track_installation_properties; | ||
sensors_analytics::Sdk::TrackInstallation("AppInstall", track_installation_properties); | ||
|
||
// 记录一个行为事件 | ||
sensors_analytics::PropertiesNode event_properties; | ||
event_properties.SetString("computer_name", "ABCXYZ"); | ||
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 = "测试字符串"; | ||
event_properties.SetString("test_stl_string", test_string); | ||
event_properties.SetDateTime("test_time", time(NULL), 0); | ||
std::vector<std::string> test_list; | ||
test_list.push_back("item1"); | ||
test_list.push_back("item2"); | ||
event_properties.SetList("test_list", test_list); | ||
sensors_analytics::Sdk::Track("OpenApp", event_properties); | ||
// 当可以获取到用户的 “登录 ID” 时,使用登录接口设置 “登录 ID” | ||
// 此操作会在服务端进行 ID 关联,之后使用新的 ID 作为 distinct_id | ||
sensors_analytics::Sdk::Login("123456"); | ||
|
||
// 设置一个用户属性 | ||
sensors_analytics::Sdk::ProfileSetNumber("Age", 26); | ||
|
||
// 为数组类型的用户属性追加值 | ||
sensors_analytics::Sdk::ProfileAppend("hobby", "movie"); | ||
|
||
// 上面所有埋点都没有真正发送到服务端,当有网络的时候,请调用 Flush 手工触发发送 | ||
// 注意:仅当调用 Flush 函数才会触发网络发送 | ||
// 发送是阻塞的,可以考虑使用独立线程调用发送函数 | ||
// 如果因为网络问题发送失败,函数返回值为 false | ||
bool flush_result = sensors_analytics::Sdk::Flush(); | ||
std::cout << "send result: " << (flush_result ? "true" : "false") << std::endl; | ||
|
||
// 进程结束前没有 Flush 的数据将保存到 staging_file | ||
sensors_analytics::Sdk::Track("BuyTicket"); | ||
sensors_analytics::Sdk::Shutdown(); | ||
} | ||
|
||
#if defined(_WIN32) | ||
#define snprintf sprintf_s | ||
#endif | ||
|
||
// 随机生成一个 ID | ||
std::string GenerateId() { | ||
char str_uuid[80]; | ||
srand(time(NULL)); | ||
snprintf(str_uuid, sizeof(str_uuid), | ||
"%x%x-%x-%x-%x-%x%x%x", | ||
rand(), | ||
rand(), | ||
rand(), | ||
((rand() & 0x0fff) | 0x4000), | ||
rand() % 0x3fff + 0x8000, | ||
rand(), | ||
rand(), | ||
rand()); | ||
return str_uuid; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{7DDEE0AC-2D93-49DC-A46A-A02729D57E8E}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>demo</RootNamespace> | ||
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Filter Include="源文件"> | ||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="头文件"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | ||
</Filter> | ||
<Filter Include="资源文件"> | ||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
</Filter> | ||
</ItemGroup> | ||
</Project> |