更新时间:2023.03.21 17:25:03
在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") ] ]);
配置说明
domain,根据接入的环境配置不同的地址:
Saas 只支持save为false的
app_keys, 应用的APP Key,获取位置请参考以下截图:
如果需要上报 item 和用户属性,需要设置openapi:
CollectorConfig::init_datarangers_collector([ "save" => true, "logger_file_prefix" => "sdk/log/", "logger_file_name" => "datarangers.log", "log_max_bytes" => 1024 * 10 ]);
init_datarangers_collector方法的入参为一个array,具体的参数为
模式 | 参数名 | 是否为必须 | 说明 |
---|---|---|---|
Http模式 | save | 是 | Http模式下为false |
domain | 是 | DataRangers的域名或ip,支持http和https | |
headers | 否 | Http的heade头,如果配置了Host,则需要在headers中加入Host信息。 | |
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"]] );
生成的日志文件不会自动删除,请自行删除。
Saas 上用户属性需要先在系统中创建之后再上报,参考<<User Profile API (服务端-SAAS)>>
Item属性需要先在系统中创建之后再上报,参考<<业务对象(Item)数据接入(SAAS查看>>
业务对象内置的id属性,对应的字段名称为item_id,因此不需要再定义一个 id
属性