MixpanelBaseProducer.php
2 years ago
MixpanelEvents.php
2 years ago
MixpanelGroups.php
2 years ago
MixpanelPeople.php
2 years ago
index.php
2 years ago
MixpanelEvents.php
90 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | require_once(dirname(__FILE__) . "/MixpanelBaseProducer.php"); |
| 4 | require_once(dirname(__FILE__) . "/MixpanelPeople.php"); |
| 5 | require_once(dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php"); |
| 6 | class Producers_MixpanelEvents extends Producers_MixpanelBaseProducer { |
| 7 | private $_super_properties = array("mp_lib" => "php"); |
| 8 | public function track($event, $properties = array()) { |
| 9 | // if no token is passed in, use current token |
| 10 | if (!isset($properties["token"])) $properties['token'] = $this->_token; |
| 11 | // if no time is passed in, use the current time |
| 12 | if (!isset($properties["time"])) $properties['time'] = microtime(true); |
| 13 | $params['event'] = $event; |
| 14 | $params['properties'] = array_merge($this->_super_properties, $properties); |
| 15 | $this->enqueue($params); |
| 16 | } |
| 17 | public function register($property, $value) { |
| 18 | $this->_super_properties[$property] = $value; |
| 19 | } |
| 20 | public function registerAll($props_and_vals = array()) { |
| 21 | foreach($props_and_vals as $property => $value) { |
| 22 | $this->register($property, $value); |
| 23 | } |
| 24 | } |
| 25 | public function registerOnce($property, $value) { |
| 26 | if (!isset($this->_super_properties[$property])) { |
| 27 | $this->register($property, $value); |
| 28 | } |
| 29 | } |
| 30 | public function registerAllOnce($props_and_vals = array()) { |
| 31 | foreach($props_and_vals as $property => $value) { |
| 32 | if (!isset($this->_super_properties[$property])) { |
| 33 | $this->register($property, $value); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | public function unregister($property) { |
| 38 | unset($this->_super_properties[$property]); |
| 39 | } |
| 40 | public function unregisterAll($properties) { |
| 41 | foreach($properties as $property) { |
| 42 | $this->unregister($property); |
| 43 | } |
| 44 | } |
| 45 | public function getProperty($property) { |
| 46 | return $this->_super_properties[$property]; |
| 47 | } |
| 48 | public function identify($user_id, $anon_id = null) { |
| 49 | $this->register("distinct_id", $user_id); |
| 50 | $UUIDv4 = '/^(\$device:)?[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*$/i'; |
| 51 | if (!empty($anon_id)) { |
| 52 | if (preg_match($UUIDv4, $anon_id) !== 1) { |
| 53 | error_log("Running Identify method (identified_id: $user_id, anon_id: $anon_id) failed, anon_id not in UUID v4 format"); |
| 54 | } else { |
| 55 | $this->track('$identify', array( |
| 56 | '$identified_id' => $user_id, |
| 57 | '$anon_id' => $anon_id |
| 58 | )); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | public function createAlias($distinct_id, $alias) { |
| 63 | $msg = array( |
| 64 | "event" => '$create_alias', |
| 65 | "properties" => array("distinct_id" => $distinct_id, "alias" => $alias, "token" => $this->_token) |
| 66 | ); |
| 67 | // Save the current fork/async options |
| 68 | $old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : false; |
| 69 | $old_async = isset($this->_options['async']) ? $this->_options['async'] : false; |
| 70 | // Override fork/async to make the new consumer synchronous |
| 71 | $this->_options['fork'] = false; |
| 72 | $this->_options['async'] = false; |
| 73 | // The name is ambiguous, but this creates a new consumer with current $this->_options |
| 74 | $consumer = $this->_getConsumer(); |
| 75 | $success = $consumer->persist(array($msg)); |
| 76 | // Restore the original fork/async settings |
| 77 | $this->_options['fork'] = $old_fork; |
| 78 | $this->_options['async'] = $old_async; |
| 79 | if (!$success) { |
| 80 | error_log("Creating Mixpanel Alias (distinct id: $distinct_id, alias: $alias) failed"); |
| 81 | throw new Exception("Tried to create an alias but the call was not successful"); |
| 82 | } else { |
| 83 | return $msg; |
| 84 | } |
| 85 | } |
| 86 | function _getEndpoint() { |
| 87 | return $this->_options['events_endpoint']; |
| 88 | } |
| 89 | } |
| 90 |