Advanced guide
1. Send events
After the SDK is initialized, you can start tracking data to collect user behavior. In most cases, regular events meet business needs. You can also use first events, updatable events, and other event types based on your actual business scenarios.
1.1 Regular events
You can call track to upload events. We recommend setting event properties and the conditions for sending events according to the document you prepared earlier. The following example tracks a user purchasing a product:
//Set event properties
$properties = array();
$properties["product_name"] = "book";
try {
$te->track("distinctId", "accountId", "productBuy", $properties);
} catch (Exception $e) {
echo $e;
}
1.2 First events
A first event is an event that is recorded only once for a given device or an ID of another dimension. For example, in some scenarios you may want to record the activation event on a device; you can report this data as a first event.
// Example: report a first event, assuming the event name is device_activation
$properties = array();
$properties["price"] = 100;
$properties["status"] = 3;
$firstCheckId = "first_flag_id";
try {
$te->track_first("distinctId", "accountId", "device_activation", $firstCheckId, $properties);
} catch (Exception $e) {
echo $e;
}
Note: Because the first-event check is performed on the server, first events are stored with a 1-hour delay by default.
1.3 Updatable events
You can use updatable events to modify event data in specific scenarios. An updatable event requires an ID that identifies the event, which you pass in when you create the updatable event object. The AE backend determines which data to update based on the event name and event ID.
// Example: report an updatable event, assuming the event name is eventName
// After reporting, the event property status is 3 and price is 100
$properties = array();
$properties["price"] = 100;
$properties["status"] = 3;
$eventId = "eventId";
try {
$te->track_update("distinctId", "accountId", "eventName", $eventId, $properties);
} catch (Exception $e) {
echo $e;
}
// After reporting, the event property status of the event whose event ID is eventId and event name is eventName is updated to 5, and price is unchanged
$properties1 = array();
$properties1["status"] = 5;
try {
$te->track_update("distinctId", "accountId", "eventName", $eventId, $properties1);
} catch (Exception $e) {
echo $e;
}
1.4 Overwritable events
Overwritable events are similar to updatable events, except that an overwritable event completely overwrites historical data with the latest data. In effect, the previous record is deleted and the latest data is ingested. The AE backend determines which data to update based on the event name and event ID.
// Example: report an overwritable event, assuming the event name is eventName
// After reporting, the event property status is 3 and price is 100
$properties = array();
$properties["price"] = 100;
$properties["status"] = 3;
$eventId = "eventId";
try {
$te->track_overwrite("distinctId", "accountId", "eventName", $eventId, $properties);
} catch (Exception $e) {
echo $e;
}
// After reporting, the event property status of the event whose event ID is eventId and event name is eventName is updated to 5, and the price property is deleted
$properties1 = array();
$properties1["status"] = 5;
try {
$te->track_overwrite("distinctId", "accountId", "eventName", $eventId, $properties1);
} catch (Exception $e) {
echo $e;
}
2. User properties
The user property APIs supported by the AE platform are: user_set, user_setOnce, user_add,
user_unset、user_del、user_append、user_uniq_append。
2.1 user_set
For general user properties, you can call user_set to set them. Properties uploaded through this API overwrite the original values. If the user property doesn't exist yet, it is created with the same type as the value passed in. The following example sets the username:
$properties = array();
//Upload a user property: create the property "user_name" with the value "ABC"
$properties["user_name"] = "ABC";
try{
$te->user_set('distinct_id', 'account_id', $properties);
}catch (Exception $e){
//Exception handling
echo $e;
}
//Upload the user property again. The value of "user_name" is overwritten with "XYZ"
$properties["user_name"] = "XYZ";
try{
$te->user_set('distinct_id', 'account_id', $properties);
}catch (Exception $e){
//Exception handling
echo $e;
}
2.2 user_setOnce
If a user property only needs to be set once, you can call user_setOnce to set it. If the property already has a value, this call is ignored. The following example again sets the username:
$properties = array();
//Likewise, upload a user property: create the property "user_name" with the value "ABC"
$properties["user_name"] = "ABC";
try{
$te->user_setOnce('distinct_id', 'account_id', $properties);
} catch (Exception $e){
//Exception handling
echo $e;
}
//Upload user properties again. "user_name" already has a value, so it isn't modified and is still "ABC"; the value of "user_age" is 18
$properties["user_name"] = "XYZ";
$properties["user_age"] = 18;
try{
$te->user_setOnce('distinct_id', 'account_id', $properties);
} catch (Exception $e){
//Exception handling
echo $e;
}
2.3 user_add
To upload a numeric property, you can call user_add to accumulate its value. If the property hasn't been set yet, it is assigned 0 before the calculation. You can pass a negative value, which is equivalent to subtraction. The following example accumulates the total payment amount:
try{
$properties = array();
$properties['level'] = 2;
$te->user_add($distinct_id, $account_id, $properties);
}catch (Exception $e){
//handle except
echo $e;
}
The property key is a string, and the Value can only be a number.
2.4 user_append
You can call user_append to append values to an array-type user property.
//user_append appends to one or more collections of a user
try{
$properties = array();
$properties['arr'] = ['str3','str4'];//Append multiple values to a collection-type property, in key-array form; all elements in the array are strings
$te->user_append('distinct_id', 'account_id', $properties);
}catch (Exception $e){
//handle except
echo $e;
}
2.5 user_uniq_append
You can call user_uniq_append to append values to a user property of the array type. Calling user_uniq_append deduplicates the appended user property values, whereas user_append doesn't, so the user property may contain duplicates.
//user_uniq_append appends to one or more collections of a user (duplicate elements are removed)
try{
$properties = array();
$properties['arr'] = ['str3','str4'];//Append multiple values to a collection-type property, in key-array form; all elements in the array are strings
$te->user_uniq_append('distinct_id', 'account_id', $properties);
}catch (Exception $e){
//handle except
echo $e;
}
2.6 user_unset
To clear the value of a user property, you can call user_unset to clear the specified property. If the property hasn't been created in the cluster yet, user_unset does not create it
$properties1 = array(
'age', "update_time"
);
try {
$te->user_unset(null, 'account_id', $properties1);
} catch (Exception $e) {
//handle except
echo $e;
}
user_unset: The value passed in is the Key of the property to clear.
2.7 user_del
To delete a user, you can call user_del. After that, you can no longer query this user's user properties, but the events generated by the user can still be queried. This operation may have irreversible consequences, so use it with caution
try{
$te->user_del('distinct_id', 'account_id');
} catch (Exception $e){
//Exception handling
echo $e;
}
3. Other features
3.1 TDBatchConsumer
When the data volume is too large or the network is abnormal, data may be lost. We don't recommend using it in the production environment
Transfers data to the AE server in batches in real time, without a transfer tool.
require "TaPhpSdk.php";
$te = new TDAnalytics(new TDBatchConsumer("SERVER_URL","APP_ID"));
Parameters:
-
APP_ID: The APP ID of your project, which you can find on the project management page in the AE backend -
SERVER_URL: The URL that data is uploaded to- If you use the cloud service, enter: https://global-receiver-ta.thinkingdata.cn
- If you use an on-premises deployment, bind a domain name to the data collection URL and configure an HTTPS certificate: https://your-domain-for-data-collection

