Skip to content

Commit 485f15c

Browse files
authored
Add StatEventBuilder (#24)
1 parent 8c648c7 commit 485f15c

File tree

3 files changed

+199
-6
lines changed

3 files changed

+199
-6
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ You can create an experiment and get your token and id of experiment on [ABRoute
6464
use Abrouter\Client\Config\Config;
6565
use DI\ContainerBuilder;
6666
use Abrouter\Client\Client;
67+
use Abrouter\Client\Builders\StatEventBuilder;
6768

6869
require '/app/vendor/autoload.php';
6970

@@ -76,17 +77,32 @@ $di = $containerBuilder->build();
7677
* @var Client $client
7778
*/
7879
$client = $di->make(Abrouter\Client\Client::class); // using PHP-DI
79-
$client->statistics()->sendEvent(new EventDTO(
80-
null, // temporary user id
81-
$userId, // permanent user id
82-
'visited_test_page'
83-
));
80+
81+
$eventBuilder = $this->getContainer()->make(StatEventBuilder::class);
82+
83+
//sending button_click event
84+
$client->statistics()->sendEvent(
85+
$eventBuilder
86+
->incremental()
87+
->event('button_click')
88+
->setUserId($userSignature)
89+
->build()
90+
);
91+
92+
// sending purchase event
93+
$client->statistics()->sendEvent(
94+
$eventBuilder
95+
->incremental()
96+
->event('purchase')
97+
->setValue("30")
98+
->setUserId($userSignature)
99+
->build()
100+
);
84101
```
85102

86103
Please note, you can use the IncrementalEventDTO (Abrouter\Client\DTO\IncrementalEventDTO) if you would like to send the increment counter statistics, and SummarizeEventDTO(same namespace) to track some sum.
87104

88105

89-
90106
## Parallel running
91107

92108
Parallel running is a mode which allows you to run the experiments asynchronous.

src/Builders/StatEventBuilder.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abrouter\Client\Builders;
6+
7+
use Abrouter\Client\DTO\BaseEventDTO;
8+
use Abrouter\Client\DTO\EventDTOInterface;
9+
use Abrouter\Client\DTO\IncrementalEventDTO;
10+
use Abrouter\Client\DTO\SummarizeEventDTO;
11+
12+
class StatEventBuilder
13+
{
14+
private const EVENT_TYPE_INCREMENTAL = 1;
15+
private const EVENT_TYPE_SUMMARIZE = 2;
16+
17+
private ?string $temporaryUserId = null;
18+
private ?string $userId = null;
19+
private ?string $event = null;
20+
private ?string $referrer = null;
21+
private ?array $meta = null;
22+
private ?string $tag = null;
23+
private ?string $ip = null;
24+
private ?string $createdAt = null;
25+
private ?string $value = null;
26+
27+
private int $type = self::EVENT_TYPE_INCREMENTAL;
28+
29+
public function setTemporaryUserId(string $temporaryUserId): self
30+
{
31+
$this->temporaryUserId = $temporaryUserId;
32+
return $this;
33+
}
34+
35+
public function setUserId(string $userId): self
36+
{
37+
$this->userId = $userId;
38+
return $this;
39+
}
40+
41+
public function event(string $event): self
42+
{
43+
$this->event = $event;
44+
return $this;
45+
}
46+
47+
public function setReferrer(string $referrer): self
48+
{
49+
$this->referrer = $referrer;
50+
return $this;
51+
}
52+
53+
public function setTag(string $tag): self
54+
{
55+
$this->tag = $tag;
56+
return $this;
57+
}
58+
59+
public function setMeta(array $meta): self
60+
{
61+
$this->meta = $meta;
62+
return $this;
63+
}
64+
65+
public function setIp(string $ip): self
66+
{
67+
$this->ip = $ip;
68+
return $this;
69+
}
70+
71+
public function setCreatedAt(string $createdAt): self
72+
{
73+
$this->createdAt = $createdAt;
74+
return $this;
75+
}
76+
77+
public function setValue($value): self
78+
{
79+
$this->value = (string) $value;
80+
return $this;
81+
}
82+
83+
public function incremental(): self
84+
{
85+
$this->type = self::EVENT_TYPE_INCREMENTAL;
86+
return $this;
87+
}
88+
89+
public function summarize(): self
90+
{
91+
$this->type = self::EVENT_TYPE_SUMMARIZE;
92+
return $this;
93+
}
94+
95+
public function build(): EventDTOInterface
96+
{
97+
$baseEventDTO = new BaseEventDTO(
98+
$this->temporaryUserId,
99+
$this->userId,
100+
$this->event,
101+
$this->tag,
102+
$this->referrer,
103+
$this->meta,
104+
$this->ip,
105+
$this->createdAt
106+
);
107+
108+
if ($this->type === self::EVENT_TYPE_INCREMENTAL) {
109+
return new IncrementalEventDTO($baseEventDTO);
110+
}
111+
112+
return new SummarizeEventDTO($this->value, $baseEventDTO);
113+
}
114+
}

tests/Integration/StatSendSyncTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Abrouter\Client\Tests\Integration;
66

7+
use Abrouter\Client\Builders\StatEventBuilder;
78
use Abrouter\Client\Client;
89
use Abrouter\Client\DTO\BaseEventDTO;
910
use Abrouter\Client\DTO\EventDTO;
@@ -101,4 +102,66 @@ public function testSummarizableStatSend()
101102
$this->assertEquals($event->getValue(), '10');
102103
}
103104
}
105+
106+
public function testSummarizableStatSendWithBuilder()
107+
{
108+
$this->bindConfig(
109+
'https://abrouter.com',
110+
'add73bda37106bbddf2e6b3f61c6ed197c2250e99df9474ad01b9afb2035af33cf66c292fdf6a6e8',
111+
);
112+
$this->clearRedis();
113+
114+
$client = $this->getContainer()->make(Client::class);
115+
116+
$eventBuilder = $this->getContainer()->make(StatEventBuilder::class);
117+
118+
$userSignature = 'test-run-f:' . uniqid();
119+
$client->statistics()->sendEvent(
120+
$eventBuilder
121+
->summarize()
122+
->event('event1_sum')
123+
->setUserId($userSignature)
124+
->setValue('10')
125+
->build()
126+
);
127+
128+
$userEventsRepository = $this->getContainer()->make(UserEventsRepository::class);
129+
$events = $userEventsRepository->getUserEvents($userSignature);
130+
131+
$this->assertEquals(1, sizeof($events->getStatisticEvents()));
132+
foreach ($events->getStatisticEvents() as $event) {
133+
$this->assertEquals($event->getEvent(), 'event1_sum');
134+
$this->assertEquals($event->getValue(), '10');
135+
}
136+
}
137+
138+
public function testIncrementalStatSendWithBuilder()
139+
{
140+
$this->bindConfig(
141+
'https://abrouter.com',
142+
'add73bda37106bbddf2e6b3f61c6ed197c2250e99df9474ad01b9afb2035af33cf66c292fdf6a6e8',
143+
);
144+
$this->clearRedis();
145+
146+
$client = $this->getContainer()->make(Client::class);
147+
148+
$eventBuilder = $this->getContainer()->make(StatEventBuilder::class);
149+
150+
$userSignature = 'test-run-f:' . uniqid();
151+
$client->statistics()->sendEvent(
152+
$eventBuilder
153+
->incremental()
154+
->event('event1')
155+
->setUserId($userSignature)
156+
->build()
157+
);
158+
159+
$userEventsRepository = $this->getContainer()->make(UserEventsRepository::class);
160+
$events = $userEventsRepository->getUserEvents($userSignature);
161+
162+
$this->assertEquals(1, sizeof($events->getStatisticEvents()));
163+
foreach ($events->getStatisticEvents() as $event) {
164+
$this->assertEquals($event->getEvent(), 'event1');
165+
}
166+
}
104167
}

0 commit comments

Comments
 (0)