-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mida.php
142 lines (131 loc) · 4.51 KB
/
Mida.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
class Mida {
protected $publicKey;
protected $host;
protected $user_id;
protected $enabled_features;
protected $maxCacheSize;
protected $featureFlagCache;
function __construct($publicKey, $options = []) {
if (!$publicKey) {
throw new Exception("You must pass your Mida project key");
}
$this->publicKey = $publicKey;
$this->host = 'https://api.mida.so';
$this->user_id = null;
$this->enabled_features = [];
$this->maxCacheSize = isset($options['maxCacheSize']) ? $options['maxCacheSize'] : 50000;
$this->featureFlagCache = new ArrayObject();
}
function getExperiment($experimentKey, $distinctId) {
if (!$experimentKey || !$distinctId || !$this->user_id) {
throw new Exception("You must pass your Mida experiment key. You must pass your user distinct ID");
}
$data = [
'key' => $this->publicKey,
'experiment_key' => $experimentKey,
'distinct_id' => $distinctId || $this->user_id
];
$headers = [];
$client = new Client([
'base_uri' => $this->host
]);
$response = $client->request('POST', '/experiment/query', [
'headers' => $headers,
'json' => $data
]);
$json = json_decode($response->getBody()->getContents(), true);
if ($json['version']) {
return $json['version'];
}
return null;
}
function setEvent($eventName, $distinctId, $properties = []) {
if (!$eventName || !$distinctId) {
throw new Exception("You need to set an event name. You must pass your user distinct ID");
}
$data = [
'key' => $this->publicKey,
'name' => $eventName,
'distinct_id' => $distinctId ?: $this->user_id
'properties' => json_encode($properties)
];
$headers = [];
$client = new Client([
'base_uri' => $this->host
]);
$response = $client->request('POST', '/experiment/event', [
'headers' => $headers,
'json' => $data
]);
return true;
}
function setAttribute($distinctId, $properties = []) {
if (!$distinctId || !$this->user_id) {
throw new Exception("You must pass your user distinct ID");
}
if (!$properties) {
throw new Exception("You must pass your user properties");
}
$data = $properties;
$data['id'] = $distinctId || $this->user_id;
$headers = [];
$client = new Client([
'base_uri' => $this->host
]);
$response = $client->request('POST', '/track/' . $this->publicKey, [
'headers' => $headers,
'json' => $data
]);
return true;
}
function cachedFeatureFlag() {
$cacheKey = $this->publicKey . ':' . $this->user_id;
if ($this->featureFlagCache->offsetExists($cacheKey)) {
return $this->featureFlagCache->offsetGet($cacheKey);
}
return [];
}
function isFeatureEnabled($key) {
$this->enabled_features = $this->cachedFeatureFlag();
return in_array($key, $this->enabled_features);
}
function onFeatureFlags($distinctId = null) {
$cachedItems = count($this->cachedFeatureFlag());
try {
$this->reloadFeatureFlags($distinctId);
if (!$cachedItems) {
return true;
}
} catch (Exception $e) {
throw $e;
}
if ($cachedItems) {
return true;
}
}
function reloadFeatureFlags($distinctId = null) {
$data = [
'key' => $this->publicKey,
'user_id' => $distinctId
];
$headers = [];
$client = new Client([
'base_uri' => $this->host
]);
$response = $client->request('POST', '/feature-flag', [
'headers' => $headers,
'json' => $data
]);
$this->enabled_features = json_decode($response->getBody()->getContents(), true);
$cacheKey = $this->publicKey . ':' . $this->user_id;
$this->featureFlagCache->offsetSet($cacheKey, $this->enabled_features);
if ($this->featureFlagCache->count() > $this->maxCacheSize) {
$oldestKey = $this->featureFlagCache->key();
$this->featureFlagCache->offsetUnset($oldestKey);
}
return true;
}
}