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