From 0cc526b569da421e7d242c562898298f5bb60c14 Mon Sep 17 00:00:00 2001 From: "zhangpeng.spin" Date: Tue, 16 May 2023 21:48:06 +0800 Subject: [PATCH] feat: support anonymous id --- src/Collector.php | 39 +++++++++++++++++++++++++++++++++++++++ src/EventCollector.php | 34 +++++++++++++++++++++++++++++++++- src/Model/Header.php | 23 +++++++++++++++++++++++ test/HttpTester.php | 17 +++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/Collector.php b/src/Collector.php index c16bacf..cda58e1 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -9,6 +9,8 @@ namespace DataRangers; +use DataRangers\Model\Header; + interface Collector { /** @@ -75,6 +77,43 @@ public function profileIncrement($userUniqueId, $appId, $eventParams); */ public function profileAppend($userUniqueId, $appId, $eventParams); + + /** + * 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); + /** * @param $appId int app id * @param $itemName string item name diff --git a/src/EventCollector.php b/src/EventCollector.php index c67a55a..17e900c 100644 --- a/src/EventCollector.php +++ b/src/EventCollector.php @@ -99,6 +99,14 @@ public function profile($userUniqueId, $appId, $eventName, $eventParams) $header = new Header(); $header->setAppId($appId); $header->setUserUniqueId($userUniqueId); + $this->profileHeader($header, $eventName, $eventParams); + } + + public function profileHeader($header, $eventName, $eventParams) + { + $appId = $header->getAppId(); + $userUniqueId = $header->getUserUniqueId(); + $events = []; $event = new Event($userUniqueId); $event->setEvent($eventName); @@ -152,6 +160,31 @@ public function profileAppend($userUniqueId, $appId, $eventParams) $this->profile($userUniqueId, $appId, ProfileMethod::APPEND, $eventParams); } + public function profileSetWithHeader($header, $eventParams) + { + $this->profileHeader($header, ProfileMethod::SET, $eventParams); + } + + public function profileUnsetWithHeader($header, $eventParams) + { + $this->profileHeader($header, ProfileMethod::UN_SET, $eventParams); + } + + public function profileSetOnceWithHeader($header, $eventParams) + { + $this->profileHeader($header, ProfileMethod::SET_ONCE, $eventParams); + } + + public function profileIncrementWithHeader($header, $eventParams) + { + $this->profileHeader($header, ProfileMethod::INCREMENT, $eventParams); + } + + public function profileAppendWithHeader($header, $eventParams) + { + $this->profileHeader($header, ProfileMethod::APPEND, $eventParams); + } + public function itemSet($appId, $itemName, $items) { $events = []; @@ -266,5 +299,4 @@ public function setItemsWithEvent($items, Event $event): void $event->setItems($eventWithItems); } } - } \ No newline at end of file diff --git a/src/Model/Header.php b/src/Model/Header.php index 9614995..2588560 100644 --- a/src/Model/Header.php +++ b/src/Model/Header.php @@ -55,6 +55,26 @@ class Header implements \JsonSerializable private $vendor_id; private $ssid; + private $anonymous_id; + + /** + * @return mixed + */ + public function getAnonymousId(): string + { + return $this->anonymous_id; + } + + /** + * @param mixed $anonymous_id + */ + public function setAnonymousId(string $anonymous_id): void + { + $this->anonymous_id = $anonymous_id; + } + + + /** * @return mixed */ @@ -750,6 +770,9 @@ public function jsonSerialize() if ($this->user_unique_id != null) $data["user_unique_id"] = $this->user_unique_id; if ($this->vendor_id != null) $data["vendor_id"] = $this->vendor_id; if ($this->ssid != null) $data["ssid"] = $this->ssid; + if ($this->anonymous_id != null) { + $data["anonymous_id"] = $this->anonymous_id; + } return $data; } } \ No newline at end of file diff --git a/test/HttpTester.php b/test/HttpTester.php index 261f864..ec29b30 100644 --- a/test/HttpTester.php +++ b/test/HttpTester.php @@ -5,6 +5,7 @@ use DataRangers\AppEventCollector; use DataRangers\CollectorConfig; use DataRangers\Model\Header; +use DataRangers\WebEventCollector; CollectorConfig::init_datarangers_collector([ "domain" => getenv("HTTP_DOMAIN"), @@ -77,4 +78,20 @@ [["item_name" => "book", "item_id" => "0001"], ["item_name" => "book", "item_id" => "0002"]] ]); +$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"]); + + sleep(10);