1 | <?php |
||
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 = []) |
||
102 | |||
103 | /** |
||
104 | * Set the user properties on the event |
||
105 | * |
||
106 | * @param array $userProperties |
||
107 | */ |
||
108 | public function setUserProperties(array $userProperties) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
296 | |||
297 | /** |
||
298 | * Unset event property |
||
299 | * |
||
300 | * See the unsetProperty() method |
||
301 | * |
||
302 | * @param string $name |
||
303 | */ |
||
304 | public function __unset($name) |
||
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) |
||
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) |
||
353 | |||
354 | /** |
||
355 | * Convert the event to array format |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | public function toArray() |
||
363 | |||
364 | /** |
||
365 | * JSON serialize |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public function jsonSerialize() |
||
373 | } |
||
374 |