目录
此内容是否有帮助?

# C

TIP

Before you begin, please read Preparation before Data Ingestion.

Latest version: 1.3.6

Update time: 2022-06-22

Resource download: Source Code (opens new window)

# SDK Integration (opens new window)

1.1 Download the source code, and import the following files into your project.

./c-sdk_version/include/thinkingdata.h
./c-sdk_version/lib/libthinkingdata.a

1.2 Logbus Integration

We recommend using SDK+LogBus to track and report data on server. You can refer to the following documents to complete the installation of Logbus: LogBus User Guide

# Initialization

The following is the sample code for SDK initialization:

TAConfig *config = ta_init_config();
TA_ASSERT(TA_OK == ta_add_string("file_path", "LOG_DIRECTORY", strlen("LOG_DIRECTORY"), config));

struct TAConsumer *consumer = NULL;
if (TA_OK != ta_init_logging_consumer(&consumer, config)) {
     fprintf(stderr, "Failed to initialize the consumer.");
     return 1;
 }
ta_free_properties(config);
ThinkingdataAnalytics *ta = NULL;
if (TA_OK != ta_init(consumer, &ta)) {
     fprintf(stderr, "Failed to initialize the SDK.");
     return 1;
}

LOG_DIRECTORY is the local folder path.

# Common Features

In order to ensure that the distinct ID and account ID can be bound smoothly, if your game uses the distinct ID and account ID, we strongly recommend that you upload these two IDs at the same time, otherwise the account will not match, causing users to double count. For specific ID binding rules, please refer to the chapter on User identification rules (opens new window).

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

TAProperties *properties = ta_init_properties();

TA_ASSERT(TA_OK == ta_add_string("#ip", "192.168.1.1", strlen("192.168.1.1"), properties));

TA_ASSERT(TA_OK == ta_add_string("channel", "ta", strlen("ta"), properties));//string
TA_ASSERT(TA_OK == ta_add_int("age", 1, properties)); //number
TA_ASSERT(TA_OK == ta_add_bool("is_success", true, properties));//bool
TA_ASSERT(TA_OK == ta_add_date("birthday", time(NULL), 0, properties));//time
// array
TA_ASSERT(TA_OK == ta_append_array("arr", "value", strlen("value"), properties));
TA_ASSERT(TA_OK == ta_append_array("arr", "value1", strlen("value1"), properties));
// object
TAProperties *object = ta_init_custom_properties("object");
TA_ASSERT(TA_OK == ta_add_string("key", "value", strlen("value"), object));
TA_ASSERT(TA_OK == ta_add_property(object, properties));
// array object
TAProperties *object1 = ta_init_custom_properties("object1");
TA_ASSERT(TA_OK == ta_add_string("key", "value", strlen("value"), object1));
TA_ASSERT(TA_OK == ta_append_properties("object_arr", object1, properties));

TA_ASSERT(TA_OK == ta_track("account_id", "distinct_id", "payment", properties, ta));
ta_free_properties(properties);
  • Key is the name of the property and refers to the string type. It must start with a character, and contain numbers, characters (insensitive to case, and upper cases would be transformed into lower cases by TE) and underscores "_", with a maximum length of 50 characters.
  • Value, the value of the property, supports string, numbers, Boolean, time, object, array object, and array

The requirements for event properties and user properties are the same as that for super properties

# 3.2 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. If no user properties are set before, user properties will be newly created. The type of newly-created user properties must conform to that of the uploaded properties. User name setting is taken as the example here:

//UserName is TA
TAProperties *user_properties = ta_init_properties();
TA_ASSERT(TA_OK == ta_add_string("user_name", "TA", strlen("TA"), user_properties));
TA_ASSERT(TA_OK == ta_user_set(account_id, distinct_id, user_properties,ta));
ta_free_properties(user_properties);

//UserName is TE
TAProperties *user_properties2 = ta_init_properties();
TA_ASSERT(TA_OK == ta_add_string("user_name", "TE", strlen("TE"), user_properties2));
TA_ASSERT(TA_OK == ta_user_set(account_id, distinct_id, user_properties2,ta));
ta_free_properties(user_properties2);

# Best Practice

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

TAConfig *config = ta_init_config();
//LOG_DIRECTORY is the local folder address
TA_ASSERT(TA_OK == ta_add_string("file_path", "LOG_DIRECTORY", strlen("LOG_DIRECTORY"), config));

struct TAConsumer *consumer = NULL;
if (TA_OK != ta_init_logging_consumer(&consumer, config)) {
     fprintf(stderr, "Failed to initialize the consumer.");
     return 1;
 }
ta_free_properties(config);
ThinkingdataAnalytics *ta = NULL;
if (TA_OK != ta_init(consumer, &ta)) {
       fprintf(stderr, "Failed to initialize the SDK.");
       return 1;
 }
 
TAProperties *properties = ta_init_properties();
//Set the user's ip address, and TE will analyze the user's geographical location information according to the IP address
TA_ASSERT(TA_OK == ta_add_string("#ip", "192.168.1.1", strlen("192.168.1.1"), properties));
TA_ASSERT(TA_OK == ta_add_string("channel", "ta", strlen("ta"), properties));//string
TA_ASSERT(TA_OK == ta_add_int("age", 1, properties)); //number
TA_ASSERT(TA_OK == ta_add_bool("is_success", true, properties));//bool
TA_ASSERT(TA_OK == ta_add_date("birthday", time(NULL), 0, properties));//datetime
//array
TA_ASSERT(TA_OK == ta_append_array("arr", "value", strlen("value"), properties));
TA_ASSERT(TA_OK == ta_append_array("arr", "value1", strlen("value1"), properties));
//object
TAProperties *object = ta_init_custom_properties("object");
TA_ASSERT(TA_OK == ta_add_string("key", "value", strlen("value"), object));
TA_ASSERT(TA_OK == ta_add_property(object, properties));
//array object
TAProperties *object1 = ta_init_custom_properties("object1");
TA_ASSERT(TA_OK == ta_add_string("key", "value", strlen("value"), object1));
TA_ASSERT(TA_OK == ta_append_properties("object_arr", object1, properties));
TA_ASSERT(TA_OK == ta_track("account_id", "distinct_id", "payment", properties, ta));
ta_free_properties(properties);

//set user properties
TAProperties *user_properties = ta_init_properties();
TA_ASSERT(TA_OK == ta_add_string("user_name", "TA", strlen("TA"), user_properties));
TA_ASSERT(TA_OK == ta_user_set(account_id, distinct_id, user_properties,ta));
ta_free_properties(user_properties);

//Calling the flush API will immediately write the data to the file. 
//In the production environment, pay attention to avoid IO or network overhead caused by frequent calls to flush
ta_flush(ta);