PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.2
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 / Tracking.php

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

223 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Dependencies\WPMedia\Mixpanel;
5
6 use Imagify_WPMedia_Mixpanel as WPMedia_Mixpanel;
7
8 class Tracking {
9 const HOST = 'api-eu.mixpanel.com';
10
11 const LIB_URL = 'https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js';
12
13 /**
14 * Mixpanel instance
15 *
16 * @var WPMedia_Mixpanel
17 */
18 private $mixpanel;
19
20 /**
21 * Mixpanel token
22 *
23 * @var string
24 */
25 protected $token;
26
27 /**
28 * Constructor
29 *
30 * @param string $mixpanel_token Mixpanel token.
31 * @param mixed[] $options Options for Mixpanel instance.
32 */
33 public function __construct( string $mixpanel_token, array $options = [] ) {
34 $mixpanel_options = array_merge(
35 [
36 'host' => self::HOST,
37 'events_endpoint' => '/track/?ip=0',
38 ],
39 $options
40 );
41
42 $this->token = $mixpanel_token;
43 $this->mixpanel = WPMedia_Mixpanel::getInstance(
44 $this->token,
45 $mixpanel_options
46 );
47 }
48
49 /**
50 * Check if debug mode is enabled
51 *
52 * @return bool
53 */
54 private function is_debug(): bool {
55 $debug = ( defined( 'WP_DEBUG' ) ? constant( 'WP_DEBUG' ) : false )
56 && ( defined( 'WP_DEBUG_LOG' ) ? constant( 'WP_DEBUG_LOG' ) : false );
57
58 /**
59 * Filters whether Mixpanel debug mode is enabled.
60 *
61 * @param bool $debug Debug mode value.
62 */
63 return apply_filters( 'wp_media_mixpanel_debug', $debug );
64 }
65
66 /**
67 * Log event to error log if debug mode is enabled
68 *
69 * @param string $event Event name.
70 * @param mixed[] $properties Event properties.
71 */
72 private function log_event( string $event, array $properties ): void {
73 if ( ! $this->is_debug() ) {
74 return;
75 }
76
77 error_log( 'Mixpanel event: ' . $event . ' ' . var_export( $properties, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_var_export
78 }
79
80 /**
81 * Track an event in Mixpanel
82 *
83 * @param string $event Event name.
84 * @param mixed[] $properties Event properties.
85 */
86 public function track( string $event, array $properties ): void {
87 $this->log_event( $event, $properties );
88
89 $this->mixpanel->track( $event, $properties );
90 }
91
92 /**
93 * Identify a user in Mixpanel
94 *
95 * @param string $user_id User ID.
96 *
97 * @return void
98 */
99 public function identify( string $user_id ): void {
100 $this->mixpanel->identify( $this->hash( $user_id ) );
101 }
102
103 /**
104 * Set a user property in Mixpanel
105 *
106 * @param string $user_id User ID.
107 * @param string $property Property name.
108 * @param mixed $value Property value.
109 */
110 public function set_user_property( string $user_id, string $property, $value ): void {
111 $this->mixpanel->people->set(
112 $user_id,
113 [
114 $property => $value,
115 ],
116 '0'
117 );
118 }
119
120 /**
121 * Hash a value using sha224
122 *
123 * @param string $value Value to hash.
124 *
125 * @return string
126 */
127 public function hash( string $value ): string {
128 return hash( 'sha224', $value );
129 }
130
131 /**
132 * Get the WordPress version
133 *
134 * @return string
135 */
136 public function get_wp_version(): string {
137 $version = preg_replace( '@^(\d\.\d+).*@', '\1', get_bloginfo( 'version' ) );
138
139 if ( null === $version ) {
140 $version = '0.0';
141 }
142
143 return $version;
144 }
145
146 /**
147 * Get the PHP version
148 *
149 * @return string
150 */
151 public function get_php_version(): string {
152 $version = preg_replace( '@^(\d\.\d+).*@', '\1', phpversion() );
153
154 if ( null === $version ) {
155 $version = '0.0';
156 }
157
158 return $version;
159 }
160
161 /**
162 * Get the active theme
163 *
164 * @return string
165 */
166 public function get_current_theme(): string {
167 $theme = wp_get_theme();
168
169 return $theme->get( 'Name' );
170 }
171
172 /**
173 * Get list of active plugins names
174 *
175 * @return string[]
176 */
177 public function get_active_plugins(): array {
178 $plugins = [];
179 $active_plugins = (array) get_option( 'active_plugins', [] );
180 $all_plugins = get_plugins();
181
182 foreach ( $active_plugins as $plugin_path ) {
183 if ( ! is_string( $plugin_path ) ) {
184 continue;
185 }
186
187 if ( ! isset( $all_plugins[ $plugin_path ] ) ) {
188 continue;
189 }
190
191 $plugins[] = $all_plugins[ $plugin_path ]['Name'];
192 }
193
194 return $plugins;
195 }
196
197 /**
198 * Get the Mixpanel token
199 *
200 * @return string
201 */
202 public function get_token(): string {
203 return $this->token;
204 }
205
206 /**
207 * Add the Mixpanel script & initialize it
208 */
209 public function add_script(): void {
210 ?>
211 <!-- start Mixpanel --><script>
212 const MIXPANEL_CUSTOM_LIB_URL = "<?php echo esc_js( self::LIB_URL ); ?>";
213 (function (f, b) { if (!b.__SV) { var e, g, i, h; window.mixpanel = b; b._i = []; b.init = function (e, f, c) { function g(a, d) { var b = d.split("."); 2 == b.length && ((a = a[b[0]]), (d = b[1])); a[d] = function () { a.push([d].concat(Array.prototype.slice.call(arguments, 0))); }; } var a = b; "undefined" !== typeof c ? (a = b[c] = []) : (c = "mixpanel"); a.people = a.people || []; a.toString = function (a) { var d = "mixpanel"; "mixpanel" !== c && (d += "." + c); a || (d += " (stub)"); return d; }; a.people.toString = function () { return a.toString(1) + ".people (stub)"; }; i = "disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking start_batch_senders people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove".split( " "); for (h = 0; h < i.length; h++) g(a, i[h]); var j = "set set_once union unset remove delete".split(" "); a.get_group = function () { function b(c) { d[c] = function () { call2_args = arguments; call2 = [c].concat(Array.prototype.slice.call(call2_args, 0)); a.push([e, call2]); }; } for ( var d = {}, e = ["get_group"].concat( Array.prototype.slice.call(arguments, 0)), c = 0; c < j.length; c++) b(j[c]); return d; }; b._i.push([e, f, c]); }; b.__SV = 1.2; e = f.createElement("script"); e.type = "text/javascript"; e.async = !0; e.src = "undefined" !== typeof MIXPANEL_CUSTOM_LIB_URL ? MIXPANEL_CUSTOM_LIB_URL : "file:" === f.location.protocol && "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//) ? "https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js" : "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js"; g = f.getElementsByTagName("script")[0]; g.parentNode.insertBefore(e, g); } })(document, window.mixpanel || []);
214 mixpanel.init( '<?php echo esc_js( $this->token ); ?>', {
215 api_host: "https://<?php echo esc_js( self::HOST ); ?>",
216 id: false,
217 property_blacklist: ['$initial_referrer', '$current_url', '$initial_referring_domain', '$referrer', '$referring_domain']
218 } );
219 </script><!-- end Mixpanel -->
220 <?php
221 }
222 }
223