Version Compatibility

  • The SDK complies with the ANSI C99 standard, and some functions depend on the POSIX library and do not rely on third-party libraries

1. C SDK Usage Instructions

Before using, please read the data model introduction.

2. Sensors Analytics SDK Integration

Developers can integrate the Sensors Analytics SDK in C programs to collect and analyze user data.

Download the source code of the Sensors Analytics SDK from GitHub, which can be compiled locally and added to the project in the form of a static library. The SDK source code includes:

./LICENSE ./Makefile // Makefile ./sensors_analytics.h // SDK 头文件 ./sensors_analytics.c // SDK 源文件 ./demo.c // Demo 程序,使用 LoggingConsumer 将日志文件输出到本地磁盘
CODE

After compiling the SDK, a static library will be generated:

./output/include/sensors_analytics.h ./output/lib/libsensorsanalytics.a
CODE

Developers record user behavior data in the program using the C SDK. The SDK outputs formatted behavior data to local disk files in the form of log files. Developers need to use the Sensors Analytics LogAgent tool to transfer log files to Sensors Analytics. The LogAgent tool ensures the timeliness and integrity of log transmission.

3. Initialize the Sensors Analytics SDK

Use in Program

// 初始化神策分析对象 // // @param consumer<in> 日志数据的 “消费” 方式 // @param sa<out> 初始化的神策分析实例 // // @return SA_OK 初始化成功,否则初始化失败 int sa_init(struct SAConsumer* consumer, struct SensorsAnalytics** sa);
CODE

Initializing the Sensors Analytics instance to implement various SDK functionalities. The first parameter of the consumer object is responsible for "consuming" user behavior logs, used in the program

// 初始化 Logging Consumer // // @param file_name<in> 日志文件名,例如: /data/logs/http.log // @param consumer<out> SALoggingConsumer 实例 // // @return SA_OK 初始化成功,否则初始化失败 int sa_init_logging_consumer(const char* file_name, SALoggingConsumer** consumer);
CODE

Initialize the SALoggingConsumer instance, which is responsible for outputting user behavior logs to local disk.

// 初始化 SALoggingConsumer 实例 SALoggingConsumer* consumer = NULL; if (SA_OK != sa_init_logging_consumer("./demo.out", &consumer)) { fprintf(stderr, "Failed to initialize the consumer."); return 1; } SensorsAnalytics *sa = NULL; if (SA_OK != sa_init(consumer, &sa)) { fprintf(stderr, "Failed to initialize the SDK."); return 1; } // ... 使用神策分析进行各种操作 // 将本地缓存的数据同步到文件或 Sensors Analtycis 中 sa_flush(sa); // 释放 Sensors Analtycis 实例 sa_free(sa);
CODE