The following is a code example of using the PHP SDK.
use DataTester\Client\AbClient;
// Initialize the traffic-allocating object
// Find your app key by clicking "details" on the project list page on the BytePlus console
$abClient = new AbClient("${app_key}");
// The second value is the log interface. You can change it if you want
// The third value is for managing meta information. You can change it if you want
// The fourth value is for event tracking. You can change it if you want
// userAbInfoHandler is to ensure that incoming users do not leave the version. If you want to constantly store the information of incoming users, you can implement UserAbInfoHandler by yourself
// $abClient = new AbClient("ede2cd73482xxxxxx05bd1b24c1", $logger, $configManager, $eventDispatcher,$userAbInfoHandler);
// The user identifier for event tracking. You need to replace it with the actual user ID
$trackId = "uuid";
// The local user identifier. Not used for event tracking. You need to replace it with the actual user ID
$decisionId = "decisionID";
// When a user/device is not in this version, then the value of this parameter is returned. You can set its value as "null"
$defaultValue = true;
// User properties. Only used for allocating the traffic
$attributes = [];
// It is recommended that you use this interface
// The first parameter is the experiment key
$value = $abClient->activate(
"${experiment_key}",
$decisionId,
$trackId,
$attributes,
$defaultValue);
if ($value) {
// The control version
} else {
// The variant version
}
API reference
AbClient
A class for traffic allocation during initialization.
Your project’s App Key. You can obtain it in the Prerequisites section.
2b47*****8d78fd718548153901addde
LoggerInterface
The logger interface which has a default value but you can customize it.
None
ProductConfigManagerInterface
The experiment’s meta-information management interface, which obtains the experiment’s information. You can customize it.
None
EventDispatcherInterface
The interface for event tracking. You can customize it.
None
UserAbInfoHandler
Ensure that incoming users’ information about version ID remains unchanged. If you want to constantly store the information of incoming users, you must implement it by yourself.
None
activate
Obtain a specific experiment version’s configuration after traffic allocation and automatically track the exposed events.
Make sure you fill in the trackId field if you want to track events.
The user identifier for event tracking. You need to replace it with the actual user ID
defaultValue
When a user/device is not in this version, then the value of this parameter is returned. You can set its value as “None”
attributes
User properties
Returned value
Parameters of the version that a user enters or the default value when a user/device is not in any version. Can be string, number, boolean, and json type. The following is an example:
// When the parameter type is string, the returned value is string "a";
// When the parameter type is number, the returned value is float 123.456;
// When the parameter type is boolean, the returned value is boolean true;
// When the parameter type is json, the returned value array ["key" => "a"].
activateWithoutImpression
Obtain a specific experiment version’s configuration after traffic allocation but not track the exposed events.
The detailed information about a feature’s variant that a user joins. ‘None’ when a user/device is not in this feature or the feature is disabled. The following is an example:
Obtain the version’s name of the experiment that a user enters.
Interfaces with WithImpression automatically track events. Meanwhile, make sure you fill in the trackId field in the activate interface if you want to track events.
Name of the version that a user enters or “None” when a user/device is not in any version.
getExperimentConfigsWithImpression
Obtain detailed information about the version that a user enters.
Interfaces with WithImpression automatically track events. Meanwhile, make sure you fill in the trackId field in the activate interface if you want to track events.
Obtain detailed information about a feature that a user joins.
Interfaces with WithImpression automatically track events. Meanwhile, make sure you fill in the trackId field in the activate interface if you want to track events.
The detailed information about a feature’s variant that a user joins. ‘None’ when a user/device is not in this feature or the feature is disabled. The following is an example:
In order to better use the sdk, some suggestions are provided.
LoggerInterface
The log interface provides a default implementation; if there is a business need, you can customize the implementation class processing and use it when instantiating AbClient.
ProductConfigManagerInterface
Meta management interface: request the meta service to pull the experiment and feature information under the application. Default implementation pull it in real time every time AbClient is instantiated; if there is a business need, you can customize the implementation class processing, and use it in when instantiating AbClient.
Suggestion
It is recommended to use redis to cache meta information to avoid pulling every time AbClient is initialized.
The following is an example:
$client = new AbClient("token", null, new RedisConfigManager("token"));
class RedisConfigManager implements ProductConfigManagerInterface
{
/**
* @var ProductConfig $_productConfig
*/
private $_productConfig;
/**
* @var LoggerInterface Logger instance.
*/
private $_logger;
/**
* @var string $_token
*/
private $_token;
public function __construct(
$token
)
{
$this->_logger = new DefaultLogger();
$this->_token = $token;
}
public function getConfig(): ?ProductConfig
{
if ($this->_productConfig != null) {
return $this->_productConfig;
}
$valueFromRedis = $this->getValueFromRedis("tester_meta_info");
// pull meta when redis cache expired
if ($valueFromRedis == null) {
$productConfigManger = new HTTPProductConfigManager($this->_token);
try {
$metaInfo = $productConfigManger->getMeta();
$this->setValue2Redis("tester_meta_info", JsonParse::transferArray2JsonStr($metaInfo), 60);
$this->_productConfig = new ProductConfig($metaInfo, $this->_logger);
return $this->_productConfig;
} catch (\Exception $e) {
return null;
}
}
$metaInfo = JsonParse::transferJsonStr2Array($valueFromRedis);
$this->_productConfig = new ProductConfig($metaInfo, $this->_logger);
return $this->_productConfig;
}
private function getValueFromRedis(string $key): ?string
{
// need to implement it yourself
// return redis.get($key);
return null;
}
private function setValue2Redis(string $key, string $value, int $expire)
{
// need to implement it yourself
// redis.set($key, $value, $expire);
}
}
EventDispatcherInterface
Event tracking interface: which track exposure events, provides a default implementation, and track in real time when ‘activate’ and ‘WithImpression’ interfaces are called; if there are business needs, you can customize the implementation class processing, and use it in when instantiating AbClient.
Suggestion
It is recommended to use mq(kafka/rocketmq) to track events to avoid tracking events by http every time interfaces are called.
Send msg to mq when events happen
Use other services to consume kafka and track events
The following is an example:
$client = new AbClient("token", null, null, new KafkaEventDispatcher());
class KafkaEventDispatcher implements EventDispatcherInterface
{
public function dispatchEvent($events)
{
// need to implement it yourself
kafka.send(JsonParse::transferArray2JsonStr($events));
}
}
UserAbInfoHandler
Maintain user historical decision information; If you need to use the function of ‘freezing experiment‘ or ‘Traffic changes will not affect exposed users‘, you can customize the implementation class processing, and use it in when instantiating AbClient.
Suggestion
It is recommended to use redis to cache decision information.
The following is an example:
$client = new AbClient("token", null, null, null, new RedisHandler());
class RedisHandler implements UserAbInfoHandler
{
public function query(string $decisionId): ?string
{
// need to implement it yourself
return redis.get($decisionId);
}
public function createOrUpdate(string $decisionId, string $experiment2variantStr): bool
{
// need to implement it yourself
return redis.set($decisionId, $experiment2variantStr);
}
public function needPersistData(): bool
{
// return true if customize this interface
return true;
}
}
Anonymously tracking
If there is no user_unique_id as trackId, you can use device_id, web_id, bddid(only onpremise) for anonymous tracking.
$client = new AbClient("token", null, null, new KafkaEventDispatcher());
class KafkaEventDispatcher implements EventDispatcherInterface
{
public function dispatchEvent($events)
{
// need to implement it yourself
kafka.send(JsonParse::transferArray2JsonStr($events));
}
}
$client = new AbClient("token", null, null, null, new RedisHandler());
class RedisHandler implements UserAbInfoHandler
{
public function query(string $decisionId): ?string
{
// need to implement it yourself
return redis.get($decisionId);
}
public function createOrUpdate(string $decisionId, string $experiment2variantStr): bool
{
// need to implement it yourself
return redis.set($decisionId, $experiment2variantStr);
}
public function needPersistData(): bool
{
// return true if customize this interface
return true;
}
}
DataTester PHP SDK
Limitation
Prerequisite
Obtain your project’s App Key:
Adding dependency
Changing the domain name
The default domain name must be changed as follows.Source file is [Urls.php].
Using the SDK
The following is a code example of using the PHP SDK.
API reference
AbClient
A class for traffic allocation during initialization.
Parameter description
activate
Obtain a specific experiment version’s configuration after traffic allocation and automatically track the exposed events.
Parameter description
Returned value
Parameters of the version that a user enters or the default value when a user/device is not in any version. Can be string, number, boolean, and json type. The following is an example:
activateWithoutImpression
Obtain a specific experiment version’s configuration after traffic allocation but not track the exposed events.
Parameter description
Returned value
Parameters of the version or empty array. The following is an example:
getExperimentVariantName
Obtain the version’s name of an experiment that a user enters:
Parameter description
Returned value
Name of the version that a user enters or “None” when a user/device is not in any version.
getExperimentConfigs
Obtain detailed information about the version that a user enters:
Parameter description
Returned value
The detailed information about the version that a user enters or ‘None’ when a user/device is not in any version. The following is an example:
getAllExperimentConfigs
Obtain detailed information about all the versions in all experiments.
Parameter description
Returned value
The detailed information about versions that a user enters or empty array when a user/device is not in any version. The following is an example:
getFeatureConfigs
Obtain detailed information about a feature that a user joins.
Parameter description
Returned value
The detailed information about a feature’s variant that a user joins. ‘None’ when a user/device is not in this feature or the feature is disabled. The following is an example:
getAllFeatureConfigs
Obtain detailed information about all features that a user joins.
Parameter description
Returned value
The detailed information about all feature variants that a user joins. The following is an example:
getExperimentVariantNameWithImpression
Obtain the version’s name of the experiment that a user enters.
Parameter description
Returned value
Name of the version that a user enters or “None” when a user/device is not in any version.
getExperimentConfigsWithImpression
Obtain detailed information about the version that a user enters.
Parameter description
Returned value
The detailed information about the version that a user enters or ‘None’ when a user/device is not in this version. The following is an example:
getFeatureConfigsWithImpression
Obtain detailed information about a feature that a user joins.
Parameter description
Returned value
The detailed information about a feature’s variant that a user joins. ‘None’ when a user/device is not in this feature or the feature is disabled. The following is an example:
Others
In order to better use the sdk, some suggestions are provided.
LoggerInterface
ProductConfigManagerInterface
Suggestion
The following is an example:
EventDispatcherInterface
Suggestion
The following is an example:
UserAbInfoHandler
Suggestion
The following is an example:
Anonymously tracking
DataTester PHP SDK
版本需求
准备工作
获取应用的App Key(即SDK使用的token):
依赖导入
域名修改
代码示例
接口描述
AbClient
初始化ABTest分流类
参数
activate
获取特定key的分流结果,并上报曝光事件
参数
返回值
该函数返回命中版本的参数值,未命中时返回默认值
activateWithoutImpression
获取特定key的分流结果,且不上报曝光事件
参数
返回值
该函数返回命中版本的参数值,未命中时返回空数组
getExperimentVariantName
获取用户命中的特定实验的变体名称
参数
返回值
该函数返回用户命中的特定实验的变体名称
getExperimentConfigs
获取用户命中的特定实验的变体详情
参数
返回值
该函数返回命中变体的array对象,表明用户命中某个实验的变体详情,通常仅能命中一个变体
getAllExperimentConfigs
获取用户命中的所有实验的变体详情
参数
返回值
该函数返回命中变体的array对象,表明用户命中所有实验的变体详情,通常命中多个变体
getFeatureConfigs
获取用户命中的特定feature的变体详情
参数
返回值
该函数返回命中变体的array对象,表明用户命中某个feature的变体详情,通常仅能命中一个变体
getAllFeatureConfigs
获取用户命中的所有feature的变体详情
参数
返回值
该函数返回命中变体的array对象,表明用户命中所有feature的变体详情,通常命中多个变体
getExperimentVariantNameWithImpression
同接口“getExperimentVariantName”
getExperimentConfigsWithImpression
同接口“getExperimentConfigs”
getFeatureConfigsWithImpression
同接口“getFeatureConfigs”
其他
LoggerInterface
日志打印接口,提供默认实现;如有业务需要,可自定义实现类处理,实例化AbClient时传入
ProductConfigManagerInterface
配置管理接口,请求meta服务拉取应用下的实验信息,提供默认实现,每次实例化AbClient时实时拉取;如有业务需要,可自定义实现类处理,实例化AbClient时传入
使用Redis缓存示例(仅供参考)
EventDispatcherInterface
事件上报接口,上报进组曝光事件,提供默认实现,调用activate与WithImpression接口时实时上报;如有业务需要,可自定义实现类处理,实例化AbClient时传入
基于kafka等消息队列,在实例化AbClient对象时传入EventDispatcherInterface的实现类;事件直接写入kafka,通过其他服务去消费kafka并上报(上报可参考 DefaultEventDispatcher的实现),写入和消费kafka的逻辑需自行实现
UserAbInfoHandler
用户信息处理接口,冻结实验、进组不出组场景下使用
使用Redis缓存示例(仅供参考)
匿名上报