PHP 版本要求 >= 7.4
在composer.json中添加依赖:(^1.0 表示使用最新的版本 )
{ "require": { "datarangers/datarangers": "^1.0" } }
执行命令:
composer require datarangers/datarangers
为了您更好地理解和使用我们的sdk,我们已经将SDK进行了开源,具体地址为https://github.com/volcengine/datarangers-sdk-php。您可以直接将SDK源码下载并使用。
需要配合loagent一起使用
CollectorConfig::init_datarangers_collector([ "save" => true, "logger_file_prefix" => "sdk/log/", "logger_file_name" => "datarangers.log", "log_max_bytes" => 1024 * 10 ]); $rc = new AppEventCollector();
// 初始化 CollectorConfig::init_datarangers_collector([ "domain" => "https://xxxx", "save" => false, "headers" => [ "Content-Type" => "application/json" ], "http_timeout"=> 10000 ]); $rc = new AppEventCollector();
init_datarangers_collector方法的入参为一个array,具体的参数为
模式 | 参数名 | 是否为必须 | 说明 |
---|---|---|---|
Http模式 | save | 是 | Http模式下为false |
domain | 是 | DataRangers的域名或ip,支持http和https | |
headers | 否 | Http的header头。 | |
http_timeout | 否 | Http请求超时时间,单位为毫秒。默认为1000 | |
LogAgent模式 | save | 是 | LogAgent模式下为true,只支持私有化 |
logger_file_prefix | 否 | 日志存储路径 | |
logger_file_name | 否 | 日志存储文件名 | |
log_max_bytes | 是 | 日志的最大大小,超过该值日志会进行切分 |
SDK提供了Collector接口,具体的接口如下。
/** * 功能描述: 异步发送事件 * @param $userUniqueId string 用户uuid * @param $appId int 应用id * @param $custom array 用户自定义公共参数,例如上报自定义的公共属性 * @param $eventName object 事件名,可以为array * @param $eventParams object 事件参数,可以为array,与$eventName 长度相同 * 例如 $eventName $eventParams 分别为 * $eventName = "launch" $eventParams = ["param1"=>"param1","param2"=>"param2"] * $eventName = ["launch1","launch2"] $eventParams = [["param1"=>"param1","param2"=>"param2"],["param3"=>"param3","param4"=>"param4"]] */ public function sendEvent($userUniqueId, $appId, $custom, $eventName, $eventParams);
/** * 功能描述: 设置用户的属性 * @param $userUniqueId string 用户uuid * @param $appId int 应用id * @param $eventParams array 需要设置的用户属性 example ["php_version"=>"1.3.0"] * @return mixed */ public function profileSet($userUniqueId, $appId, $eventParams);
/** * 功能描述: 删除用户的属性 * @param $userUniqueId string * @param $appId int * @param $eventParams array 需要删除的用户属性 example ["php_version"=>""]. unset php_version * @return mixed */ public function profileUnset($userUniqueId, $appId, $eventParams);
/** * 功能描述: 设置用户属性,存在则不设置,不存在则创建,适合首次相关的用户属性,比如首次访问时间等。 * @param $userUniqueId string * @param $appId int * @param $eventParams array set once profile example ["php_version"=>"1.1"]. set php_version only once * @return mixed */ public function profileSetOnce($userUniqueId, $appId, $eventParams);
/** * 功能描述: 设置数值类型的用户属性,可进行累加 * @param $userUniqueId string * @param $appId int * @param $eventParams array increment profile example ["php_example"=>10]. php_example=php_example+10 * @return mixed */ public function profileIncrement($userUniqueId, $appId, $eventParams);
/** * 功能描述: 设置用户属性,可持续添加用户属性 * @param $userUniqueId string * @param $appId int * @param $eventParams array append profile example ["php_version"=>["1.1","1.2"]]. append php_version * @return mixed */ public function profileAppend($userUniqueId, $appId, $eventParams);
/** * 功能描述: 对业务对象进行设置 * @param $appId int app id * @param $itemName string 业务对象的名称 * @param $items array 业务对象的类,需要继承Items类,注意 example:["item_id"=>"0001","item_name"=>"book","item_price"=>5.0] * @return mixed */ public function itemSet($appId, $itemName, $items);
/** * 功能描述: 删除item的属性 * @param $appId int * @param $itemName string * @param $itemId string * @param $items 需要删除的item属性 example: ["param1","param2","param3"] * @return mixed */ public function itemUnset($appId, $itemName, $itemId, $items);
/** * set user profile * @param $header Header * @param $eventParams array set profile example ["php_version"=>"1.3.0"] * @return mixed */ public function profileSetWithHeader($header, $eventParams);
/** * @param $header Header * @param $eventParams array unset profile example ["php_version"=>""]. unset php_version * @return mixed */ public function profileUnsetWithHeader($header, $eventParams);
/** * @param $header Header * @param $eventParams array set once profile example ["php_version"=>"1.1"]. set php_version only once * @return mixed */ public function profileSetOnceWithHeader($header, $eventParams);
/** * @param $header Header * @param $eventParams array increment profile example ["php_example"=>10]. php_example=php_example+10 * @return mixed */ public function profileIncrementWithHeader($header, $eventParams);
/** * @param $header Header * @param $eventParams array append profile example ["php_version"=>["1.1","1.2"]]. append php_version * @return mixed */ public function profileAppendWithHeader($header, $eventParams);
// 初始化,以http模式为例 CollectorConfig::init_datarangers_collector([ "domain" => "https://xxxx", "save" => false, "headers" => [ "Host" => "xxxx", "Content-Type" => "application/json" ], "http_timeout"=> 10000 ]);
$rc = new AppEventCollector(); # 上报事件 $rc->sendEvent("test-uuidsdk1", 1001, null, ["php_event"], [["php_name" => "php", "php_version" => "5.6"]]);
# 上报单个事件 $rc->sendEvent("test-uuidsdk1", 1001, null, "php_single_event", ["php_name" => "php", "php_version" => "5.6"]);
# 上报用户属性 $rc->profileSet("test-uuidsdk1", 1001, ["profile_php_name" => "php7", "profile_php_version" => "7.4", "profile_int" => 1]);
# 上报 item 属性 $rc->itemIdSet(1001, "book", "book3", ["author" => "吴承恩", "name" => "西游记", "price" => 59.90, "category" => 1]); $rc->itemIdSet(1001, "book", "book4", ["author" => "Guanzhong Luo", "name" => "SanGuoYanYi", "price" => 69.90, "category" => 1]);
# 在事件中上报 item $rc->sendEvent("test-uuidsdk1", 1001, null, ["php_event_with_item"], [["php_name" => "php", "php_version" => "5.6"]], [ [["item_name" => "book", "item_id" => "book3"], ["item_name" => "book", "item_id" => "book4"]] ]);
# 在单个事件中上报 item $rc->sendEvent("test-uuidsdk1", 1001, null, "php_single_event_with_item", ["php_name" => "php", "php_version" => "5.6"], [["item_name" => "book", "item_id" => "book3"], ["item_name" => "book", "item_id" => "book4"]] );
// 使用 device_id 匿名上报 $header2 = new Header(); $header2->setAppId(10000000); $header2->setUserUniqueId(""); $header2->setDeviceId(7033713549469860107); $rc->profileSetWithHeader($header2, ["profile_php_name" => "php7", "profile_php_version" => "7.4", "profile_int" => 2, "testdate"=>"2023-05-16"]);
// 使用 anonymousId 匿名上报 $webRc = new WebEventCollector(); $header3 = new Header(); $header3->setAppId(10000000); $header3->setUserUniqueId(""); $header3->setAnonymousId("test_anonymousId1"); $webRc->sendUserDefineEvent($header3, "", 10000000, null, "php_event_with_anonymous_id", ["php_name" => "php", "php_version" => "5.6", "float_param" => floatval(5), "session_id" => "1234567890"]);
// 使用 user_unique_id_type 进行多口径上报,版本要求: >= 1.0.12 $appRc = new AppEventCollector(); $header3 = new Header(); $header3->setAppId(10000000); $header3->setUserUniqueIdType("phone_id"); $appRc->sendUserDefineEvent($header3, "test_sdk_phone_id1", 10000000, null, "php_event_with_anonymous_id", ["php_name" => "php", "php_version" => "5.6", "float_param" => floatval(5), "session_id" => "1234567890"]);
id
属性