Skip to content

Commit

Permalink
Merge pull request #18 from volcengine/feat/anonyous_id
Browse files Browse the repository at this point in the history
feat: support anonymous id
  • Loading branch information
zhangpengspin authored May 16, 2023
2 parents 463ad92 + 0cc526b commit b9dcc7d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace DataRangers;


use DataRangers\Model\Header;

interface Collector
{
/**
Expand Down Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion src/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -266,5 +299,4 @@ public function setItemsWithEvent($items, Event $event): void
$event->setItems($eventWithItems);
}
}

}
23 changes: 23 additions & 0 deletions src/Model/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
17 changes: 17 additions & 0 deletions test/HttpTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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);

0 comments on commit b9dcc7d

Please sign in to comment.