Skip to main content

PHP

Last updated 10/03/2026
tip

Before you integrate the SDK, read Pre-integration preparation.

You can get the PHP SDK source code on GitHub.

Latest version: v3.1.1

Update time: 2024-07-24

Downloads: Source code

Note

This document applies to v3.0.0 and later. For earlier versions, see PHP SDK Integration Guide (V2)

1. Integrate the SDK​

  1. Integrate with Composer
{
"require": {
"thinkinggame/ta-php-sdk": "v3.1.1"
}
}
  1. You can also get the SDK source code from GitHub and integrate it into your project. Just put TaPhpSdk.php in your project directory. This SDK is compatible with PHP >5.5, and some features depend on the curl extension.
  2. Install Logbus

We recommend using SDK + LogBus to collect and report server-side data. To install Logbus, see the following document: LogBus user guide

2. Initialization​

The following sample code initializes the SDK:

require_once "vendor/autoload.php";

use Exception;
use ThinkingData\TDLog;
use ThinkingData\TDAnalytics;
use ThinkingData\TDFileConsumer;
use ThinkingData\TDDebugConsumer;
use ThinkingData\TDBatchConsumer;
use ThinkingData\ThinkingDataException;

TDLog::$enable = true;

$consumer = new TDFileConsumer("LOG_DIRECTORY", 200, true, "LOG_FILE_PREFIX");
$teSDK = new TDAnalytics($consumer, true);

LOG_DIRECTORY is the path of the local folder that data is written to. Set the folder that LogBus monitors to this path, and LogBus monitors and uploads the data.

LOG_FILE_PREFIX is the prefix of the log file name.

3. Common features​

To make sure that the distinct ID and account ID can be bound correctly, if your game uses both the distinct ID and the account ID, we strongly recommend that you upload both IDs. Otherwise, accounts may fail to match and users may be counted more than once. For details on ID binding rules, see User identification rules.

3.1 Send events​

You can call track to upload events. We recommend setting event properties and the conditions for sending events based on the document you prepared earlier. The following sample code sends an event:

$account_id = 2121;
$distinct_id = 'SJ232d233243';
$properties = array();
$properties['age'] = 20;
$properties['Product_Name'] = 'c';
$properties['update_time'] = date('Y-m-d H:i:s', time());
$json = array();
$json['a'] = "a";
$json['b'] = "b";
$jsonArray = array();
$jsonArray[0] = $json;
$jsonArray[1] = $json;
$properties['json'] = $json;
$properties['jsonArray'] = $jsonArray;

try {
$teSDK->track($distinct_id, $account_id, "viewPage", $properties);
} catch (Exception $e) {
echo $e;
}
  • The event name is of the string type. It must start with a letter, can contain digits, letters, and underscores "_", and can be up to 50 characters long.
  • Key is the name of the property and is of string type. It must start with a letter, can contain digits, letters, and underscores "_", can be up to 50 characters long, and is case-insensitive. AE converts it to lowercase
  • Value is the value of the property. Supported types are string, number, Boolean, time, object, object group, and array

User properties have the same requirements as event properties

3.2 Set user properties​

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();
$properties['once_key'] = 'twice';
$properties['age'] = 10;
$properties['money'] = 300;
$properties['array1'] = ['str1', 'str2'];
try {
$teSDK->user_set('distinct_id', 'account_id', $properties);
} catch (Exception $e) {
//handle except
echo $e;
}

3.3 Send data​

When you use TDFileConsumer, collected events are first converted to JSON strings and added to a cache array. Data is written to disk only when the number of elements in the array exceeds the configured capacity. The default capacity is 100 records.

In some business scenarios, if you want data to be reported to the AE server immediately, you can call flush(). Note that calling flush() frequently degrades service performance.

$teSDK->flush();

3.4 Shut down the SDK​

try{
$teSDK->close();
} catch (Exception $e){
//Exception handling
echo $e;
}

Shuts down and exits the SDK. Call this API before you shut down the server to avoid losing data in the cache

4. Best practices​

The following sample code includes all of the operations above. We recommend using them in the following order:

require_once "vendor/autoload.php";

use Exception;
use ThinkingData\TDLog;
use ThinkingData\TDAnalytics;
use ThinkingData\TDFileConsumer;
use ThinkingData\TDDebugConsumer;
use ThinkingData\TDBatchConsumer;
use ThinkingData\ThinkingDataException;

TDLog::$enable = true;

$consumer = new TDFileConsumer("LOG_DIRECTORY", 200, true, "te");
$teSDK = new TDAnalytics($consumer, true);

$account_id = 2121;
$distinct_id = 'SJ232d233243';
$properties = array();
$properties['age'] = 20;
$properties['Product_Name'] = 'c';
$properties['update_time'] = date('Y-m-d H:i:s', time());
$json = array();
$json['a'] = "a";
$json['b'] = "b";
$jsonArray = array();
$jsonArray[0] = $json;
$jsonArray[1] = $json;
$properties['json'] = $json;
$properties['jsonArray'] = $jsonArray;

try {
$teSDK->track($distinct_id, $account_id, "viewPage", $properties);
} catch (Exception $e) {
echo $e;
}

$properties = array();
$properties['once_key'] = 'twice';
$properties['age'] = 10;
$properties['money'] = 300;
$properties['array1'] = ['str1', 'str2'];
try {
$teSDK->user_set($distinct_id, $account_id, $properties);
} catch (Exception $e) {
//handle except
echo $e;
}
Was this page helpful?