Skip to content

Commit 4d1b22d

Browse files
committed
Event tracking works
1 parent 751ef48 commit 4d1b22d

3 files changed

Lines changed: 139 additions & 122 deletions

File tree

src/Models/event-options.php

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,74 @@
11
<?php
22

33

4-
54
class User
65
{
7-
public $id;
8-
public $name;
9-
public $email;
10-
11-
public function __construct($id, $name = null, $email = null)
12-
{
13-
$this->$id = $id;
14-
$this->name = $name;
15-
$this->email = $email;
16-
}
6+
public $id;
7+
public $name;
8+
public $email;
9+
10+
public function __construct($id, $name = null, $email = null)
11+
{
12+
$this->id = $id;
13+
$this->name = $name;
14+
$this->email = $email;
15+
}
1716
}
1817

1918
class EventOptions
2019
{
21-
const EVENT_IP = 'ip';
22-
const EVENT_USER_AGENT = 'userAgent';
23-
const EVENT_EVENT_TYPE = 'eventType';
24-
const EVENT_REMOTE_IP = 'remoteIp';
25-
const EVENT_USER = 'user';
26-
const EVENT_USER_ID = 'user';
27-
const EVENT_USER_NAME= 'user';
28-
const EVENT_USER_EMAIL = 'user';
29-
const EVENT_DEVICE = 'device';
30-
const EVENT_PARAMS = 'params';
31-
32-
public $ip;
33-
public $userAgent;
34-
public $eventType;
35-
public $remoteIp;
36-
public $user;
37-
public $device;
38-
public $params = array();
39-
40-
public function __construct($json = false) {
41-
if ($json) $this->set(json_decode($json, true));
42-
}
43-
44-
private function set($data) {
45-
foreach ($data AS $key => $value) {
46-
if (is_array($value)) {
47-
// $sub = new JSONObject;
48-
// TODO: Ask Alex what type of object is correct here
49-
$sub = new EventOptions;
50-
$sub->set($value);
51-
$value = $sub;
52-
}
53-
$this->{$key} = $value;
54-
}
55-
}
20+
const EVENT_IP = 'ip';
21+
const EVENT_USER_AGENT = 'userAgent';
22+
const EVENT_EVENT_TYPE = 'eventType';
23+
const EVENT_REMOTE_IP = 'remoteIp';
24+
const EVENT_USER = 'user';
25+
const EVENT_USER_ID = 'id';
26+
const EVENT_USER_NAME = 'name';
27+
const EVENT_USER_EMAIL = 'email';
28+
const EVENT_DEVICE = 'device';
29+
const EVENT_PARAMS = 'params';
30+
31+
public $ip;
32+
public $userAgent;
33+
public $eventType;
34+
public $remoteIp;
35+
public $user;
36+
public $device;
37+
public $params = array();
38+
39+
public function __construct($json = false)
40+
{
41+
if ($json) $this->set(json_decode($json, true));
42+
}
43+
44+
private function set($data)
45+
{
46+
$user = $data[self::EVENT_USER];
47+
48+
$this->user = new User(
49+
isset($user[self::EVENT_USER_ID]) ? $user[self::EVENT_USER_ID] : '',
50+
isset($user[self::EVENT_USER_NAME]) ? $user[self::EVENT_USER_NAME] : '',
51+
isset($user[self::EVENT_USER_EMAIL]) ? $user[self::EVENT_USER_EMAIL] : ''
52+
);
53+
$this->device = isset($data[self::EVENT_DEVICE]) ? $data[self::EVENT_DEVICE] : null;
54+
$this->ip = isset($data[self::EVENT_IP]) ? $data[self::EVENT_IP] : null;
55+
$this->userAgent = isset($data[self::EVENT_USER_AGENT]) ? $data[self::EVENT_USER_AGENT] : null;
56+
$this->eventType = isset($data[self::EVENT_EVENT_TYPE]) ? $data[self::EVENT_EVENT_TYPE] : null;
57+
$this->remoteIp = isset($data[self::EVENT_REMOTE_IP]) ? $data[self::EVENT_REMOTE_IP] : null;
58+
$this->params = isset($data[self::EVENT_PARAMS]) ? $data[self::EVENT_PARAMS] : array();
59+
}
60+
61+
// private function set($data)
62+
// {
63+
// foreach ($data AS $key => $value) {
64+
// if (is_array($value)) {
65+
// //$sub = new JSONObject;
66+
// // TODO: Ask Alex what type of object is correct here
67+
// $sub = new EventOptions;
68+
// $sub->set($value);
69+
// $value = $sub;
70+
// }
71+
// $this->{$key} = $value;
72+
// }
73+
// }
5674
}

