最近更新时间:2024.02.27 11:47:20
首次发布时间:2024.02.27 11:47:20
PHP 版本要求 >= 7.4
在composer.json中添加依赖:(>=1.0.7 的版本才支持saas,^1.0 表示使用最新的版本 )
{ "require": { "datarangers/datarangers": "^1.0" } }
执行命令:
composer require datarangers/datarangers
为了您更好地理解和使用我们的sdk,我们已经将SDK进行了开源,具体地址为https://github.com/volcengine/datarangers-sdk-php。您可以直接将SDK源码下载并使用。
// 初始化 CollectorConfig::init_datarangers_collector([ "domain" => "https://xxxx", "save" => false, "headers" => [ "Content-Type" => "application/json" ], // 配置 app_id => app_key, 1001 是举例的应用id,需要替换成实际的应用id "app_keys" => [ 1001 => getenv("APP_KEY") ], // 配置 openapi "openapi" => [ "domain" => "https://xxxx", "ak" => getenv("OPENAPI_AK"), "sk" => getenv("OPENAPI_SK") ] ]);
配置说明
需要配合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" => [ "Host" => "xxxx", "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头。私有化需要配置Host,Host 为私有化环境Host的配置在安装部署的那台机器上,查看/home/{INSTALL_USER}/DataRangersDeploy/conf_rangers.yml中配置项sdk.report.host。 INSTALL_USER 为安装用户,一般是datarangers或者minibase。Host 注意没有 http://,https:// ,一般为 sdk 上报的域名去掉http://,https:// | |
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 appid * @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); /** * set user profile * @param $userUniqueId string * @param $appId int * @param $eventParams array set profile example ["php_version"=>"1.3.0"] * @return mixed */ public function profileSet($userUniqueId, $appId, $eventParams); /** * @param $userUniqueId string * @param $appId int * @param $eventParams array unset profile 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 item name * @param $items array item example:["item_id"=>"0001","item_name"=>"book","item_price"=>5.0] * @return mixed */ public function itemSet($appId, $itemName, $items); /** * @param $appId int * @param $itemName string * @param $itemId string * @param $items array example: ["param1","param2","param3"] * @return mixed */ public function itemUnset($appId, $itemName, $itemId, $items);
// 需要先初始化配置再使用,这里以saas的初始化为例子,私有化的环境下,请使用私有化的相关配置 CollectorConfig::init_datarangers_collector([ "domain" => "https://xxxx", "save" => false, "headers" => [ "Content-Type" => "application/json" ], // 配置 app_id => app_key, 1001 是举例的应用id,需要替换成实际的应用id "app_keys" => [ 1001 => getenv("APP_KEY") ], // 配置 openapi "openapi" => [ "domain" => "https://xxxx", "ak" => getenv("OPENAPI_AK"), "sk" => getenv("OPENAPI_SK") ] ]); $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 进行多口径上报 $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
属性