1
|
|
|
<?php |
2
|
|
|
namespace Zumba\Amplitude; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Event object, used to make a consistent interface and serialization of the event JSON used in Amplitude API calls |
6
|
|
|
* |
7
|
|
|
* To maintain better parity with the official Amplitude HTTP API, can set the built in properties using underscored |
8
|
|
|
* name (e.g. user_id instead of userId), though camelcase is recommended for better looking code. |
9
|
|
|
* |
10
|
|
|
* @property string $userId |
11
|
|
|
* @property string $deviceId |
12
|
|
|
* @property string $eventType |
13
|
|
|
* @property int $time |
14
|
|
|
* @property array $eventProperties Any values set that do not match built-in property will be set in this array |
15
|
|
|
* @property array $userProperties |
16
|
|
|
* @property string $appVersion |
17
|
|
|
* @property string $platform |
18
|
|
|
* @property string $osName |
19
|
|
|
* @property string $osVersion |
20
|
|
|
* @property string $deviceBrand |
21
|
|
|
* @property string $deviceManufacturer |
22
|
|
|
* @property string $deviceModel |
23
|
|
|
* @property string $deviceType |
24
|
|
|
* @property string $carrier |
25
|
|
|
* @property string $country |
26
|
|
|
* @property string $region |
27
|
|
|
* @property string $city |
28
|
|
|
* @property string $dma |
29
|
|
|
* @property string $language |
30
|
|
|
* @property float $price |
31
|
|
|
* @property int $quantity |
32
|
|
|
* @property float $revenue |
33
|
|
|
* @property string $productId |
34
|
|
|
* @property string $revenueType |
35
|
|
|
* @property float $locationLat |
36
|
|
|
* @property float $locationLng |
37
|
|
|
* @property string $ip |
38
|
|
|
* @property string $idfa |
39
|
|
|
* @property string $adid |
40
|
|
|
*/ |
41
|
|
|
class Event implements \JsonSerializable |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* Array of data for this event |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $data = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Array of built-in properties used for events, and the data type for each one. |
52
|
|
|
* |
53
|
|
|
* The name used here is what is expected by the Amplitude HTTP API, and how the data will be stored internally, |
54
|
|
|
* however these can be set/retrieved using camelcase. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $availableVars = [ |
59
|
|
|
'user_id' => 'string', |
60
|
|
|
'device_id' => 'string', |
61
|
|
|
'event_type' => 'string', |
62
|
|
|
'time' => 'int', |
63
|
|
|
'event_properties' => 'array', |
64
|
|
|
'user_properties' => 'array', |
65
|
|
|
'app_version' => 'string', |
66
|
|
|
'platform' => 'string', |
67
|
|
|
'os_name' => 'string', |
68
|
|
|
'os_version' => 'string', |
69
|
|
|
'device_brand' => 'string', |
70
|
|
|
'device_manufacturer' => 'string', |
71
|
|
|
'device_model' => 'string', |
72
|
|
|
'device_type' => 'string', |
73
|
|
|
'carrier' => 'string', |
74
|
|
|
'country' => 'string', |
75
|
|
|
'region' => 'string', |
76
|
|
|
'city' => 'string', |
77
|
|
|
'dma' => 'string', |
78
|
|
|
'language' => 'string', |
79
|
|
|
'price' => 'float', |
80
|
|
|
'quantity' => 'int', |
81
|
|
|
'revenue' => 'float', |
82
|
|
|
'productId' => 'string', |
83
|
|
|
'revenueType' => 'string', |
84
|
|
|
'location_lat' => 'float', |
85
|
|
|
'location_lng' => 'float', |
86
|
|
|
'ip' => 'string', |
87
|
|
|
'idfa' => 'string', |
88
|
|
|
'adid' => 'string', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Constructor |
93
|
|
|
* |
94
|
|
|
* @param array $data Initial data to set on the event |
95
|
|
|
*/ |
96
|
|
|
public function __construct(array $data = []) |
97
|
|
|
{ |
98
|
|
|
if (!empty($data)) { |
99
|
|
|
$this->set($data); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the user properties on the event |
105
|
|
|
* |
106
|
|
|
* @param array $userProperties |
107
|
|
|
*/ |
108
|
|
|
public function setUserProperties(array $userProperties) |
109
|
|
|
{ |
110
|
|
|
$props = $this->userProperties ?: []; |
111
|
|
|
$this->userProperties = array_merge($props, $userProperties); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set a value in the event. |
117
|
|
|
* |
118
|
|
|
* If the name matches one of the built-in event properties, such as user_id, device_id, etc. OR matches the camel |
119
|
|
|
* case version like userId, deviceId etc. - it will set the built-in property, casting the value to the |
120
|
|
|
* appropriate type for that property |
121
|
|
|
* |
122
|
|
|
* If the name does not match either underscore or camcelcase version of a built in event property name, it will |
123
|
|
|
* set the value in the event_properties array. |
124
|
|
|
* |
125
|
|
|
* It also accepts an array of key => value pairs for the first argument, to pass in an array of properties to set. |
126
|
|
|
* |
127
|
|
|
* All of these are equivelent, and any of these would set the event property "device_brand" to "HTC": |
128
|
|
|
* |
129
|
|
|
* <code> |
130
|
|
|
* $event->set('device_brand', 'HTC'); |
131
|
|
|
* $event->set('deviceBrand', 'HTC'); |
132
|
|
|
* // Object magic methods |
133
|
|
|
* $event->device_brand = 'HTC'; |
134
|
|
|
* $event->deviceBrand = 'HTC'; |
135
|
|
|
* // setting array |
136
|
|
|
* $event->set(['device_brand' => 'HTC']); |
137
|
|
|
* $event->set(['deviceBrand' => 'HTC']); |
138
|
|
|
* </code> |
139
|
|
|
* |
140
|
|
|
* All of the above are equivelent, use whatever is most appropriate for your project / situation. |
141
|
|
|
* |
142
|
|
|
* Note that only built-in event properties are normalized to match the built-in name. Custom properties that get |
143
|
|
|
* set in event_properties are not normalized. Meaning if you use a camelcase name, name with spaces in it, etc, |
144
|
|
|
* it will use that name as-is without attempting to normalize. |
145
|
|
|
* |
146
|
|
|
* @param string|array $name If array, will set key:value pairs |
147
|
|
|
* @param string $value Not used if first argument is an array |
148
|
|
|
* @return \Zumba\Amplitude\Event |
149
|
|
|
*/ |
150
|
|
|
public function set($name, $value = null) |
151
|
|
|
{ |
152
|
|
|
if (is_array($name)) { |
153
|
|
|
foreach ($name as $key => $val) { |
154
|
|
|
$this->set($key, $val); |
155
|
|
|
} |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
$name = $this->normalize($name); |
159
|
|
|
if (!isset($this->availableVars[$name])) { |
160
|
|
|
// treat it like an event_property |
161
|
|
|
$this->data['event_properties'][$name] = $value; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
switch ($this->availableVars[$name]) { |
166
|
|
|
case 'string': |
167
|
|
|
$value = (string)$value; |
168
|
|
|
break; |
169
|
|
|
case 'int': |
170
|
|
|
$value = (int)$value; |
171
|
|
|
break; |
172
|
|
|
case 'float': |
173
|
|
|
$value = (float)$value; |
174
|
|
|
break; |
175
|
|
|
case 'array': |
176
|
|
|
$value = (array)$value; |
177
|
|
|
break; |
178
|
|
|
} |
179
|
|
|
$this->data[$name] = $value; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets the event property, either from built-in event properties or the custom properties from event_properties. |
185
|
|
|
* |
186
|
|
|
* As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will |
187
|
|
|
* work. This is not the case for custom event properties however. |
188
|
|
|
* |
189
|
|
|
* For example, any of these calls will get the value of device_brand: |
190
|
|
|
* |
191
|
|
|
* <code> |
192
|
|
|
* $event->get('device_brand'); |
193
|
|
|
* $event->get('deviceBrand'); |
194
|
|
|
* // Magic methods work too: |
195
|
|
|
* $event->device_brand; |
196
|
|
|
* $event->deviceBrand; |
197
|
|
|
* </code> |
198
|
|
|
* |
199
|
|
|
* If no value found, returns null. |
200
|
|
|
* |
201
|
|
|
* @param string $name |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
|
|
public function get($name) |
205
|
|
|
{ |
206
|
|
|
$name = $this->normalize($name); |
207
|
|
|
if (isset($this->data[$name])) { |
208
|
|
|
return $this->data[$name]; |
209
|
|
|
} elseif (isset($this->data['event_properties'][$name])) { |
210
|
|
|
return $this->data['event_properties'][$name]; |
211
|
|
|
} |
212
|
|
|
return null; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Unset event property, either from built-in event properties or the custom properties from event_properties. |
217
|
|
|
* |
218
|
|
|
* As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will |
219
|
|
|
* work. This is not the case for custom event properties however. |
220
|
|
|
* |
221
|
|
|
* For example, any of these calls will unset the main built-in property device_brand: |
222
|
|
|
* |
223
|
|
|
* <code> |
224
|
|
|
* $event->unsetProperty('device_brand'); |
225
|
|
|
* $event->unsetProperty('deviceBrand'); |
226
|
|
|
* // Magic methods work too: |
227
|
|
|
* unset($event->device_brand); |
228
|
|
|
* unset($event->deviceBrand); |
229
|
|
|
* </code> |
230
|
|
|
* |
231
|
|
|
* @param string $name |
232
|
|
|
* @return \Zumba\Amplitude\Event |
233
|
|
|
*/ |
234
|
|
|
public function unsetProperty($name) |
235
|
|
|
{ |
236
|
|
|
$name = $this->normalize($name); |
237
|
|
|
if (isset($this->availableVars[$name])) { |
238
|
|
|
unset($this->data[$name]); |
239
|
|
|
} elseif (isset($this->data['event_properties'])) { |
240
|
|
|
unset($this->data['event_properties'][$name]); |
241
|
|
|
} |
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Check if event property is set, either from built-in event properties or custom properties from event_properties |
247
|
|
|
* |
248
|
|
|
* As with the set() method, for built-in event properties, can use camelcase OR underscore and either one will |
249
|
|
|
* work. This is not the case for custom event properties however. |
250
|
|
|
* |
251
|
|
|
* For example, any of these calls will check if the built in property for device_brand is set: |
252
|
|
|
* |
253
|
|
|
* <code> |
254
|
|
|
* $event->isPropertySet('device_brand'); |
255
|
|
|
* $event->isPropertySet('deviceBrand'); |
256
|
|
|
* // Magic methods work too: |
257
|
|
|
* isset($event->device_brand); |
258
|
|
|
* isset($event->deviceBrand); |
259
|
|
|
* </code> |
260
|
|
|
* |
261
|
|
|
* @param string $name |
262
|
|
|
* @return \Zumba\Amplitude\Event |
263
|
|
|
*/ |
264
|
|
|
public function isPropertySet($name) |
265
|
|
|
{ |
266
|
|
|
$name = $this->normalize($name); |
267
|
|
|
return isset($this->data[$name]) || isset($this->data['event_properties'][$name]); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Magic method to set the value. |
272
|
|
|
* |
273
|
|
|
* See the set() method. |
274
|
|
|
* |
275
|
|
|
* @param string $name |
276
|
|
|
* @param string $value |
277
|
|
|
* @return void |
278
|
|
|
*/ |
279
|
|
|
public function __set($name, $value) |
280
|
|
|
{ |
281
|
|
|
$this->set($name, $value); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Magic method to get the value |
286
|
|
|
* |
287
|
|
|
* See the get() method |
288
|
|
|
* |
289
|
|
|
* @param string $name |
290
|
|
|
* @return mixed |
291
|
|
|
*/ |
292
|
|
|
public function __get($name) |
293
|
|
|
{ |
294
|
|
|
return $this->get($name); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Unset event property |
299
|
|
|
* |
300
|
|
|
* See the unsetProperty() method |
301
|
|
|
* |
302
|
|
|
* @param string $name |
303
|
|
|
*/ |
304
|
|
|
public function __unset($name) |
305
|
|
|
{ |
306
|
|
|
$this->unsetProperty($name); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Magic method to see if name is set |
311
|
|
|
* |
312
|
|
|
* Uses same normalization on the name as the set method, where it will match built-in properties for either |
313
|
|
|
* camelcased or underscore version of property |
314
|
|
|
* |
315
|
|
|
* @param string $name |
316
|
|
|
* @return boolean |
317
|
|
|
*/ |
318
|
|
|
public function __isset($name) |
319
|
|
|
{ |
320
|
|
|
return $this->isPropertySet($name); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Normalized the name, by attempting to camelcase / underscore it to see if it matches any built-in property names. |
325
|
|
|
* |
326
|
|
|
* If it matches a built-in property name, will return the normalized property name. Otherwise returns the name |
327
|
|
|
* un-modified. |
328
|
|
|
* |
329
|
|
|
* @param string $name |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
protected function normalize($name) |
333
|
|
|
{ |
334
|
|
|
if (isset($this->availableVars[$name])) { |
335
|
|
|
return $name; |
336
|
|
|
} |
337
|
|
|
if (preg_match('/^[a-zA-Z_]+$/', $name)) { |
338
|
|
|
// No spaces or unexpected vars, this could be camelcased version or underscore version of a built-in |
339
|
|
|
// var name, check to see if it matches |
340
|
|
|
$underscore = Inflector::underscore($name); |
341
|
|
|
if (isset($this->availableVars[$underscore])) { |
342
|
|
|
return $underscore; |
343
|
|
|
} |
344
|
|
|
// In case it is one of the camel-cased versions |
345
|
|
|
$camel = Inflector::camelCase($name); |
346
|
|
|
if (isset($this->availableVars[$camel])) { |
347
|
|
|
return $camel; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
// Could not find name, just use original un-altered, probably used in event_properties |
351
|
|
|
return $name; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Convert the event to array format |
356
|
|
|
* |
357
|
|
|
* @return array |
358
|
|
|
*/ |
359
|
|
|
public function toArray() |
360
|
|
|
{ |
361
|
|
|
return $this->data; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* JSON serialize |
366
|
|
|
* |
367
|
|
|
* @return array |
368
|
|
|
*/ |
369
|
|
|
public function jsonSerialize() |
370
|
|
|
{ |
371
|
|
|
return $this->data; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|