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 / Producers / MixpanelEvents.php

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

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