PHP code example of fengxinyhyl / datatester-php-sdk
1. Go to this page and download the library: Download fengxinyhyl/datatester-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fengxinyhyl / datatester-php-sdk example snippets
composer
__construct(
$token,
LoggerInterface $logger = null,
ProductConfigManagerInterface $productConfigManager = null,
EventDispatcherInterface $eventDispatcher = null,
UserAbInfoHandler $userAbInfoHandler = null
)
getExperimentConfigs($experimentId, $decisionId, $attributes): ?array
getAllExperimentConfigs($decisionId, $attributes): ?array
getFeatureConfigs($featureId, $decisionId, $attributes): ?array
getAllFeatureConfigs($decisionId, $attributes): ?array
getExperimentConfigsWithImpression($experimentId, $decisionId, $attributes): ?array
getFeatureConfigsWithImpression($featureId, $decisionId, $attributes): ?array
$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);
}
}
enable anonymously tracking
$this->_abClient->setEventBuilderConfig(true, true);
composer
use DataTester\Client\AbClient;
// 初始化ABTest分流类,token获取方式详见接口描述-AbClient
$abClient = new AbClient("ede2cd734827cccf9c051005bd1b24c1");
// 第2个缺省值,日志接口,可根据业务需要传入自定义实现类,SDK提供默认实现
// 第3个缺省值,实验Meta信息管理接口,可根据业务需要传入自定义实现类,SDK提供默认实现
// 第4个缺省值,进组曝光事件上报接口,可根据业务需要传入自定义实现类,SDK提供默认实现
// 第5个缺省值,进组信息持久化接口,可根据业务需要传入自定义实现类,SDK提供默认实现(不持久化)
// $abClient = new AbClient("ede2cd734827cccf9c051005bd1b24c1", $logger, $configManager, $eventDispatcher,$userAbInfoHandler);
// trackId 事件上报用户标识,用于事件上报,请替换为客户的真实用户标识
$trackId = "uuid";
// decisionID: 本地分流用户标识,不用于事件上报,请替换为客户的真实用户标识
$decisionId = "decisionID";
// defaultValue: 当分流未命中时返回该值,根据业务需要使用,可传null
$defaultValue = "default_value";
// attributes: 用户属性,仅用于分流,不随埋点上报,可参考https://www.volcengine.com/docs/6287/65826
$attributes = [];
// 推荐接口 abtest_param为需要通过分流下发的参数名称
$value = $abClient->activate(
"abtest_param",
$decisionId,
$trackId,
$attributes,
$defaultValue);
if ($value === "param_raw") {
// 对照组
} elseif ($value === "param_test") {
// 实验组
} else {
// 默认处理
}
__construct(
$token,
LoggerInterface $logger = null,
ProductConfigManagerInterface $productConfigManager = null,
EventDispatcherInterface $eventDispatcher = null,
UserAbInfoHandler $userAbInfoHandler = null
)
getExperimentConfigs($experimentId, $decisionId, $attributes): ?array
getAllExperimentConfigs($decisionId, $attributes): ?array
getFeatureConfigs($featureId, $decisionId, $attributes): ?array
getAllFeatureConfigs($decisionId, $attributes): ?array
$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);
}
}
enable anonymously tracking
$this->_abClient->setEventBuilderConfig(true, true);