PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / wp-media / wp-mixpanel / src / Classes / Mixpanel.php

Mixpanel.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at vendor/wp-media/wp-mixpanel/src/Classes/Mixpanel.php

309 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This is the main class for the Mixpanel PHP Library which provides all of the methods you need to track events,
5 * create/update profiles and group profiles.
6 *
7 * Architecture
8 * -------------
9 *
10 * This library is built such that all messages are buffered in an in-memory "queue"
11 * The queue will be automatically flushed at the end of every request. Alternatively, you can call "flush()" manually
12 * at any time. Flushed messages will be passed to a Consumer's "persist" method. The library comes with a handful of
13 * Consumers. The "CurlConsumer" is used by default which will send the messages to Mixpanel using forked cURL processes.
14 * You can implement your own custom Consumer to customize how a message is sent to Mixpanel. This can be useful when
15 * you want to put messages onto a distributed queue (such as ActiveMQ or Kestrel) instead of writing to Mixpanel in
16 * the user thread.
17 *
18 * Options
19 * -------------
20 *
21 * <table width="100%" cellpadding="5">
22 * <tr>
23 * <th>Option</th>
24 * <th>Description</th>
25 * <th>Default</th>
26 * </tr>
27 * <tr>
28 * <td>max_queue_size</td>
29 * <td>The maximum number of items to buffer in memory before flushing</td>
30 * <td>1000</td>
31 * </tr>
32 * <tr>
33 * <td>debug</td>
34 * <td>Enable/disable debug mode</td>
35 * <td>false</td>
36 * </tr>
37 * <tr>
38 * <td>consumer</td>
39 * <td>The consumer to use for writing messages</td>
40 * <td>curl</td>
41 * </tr>
42 * <tr>
43 * <td>consumers</td>
44 * <td>An array of custom consumers in the format array(consumer_key => class_name)</td>
45 * <td>null</td>
46 * </tr>
47 * <tr>
48 * <td>host</td>
49 * <td>The host name for api calls (used by some consumers)</td>
50 * <td>api.mixpanel.com</td>
51 * </tr>
52 * <tr>
53 * <td>events_endpoint</td>
54 * <td>The endpoint for tracking events (relative to the host)</td>
55 * <td>/events</td>
56 * </tr>
57 * <tr>
58 * <td>people_endpoint</td>
59 * <td>The endpoint for making people updates (relative to the host)</td>
60 * <td>/engage</td>
61 * </tr>
62 * <tr>
63 * <td>use_ssl</td>
64 * <td>Tell the consumer whether or not to use ssl (when available)</td>
65 * <td>true</td>
66 * </tr>
67 * <tr>
68 * <td>error_callback</td>
69 * <td>The name of a function to be called on consumption failures</td>
70 * <td>null</td>
71 * </tr>
72 * <tr>
73 * <td>connect_timeout</td>
74 * <td>In both the SocketConsumer and CurlConsumer, this is used for the connection timeout (i.e. How long it has take to actually make a connection).
75 * <td>5</td>
76 * </tr>
77 * <tr>
78 * <td>timeout</td>
79 * <td>In the CurlConsumer (non-forked), it is used to determine how long the cURL call has to execute.
80 * <td>30</td>
81 * </tr>
82 * </table>
83 *
84 * Example: Tracking an Event
85 * -------------
86 *
87 * $mp = Mixpanel::getInstance("MY_TOKEN");
88 *
89 * $mp->track("My Event");
90 *
91 * Example: Setting Profile Properties
92 * -------------
93 *
94 * $mp = Mixpanel::getInstance("MY_TOKEN", array("use_ssl" => false));
95 *
96 * $mp->people->set(12345, array(
97 * '$first_name' => "John",
98 * '$last_name' => "Doe",
99 * '$email' => "john.doe@example.com",
100 * '$phone' => "5555555555",
101 * 'Favorite Color' => "red"
102 * ));
103 *
104 */
105 class Imagify_WPMedia_Mixpanel extends Imagify_WPMedia_Base_MixpanelBase {
106
107
108 /**
109 * An instance of the MixpanelPeople class (used to create/update profiles)
110 * @var Imagify_WPMedia_Producers_MixpanelPeople
111 */
112 public $people;
113
114
115 /**
116 * An instance of the MixpanelEvents class
117 * @var Imagify_WPMedia_Producers_MixpanelEvents
118 */
119 private $_events;
120
121 /**
122 * An instance of the MixpanelGroups class (used to create/update group profiles)
123 * @var Imagify_WPMedia_Producers_MixpanelPeople
124 */
125 public $group;
126
127
128
129 /**
130 * Instances' list of the Mixpanel class (for singleton use, splitted by token)
131 * @var Imagify_WPMedia_Mixpanel[]
132 */
133 private static $_instances = array();
134
135
136 /**
137 * Instantiates a new Mixpanel instance.
138 * @param $token
139 * @param array $options
140 */
141 public function __construct($token, $options = array()) {
142 parent::__construct($options);
143 $this->people = new Imagify_WPMedia_Producers_MixpanelPeople($token, $options);
144 $this->_events = new Imagify_WPMedia_Producers_MixpanelEvents($token, $options);
145 $this->group = new Imagify_WPMedia_Producers_MixpanelGroups($token, $options);
146 }
147
148
149 /**
150 * Returns a singleton instance of Mixpanel
151 * @param $token
152 * @param array $options
153 * @return Imagify_WPMedia_Mixpanel
154 */
155 public static function getInstance($token, $options = array()) {
156 if(!isset(self::$_instances[$token])) {
157 self::$_instances[$token] = new Imagify_WPMedia_Mixpanel($token, $options);
158 }
159 return self::$_instances[$token];
160 }
161
162
163 /**
164 * Add an array representing a message to be sent to Mixpanel to the in-memory queue.
165 * @param array $message
166 */
167 public function enqueue($message = array()) {
168 $this->_events->enqueue($message);
169 }
170
171
172 /**
173 * Add an array representing a list of messages to be sent to Mixpanel to a queue.
174 * @param array $messages
175 */
176 public function enqueueAll($messages = array()) {
177 $this->_events->enqueueAll($messages);
178 }
179
180
181 /**
182 * Flush the events queue
183 * @param int $desired_batch_size
184 */
185 public function flush($desired_batch_size = 50) {
186 $this->_events->flush($desired_batch_size);
187 }
188
189
190 /**
191 * Empty the events queue
192 */
193 public function reset() {
194 $this->_events->reset();
195 }
196
197
198 /**
199 * Identify the user you want to associate to tracked events. The $anon_id must be UUID v4 format and not already merged to an $identified_id.
200 * All identify calls with a new and valid $anon_id will trigger a track $identify event, and merge to the $identified_id.
201 * @param string|int $user_id
202 * @param string|int $anon_id [optional]
203 */
204 public function identify($user_id, $anon_id = null) {
205 $this->_events->identify($user_id, $anon_id);
206 }
207
208 /**
209 * Track an event defined by $event associated with metadata defined by $properties
210 * @param string $event
211 * @param array $properties
212 */
213 public function track($event, $properties = array()) {
214 $this->_events->track($event, $properties);
215 }
216
217
218 /**
219 * Register a property to be sent with every event.
220 *
221 * If the property has already been registered, it will be
222 * overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.
223 * @param string $property
224 * @param mixed $value
225 */
226 public function register($property, $value) {
227 $this->_events->register($property, $value);
228 }
229
230
231 /**
232 * Register multiple properties to be sent with every event.
233 *
234 * If any of the properties have already been registered,
235 * they will be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class
236 * instance.
237 * @param array $props_and_vals
238 */
239 public function registerAll($props_and_vals = array()) {
240 $this->_events->registerAll($props_and_vals);
241 }
242
243
244 /**
245 * Register a property to be sent with every event.
246 *
247 * If the property has already been registered, it will NOT be
248 * overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class instance.
249 * @param $property
250 * @param $value
251 */
252 public function registerOnce($property, $value) {
253 $this->_events->registerOnce($property, $value);
254 }
255
256
257 /**
258 * Register multiple properties to be sent with every event.
259 *
260 * If any of the properties have already been registered,
261 * they will NOT be overwritten. NOTE: Registered properties are only persisted for the life of the Mixpanel class
262 * instance.
263 * @param array $props_and_vals
264 */
265 public function registerAllOnce($props_and_vals = array()) {
266 $this->_events->registerAllOnce($props_and_vals);
267 }
268
269
270 /**
271 * Un-register an property to be sent with every event.
272 * @param string $property
273 */
274 public function unregister($property) {
275 $this->_events->unregister($property);
276 }
277
278
279 /**
280 * Un-register a list of properties to be sent with every event.
281 * @param array $properties
282 */
283 public function unregisterAll($properties) {
284 $this->_events->unregisterAll($properties);
285 }
286
287
288 /**
289 * Get a property that is set to be sent with every event
290 * @param string $property
291 * @return mixed
292 */
293 public function getProperty($property)
294 {
295 return $this->_events->getProperty($property);
296 }
297
298
299 /**
300 * An alias to be merged with the distinct_id. Each alias can only map to one distinct_id.
301 * This is helpful when you want to associate a generated id (such as a session id) to a user id or username.
302 * @param string|int $distinct_id
303 * @param string|int $alias
304 */
305 public function createAlias($distinct_id, $alias) {
306 $this->_events->createAlias($distinct_id, $alias);
307 }
308 }
309