src/event-manager.php

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,81 @@
1010

1111
class EventManager
1212
{
13-
private $apiKey;
14-
private $httpClient;
15-
private $options;
16-
private $eventsQueue;
17-
18-
public function __construct($apiKey, SecureNativeOptions $secureNativeOptions)
19-
{
20-
$this->apiKey = $apiKey;
21-
$this->options = $secureNativeOptions;
22-
$this->httpClient = new HttpClient($this->apiKey, $this->options);
23-
$this->eventsQueue = array();
24-
}
25-
26-
public function buildEvent(EventOptions $opts)
27-
{
28-
$cookie = Utils::cookieIdFromRequest() || Utils::securHeaderFromRequest();
29-
$cookieDecoded = Utils::decrypt($cookie, $this->apiKey);
30-
$clientFP = json_decode($cookieDecoded);
31-
$eventType = $opts->eventType || EventTypes::LOG_IN;
32-
33-
$cid = $clientFP['cid'] || '';
34-
$vid = Utils::generateGuidV4();
35-
$fp = $clientFP['fp'] || '';
36-
$ip = $opts->ip || Utils::clientIpFromRequest();
37-
$remoteIP = $opts->remoteIp || Utils::clientIpFromRequest();
38-
$userAgent = $opts->userAgent || Utils::userAgentFromRequest();
39-
$user = $opts->user || new User('anonymous');
40-
$ts = round(microtime(true) * 1000);
41-
$device = $opts->device || null;
42-
$params =$opts->params;
43-
44-
return new SecurenativeEvent($eventType, $cid, $vid, $fp, $ip, $remoteIP, $userAgent, $user, $ts, $device, $params);
45-
}
46-
47-
public function sendSync(SecurenativeEvent $event, $requestUrl)
48-
{
49-
try {
50-
$request = new Request('POST', $requestUrl, [], Utils::serialize($event));
51-
$response = $this->httpClient->send($request);
52-
return json_decode($response>getBody());
53-
} catch (RequestException $e) {
54-
return null;
55-
}
56-
}
57-
58-
public function sendAsync(SecurenativeEvent $event, $requestUrl)
59-
{
60-
if (count($this->eventsQueue) >= $this->options->getMaxEvents()) {
61-
array_shift($$this->eventsQueue);
13+
private $apiKey;
14+
private $httpClient;
15+
private $options;
16+
private $eventsQueue;
17+
18+
public function __construct($apiKey, SecureNativeOptions $secureNativeOptions)
19+
{
20+
$this->apiKey = $apiKey;
21+
$this->options = $secureNativeOptions;
22+
$this->httpClient = new HttpClient($this->apiKey, $this->options);
23+
$this->eventsQueue = array();
24+
}
25+
26+
public function buildEvent(EventOptions $opts)
27+
{
28+
$cookie = Utils::cookieIdFromRequest() ? Utils::cookieIdFromRequest() : Utils::securHeaderFromRequest();
29+
$cookieDecoded = Utils::decrypt($cookie, $this->apiKey);
30+
$clientFP = json_decode($cookieDecoded);
31+
$eventType = $opts->eventType ? $opts->eventType : EventTypes::LOG_IN;
32+
33+
$cid = $clientFP['cid'] ? $clientFP['cid'] : '';
34+
$vid = Utils::generateGuidV4();
35+
$fp = $clientFP['fp'] ? $clientFP['fp'] : '';
36+
$ip = $opts->ip ? $opts->ip : Utils::clientIpFromRequest();
37+
$remoteIP = $opts->remoteIp ? $opts->remoteIp : Utils::clientIpFromRequest();
38+
$userAgent = $opts->userAgent ? $opts->userAgent : Utils::userAgentFromRequest();
39+
$user = $opts->user ? $opts->user : new User('anonymous');
40+
$ts = round(microtime(true) * 1000);
41+
$device = $opts->device;
42+
$params = $opts->params;
43+
44+
return new SecurenativeEvent($eventType, $cid, $vid, $fp, $ip, $remoteIP, $userAgent, $user, $ts, $device, $params);
45+
}
46+
47+
public function sendSync(SecurenativeEvent $event, $requestUrl)
48+
{
49+
try {
50+
$request = new Request('POST', $requestUrl, [], Utils::serialize($event));
51+
$response = $this->httpClient->send($request);
52+
return json_decode($response > getBody());
53+
} catch (RequestException $e) {
54+
return null;
55+
}
6256
}
6357

64-
// TODO: Is json_encode() correct here?
65-
$request = new Request('POST', $requestUrl, [], json_encode(Utils::serialize($event)));
58+
public function sendAsync(SecurenativeEvent $event, $requestUrl)
59+
{
60+
if (count($this->eventsQueue) >= $this->options->getMaxEvents()) {
61+
array_shift($$this->eventsQueue);
62+
}
63+
64+
// TODO: Is json_encode() correct here?
65+
$request = new Request('POST', $requestUrl, [], Utils::serialize($event));
6666
// $request = new Request('POST', $requestUrl, [], $event);
6767

68-
array_push($this->eventsQueue, $request);
69-
70-
$this::sendEvents();
71-
}
72-
73-
private function sendEvents()
74-
{
75-
for ($i = 0; $i < count($this->eventsQueue); $i++) {
76-
$request = $this->eventsQueue[$i];
77-
78-
$promise = $this->httpClient->sendAsync($request);
79-
$promise->then(
80-
function (ResponseInterface $res) use ($request) {
81-
if (($key = array_search($request, $this->eventsQueue)) !== false) {
82-
unset($this->eventsQueue[$key]);
83-
}
84-
},
85-
function (RequestException $e) {
86-
87-
});
88-
}
89-
}
90-
68+
array_push($this->eventsQueue, $request);
69+
70+
$this::sendEvents();
71+
}
72+
73+
private function sendEvents()
74+
{
75+
for ($i = 0; $i < count($this->eventsQueue); $i++) {
76+
$request = $this->eventsQueue[$i];
77+
78+
$promise = $this->httpClient->sendAsync($request);
79+
$promise->then(function ($res) use ($request) {
80+
if (($key = array_search($request, $this->eventsQueue)) !== false) {
81+
unset($this->eventsQueue[$key]);
82+
}
83+
}, function (RequestException $e) {
84+
echo $e->getMessage() . "\n";
85+
});
86+
$promise->wait();
87+
}
88+
}
89+
9190
}

src/utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function generateGuidV4()
6161

6262
public static function serialize($obj)
6363
{
64-
return json_decode(json_encode($obj), true);
64+
return json_encode($obj);
6565
}
6666

6767
public static function decrypt($cipherText, $cipherKey)
@@ -76,7 +76,7 @@ public static function decrypt($cipherText, $cipherKey)
7676
return $decrypted;
7777
} else {
7878
echo openssl_error_string();
79-
return false;
79+
return "";
8080
}
8181
}
8282
}

0 commit comments

Comments
 (0)