| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorDeps; |
| 4 |
|
| 5 |
require_once \dirname(__FILE__) . "/MixpanelBaseProducer.php"; |
| 6 |
require_once \dirname(__FILE__) . "/MixpanelPeople.php"; |
| 7 |
require_once \dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php"; |
| 8 |
/** |
| 9 |
* Provides an API to track events on Mixpanel |
| 10 |
*/ |
| 11 |
class Producers_MixpanelEvents extends Producers_MixpanelBaseProducer |
| 12 |
{ |
| 13 |
/** |
| 14 |
* An array of properties to attach to every tracked event |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
private $_super_properties = array("mp_lib" => "php"); |
| 18 |
/** |
| 19 |
* Track an event defined by $event associated with metadata defined by $properties |
| 20 |
* @param string $event |
| 21 |
* @param array $properties |
| 22 |
*/ |
| 23 |
public function track($event, $properties = array()) |
| 24 |
{ |
| 25 |
// if no token is passed in, use current token |
| 26 |
if (!isset($properties["token"])) { |
| 27 |
$properties['token'] = $this->_token; |
| 28 |
} |
| 29 |
// if no time is passed in, use the current time |
| 30 |
if (!isset($properties["time"])) { |
| 31 |
$properties['time'] = \microtime(\true); |
| 32 |
} |
| 33 |
$params['event'] = $event; |
| 34 |
$params['properties'] = \array_merge($this->_super_properties, $properties); |
| 35 |
$this->enqueue($params); |
| 36 |
} |
| 37 |
/** |
| 38 |
* Register a property to be sent with every event. If the property has already been registered, it will be |
| 39 |
* overwritten. |
| 40 |
* @param string $property |
| 41 |
* @param mixed $value |
| 42 |
*/ |
| 43 |
public function register($property, $value) |
| 44 |
{ |
| 45 |
$this->_super_properties[$property] = $value; |
| 46 |
} |
| 47 |
/** |
| 48 |
* Register multiple properties to be sent with every event. If any of the properties have already been registered, |
| 49 |
* they will be overwritten. |
| 50 |
* @param array $props_and_vals |
| 51 |
*/ |
| 52 |
public function registerAll($props_and_vals = array()) |
| 53 |
{ |
| 54 |
foreach ($props_and_vals as $property => $value) { |
| 55 |
$this->register($property, $value); |
| 56 |
} |
| 57 |
} |
| 58 |
/** |
| 59 |
* Register a property to be sent with every event. If the property has already been registered, it will NOT be |
| 60 |
* overwritten. |
| 61 |
* @param $property |
| 62 |
* @param $value |
| 63 |
*/ |
| 64 |
public function registerOnce($property, $value) |
| 65 |
{ |
| 66 |
if (!isset($this->_super_properties[$property])) { |
| 67 |
$this->register($property, $value); |
| 68 |
} |
| 69 |
} |
| 70 |
/** |
| 71 |
* Register multiple properties to be sent with every event. If any of the properties have already been registered, |
| 72 |
* they will NOT be overwritten. |
| 73 |
* @param array $props_and_vals |
| 74 |
*/ |
| 75 |
public function registerAllOnce($props_and_vals = array()) |
| 76 |
{ |
| 77 |
foreach ($props_and_vals as $property => $value) { |
| 78 |
if (!isset($this->_super_properties[$property])) { |
| 79 |
$this->register($property, $value); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
/** |
| 84 |
* Un-register an property to be sent with every event. |
| 85 |
* @param string $property |
| 86 |
*/ |
| 87 |
public function unregister($property) |
| 88 |
{ |
| 89 |
unset($this->_super_properties[$property]); |
| 90 |
} |
| 91 |
/** |
| 92 |
* Un-register a list of properties to be sent with every event. |
| 93 |
* @param array $properties |
| 94 |
*/ |
| 95 |
public function unregisterAll($properties) |
| 96 |
{ |
| 97 |
foreach ($properties as $property) { |
| 98 |
$this->unregister($property); |
| 99 |
} |
| 100 |
} |
| 101 |
/** |
| 102 |
* Get a property that is set to be sent with every event |
| 103 |
* @param string $property |
| 104 |
* @return mixed |
| 105 |
*/ |
| 106 |
public function getProperty($property) |
| 107 |
{ |
| 108 |
return $this->_super_properties[$property]; |
| 109 |
} |
| 110 |
/** |
| 111 |
* Identify the user you want to associate to tracked events. The $anon_id must be UUID v4 format (optionally with the |
| 112 |
* prefix '$device:') and not already merged to an $identified_id. All identify calls with a new and valid $anon_id will |
| 113 |
* trigger a track $identify event, and merge to the $identified_id. |
| 114 |
* @param string|int $user_id |
| 115 |
* @param string|int $anon_id [optional] |
| 116 |
*/ |
| 117 |
public function identify($user_id, $anon_id = null) |
| 118 |
{ |
| 119 |
$this->register("distinct_id", $user_id); |
| 120 |
$UUIDv4 = '/^(\\$device:)?[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*$/i'; |
| 121 |
if (!empty($anon_id)) { |
| 122 |
if (\preg_match($UUIDv4, $anon_id) !== 1) { |
| 123 |
/* not a valid uuid */ |
| 124 |
\error_log("Running Identify method (identified_id: {$user_id}, anon_id: {$anon_id}) failed, anon_id not in UUID v4 format"); |
| 125 |
} else { |
| 126 |
$this->track('$identify', array('$identified_id' => $user_id, '$anon_id' => $anon_id)); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
/** |
| 131 |
* An alias to be merged with the distinct_id. Each alias can only map to one distinct_id. |
| 132 |
* This is helpful when you want to associate a generated id (such as a session id) to a user id or username. |
| 133 |
* |
| 134 |
* Because aliasing can be extremely vulnerable to race conditions and ordering issues, we'll make a synchronous |
| 135 |
* call directly to Mixpanel when this method is called. If it fails we'll throw an Exception as subsequent |
| 136 |
* events are likely to be incorrectly tracked. |
| 137 |
* @param string|int $distinct_id |
| 138 |
* @param string|int $alias |
| 139 |
* @return array $msg |
| 140 |
* @throws Exception |
| 141 |
*/ |
| 142 |
public function createAlias($distinct_id, $alias) |
| 143 |
{ |
| 144 |
$msg = array("event" => '$create_alias', "properties" => array("distinct_id" => $distinct_id, "alias" => $alias, "token" => $this->_token)); |
| 145 |
// Save the current fork/async options |
| 146 |
$old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : \false; |
| 147 |
$old_async = isset($this->_options['async']) ? $this->_options['async'] : \false; |
| 148 |
// Override fork/async to make the new consumer synchronous |
| 149 |
$this->_options['fork'] = \false; |
| 150 |
$this->_options['async'] = \false; |
| 151 |
// The name is ambiguous, but this creates a new consumer with current $this->_options |
| 152 |
$consumer = $this->_getConsumer(); |
| 153 |
$success = $consumer->persist(array($msg)); |
| 154 |
// Restore the original fork/async settings |
| 155 |
$this->_options['fork'] = $old_fork; |
| 156 |
$this->_options['async'] = $old_async; |
| 157 |
if (!$success) { |
| 158 |
\error_log("Creating Mixpanel Alias (distinct id: {$distinct_id}, alias: {$alias}) failed"); |
| 159 |
throw new \Exception("Tried to create an alias but the call was not successful"); |
| 160 |
} else { |
| 161 |
return $msg; |
| 162 |
} |
| 163 |
} |
| 164 |
/** |
| 165 |
* Returns the "events" endpoint |
| 166 |
* @return string |
| 167 |
*/ |
| 168 |
function _getEndpoint() |
| 169 |
{ |
| 170 |
return $this->_options['events_endpoint']; |
| 171 |
} |
| 172 |
} |
| 173 |
|