Skip to content

Commit

Permalink
feat: 支持设置sendtime (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
styluo authored Apr 1, 2022
1 parent 027f458 commit 9be4abe
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ $gio = GrowingIO::getInstance($accountID, $host, $dataSourceId, $props);
###### 请求参数
|参数|必选|类型|默认值|说明|
|:----|:----|:----|:----|-----|
|evnetTime|false|int|当前时间的时间戳|事件发生时间。如需要开启"自定义event_time上报"的功能开关,请联系技术支持|
|loginUserKey|false|string| |登录用户类型|
|loginUserId|true|string| |登录用户id|
|eventKey|true|string| |事件名, 事件标识符|
|properties|false|array|array()|事件发生时,所伴随的维度信息|
###### 示例
```php
$gio->trackCustomEvent($gio->getCustomEventFactory('loginUserId', 'eventName')
->setEventTime(1648524854000)
->setLoginUserKey('loginUserKey')
->setProperties(array('attrKey1' => 'attrValue1', 'attrKey2' => 'attrValue2'))
->create()
Expand Down
2 changes: 2 additions & 0 deletions example/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function currentMillisecond()
printf($start . PHP_EOL);
$gio->setUserAttributes('phpUserId', array('userKey1' => 'v1', 'userKey2' => 'v2'));
$gio->setUserAttributesEvent($gio->getUserAttributesFactory('pUserId')
->setEventTime((time() - 24 * 60 * 60) * 1000)
->setLoginUserKey('pUserKey')
->setProperties(array('userKey1' => 'v1', 'userKey2' => 'v2'))
->create());
Expand All @@ -37,6 +38,7 @@ function currentMillisecond()
array('userKey1' => 'v1', 'userKey2' => 'v2')
);
$gio->trackCustomEvent($gio->getCustomEventFactory('loginUserId', 'pEvent')
->setEventTime((time() - 24 * 60 * 60) * 1000)
->setLoginUserKey('loginUserKey')
->setProperties(array('userKey1' => 'v1', 'userKey2' => 'v2'))
->create()
Expand Down
93 changes: 69 additions & 24 deletions src/GrowingIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public static function getInstance($accountID, $host, $dataSourceId, $options =
/**
* track a custom event
*
* @param $loginUserId loginUser's ID
* @param $eventKey the key of customEvent, registered in GrowingIO
* @param array $properties the properties of this event, registered in GrowingIO
* @param $id 物品模型id
* @param $key 物品模型key
* @param $loginUserId loginUser's ID
* @param $eventKey the key of customEvent, registered in GrowingIO
* @param array $properties the properties of this event, registered in GrowingIO
* @param $id 物品模型id
* @param $key 物品模型key
* @return void
*/
public function track($loginUserId, $eventKey, $properties = array(), $id = null, $key = null)
Expand All @@ -128,9 +128,12 @@ public function track($loginUserId, $eventKey, $properties = array(), $id = null
$this->consumer->consume($event);
}

public function getCustomEventFactory($loginUserId, $eventKey) {
return new CustomEventFactory($this->dataSourceId, $loginUserId, $eventKey,
($this->isIdMappingEnabled()));
public function getCustomEventFactory($loginUserId, $eventKey)
{
return new CustomEventFactory(
$this->dataSourceId, $loginUserId, $eventKey,
($this->isIdMappingEnabled())
);
}

public function trackCustomEvent(CustomEvent $customEvent)
Expand All @@ -150,18 +153,23 @@ public function setUserAttributes($logUserId, $properties)
$this->consumer->consume($user);
}

public function getUserAttributesFactory($loginUserId) {
return new UserAttributesFactory($this->dataSourceId, $loginUserId,
($this->isIdMappingEnabled()));
public function getUserAttributesFactory($loginUserId)
{
return new UserAttributesFactory(
$this->dataSourceId, $loginUserId,
($this->isIdMappingEnabled())
);
}

public function setUserAttributesEvent(UserProps $userAttributesEvent) {
public function setUserAttributesEvent(UserProps $userAttributesEvent)
{
if (!is_null($userAttributesEvent)) {
$this->consumer->consume($userAttributesEvent);
}
}

public function isIdMappingEnabled() {
public function isIdMappingEnabled()
{
return (isset($this->options['idMappingEnabled']) && $this->options['idMappingEnabled'] === true);
}

Expand Down Expand Up @@ -190,44 +198,59 @@ class CustomEventFactory
private $id;
private $key;
private $idMappingEnabled;
private $eventTime;

public function __construct($dataSourceId, $loginUserId, $eventKey, $idMappingEnabled) {
public function __construct($dataSourceId, $loginUserId, $eventKey, $idMappingEnabled)
{
$this->dataSourceId = $dataSourceId;
$this->loginUserId = $loginUserId;
$this->eventKey = $eventKey;
$this->idMappingEnabled = $idMappingEnabled;
}

public function setEventKey($eventKey) {
public function setEventKey($eventKey)
{
$this->eventKey = $eventKey;
}

public function setLoginUserKey($loginUserKey) {
public function setLoginUserKey($loginUserKey)
{
$this->loginUserKey = $loginUserKey;
return $this;
}

public function setLoginUserId($loginUserId) {
public function setLoginUserId($loginUserId)
{
$this->loginUserId = $loginUserId;
return $this;
}

public function setProperties($properties) {
public function setProperties($properties)
{
$this->properties = $properties;
return $this;
}

public function setId($id) {
public function setId($id)
{
$this->id = $id;
return $this;
}

public function setKey($key) {
public function setKey($key)
{
$this->key = $key;
return $this;
}

public function create() {
public function setEventTime($eventTime)
{
$this->eventTime = $eventTime;
return $this;
}

public function create()
{
$customEvent = new CustomEvent();

if (!empty($this->loginUserId) && !empty($this->eventKey) && !empty($this->dataSourceId)) {
Expand All @@ -249,6 +272,10 @@ public function create() {
if (!empty($this->id) && !empty($this->key)) {
$customEvent->resourceItem(array('id' => $this->id, 'key' => $this->key));
}

if (!empty($this->eventTime)) {
$customEvent->eventTime($this->eventTime);
}
return $customEvent;
}
}
Expand All @@ -264,6 +291,7 @@ class CustomEvent implements \JsonSerializable
private $eventType;
private $dataSourceId;
private $resourceItem;
private $sendTime;

public function __construct()
{
Expand All @@ -279,6 +307,7 @@ public function dataSourceId($dataSourceId)
public function eventTime($time)
{
$this->timestamp = $time;
$this->sendTime = $time;
}

public function eventKey($eventKey)
Expand Down Expand Up @@ -317,6 +346,7 @@ class UserAttributesFactory
private $loginUserKey;
private $loginUserId;
private $idMappingEnabled;
private $eventTime;

public function __construct($dataSourceId, $loginUserId, $idMappingEnabled)
{
Expand All @@ -325,21 +355,30 @@ public function __construct($dataSourceId, $loginUserId, $idMappingEnabled)
$this->loginUserId = $loginUserId;
}

public function setLoginUserKey($loginUserKey) {
public function setLoginUserKey($loginUserKey)
{
$this->loginUserKey = $loginUserKey;
return $this;
}

public function setLoginUserId($loginUserId) {
public function setLoginUserId($loginUserId)
{
$this->loginUserId = $loginUserId;
return $this;
}

public function setProperties($properties) {
public function setProperties($properties)
{
$this->properties = $properties;
return $this;
}

public function setEventTime($eventTime)
{
$this->eventTime = $eventTime;
return $this;
}

public function create()
{
$userProps = new UserProps();
Expand All @@ -358,6 +397,10 @@ public function create()
$userProps->userProperties($this->properties);
}

if (!empty($this->eventTime)) {
$userProps->eventTime($this->eventTime);
}

return $userProps;
}
}
Expand All @@ -371,6 +414,7 @@ class UserProps implements \JsonSerializable
private $eventType;
private $dataSourceId;
private $timestamp;
private $sendTime;

public function __construct()
{
Expand All @@ -386,6 +430,7 @@ public function dataSourceId($dataSourceId)
public function eventTime($time)
{
$this->timestamp = $time;
$this->sendTime = $time;
}

public function loginUserKey($loginUserKey)
Expand Down
8 changes: 7 additions & 1 deletion test/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public function testTrackCustomEvent()
'userKey' => 'userKey',
'userId' => 'userId',
'eventType' => 'CUSTOM',
'dataSourceId' => '12345678'],
'dataSourceId' => '12345678',
'timestamp' => '1648524854000',
'sendTime' => '1648524854000'],
$data
);
});
self::$gio->trackCustomEvent(self::$gio->getCustomEventFactory('userId', "eventKey")
->setEventTime(1648524854000)
->setLoginUserKey('userKey')
->create());
}
Expand Down Expand Up @@ -123,11 +126,14 @@ public function testSetUserAttributesEvent()
['userId' => 'userId',
'eventType' => 'LOGIN_USER_ATTRIBUTES',
'dataSourceId' => '12345678',
'timestamp' => '1648524854000',
'sendTime' => '1648524854000',
'attributes' => array('userKey1' => 'v1', 'userKey2' => 'v2')],
$data
);
});
self::$gio->setUserAttributesEvent(self::$gio->getUserAttributesFactory('userId')
->setEventTime(1648524854000)
->setLoginUserKey('userKey')
->setProperties(array('userKey1' => 'v1', 'userKey2' => 'v2'))
->create());
Expand Down

0 comments on commit 9be4abe

Please sign in to comment.