Classes
10 months ago
Optin.php
9 months ago
Tracking.php
8 months ago
TrackingPlugin.php
7 months ago
WPConsumer.php
10 months ago
Tracking.php
221 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace WPMedia\Mixpanel; |
| 5 | |
| 6 | use WPMedia_Mixpanel; |
| 7 | |
| 8 | class Tracking { |
| 9 | const HOST = 'mixpanel-tracking-proxy-prod.public-default.live2-k8s-cph3.ingress.k8s.g1i.one'; |
| 10 | |
| 11 | /** |
| 12 | * Mixpanel instance |
| 13 | * |
| 14 | * @var WPMedia_Mixpanel |
| 15 | */ |
| 16 | private $mixpanel; |
| 17 | |
| 18 | /** |
| 19 | * Mixpanel token |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $token; |
| 24 | |
| 25 | /** |
| 26 | * Constructor |
| 27 | * |
| 28 | * @param string $mixpanel_token Mixpanel token. |
| 29 | * @param mixed[] $options Options for Mixpanel instance. |
| 30 | */ |
| 31 | public function __construct( string $mixpanel_token, array $options = [] ) { |
| 32 | $mixpanel_options = array_merge( |
| 33 | [ |
| 34 | 'host' => self::HOST, |
| 35 | 'events_endpoint' => '/track/?ip=0', |
| 36 | ], |
| 37 | $options |
| 38 | ); |
| 39 | |
| 40 | $this->token = $mixpanel_token; |
| 41 | $this->mixpanel = WPMedia_Mixpanel::getInstance( |
| 42 | $this->token, |
| 43 | $mixpanel_options |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Check if debug mode is enabled |
| 49 | * |
| 50 | * @return bool |
| 51 | */ |
| 52 | private function is_debug(): bool { |
| 53 | $debug = ( defined( 'WP_DEBUG' ) ? constant( 'WP_DEBUG' ) : false ) |
| 54 | && ( defined( 'WP_DEBUG_LOG' ) ? constant( 'WP_DEBUG_LOG' ) : false ); |
| 55 | |
| 56 | /** |
| 57 | * Filters whether Mixpanel debug mode is enabled. |
| 58 | * |
| 59 | * @param bool $debug Debug mode value. |
| 60 | */ |
| 61 | return apply_filters( 'wp_media_mixpanel_debug', $debug ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Log event to error log if debug mode is enabled |
| 66 | * |
| 67 | * @param string $event Event name. |
| 68 | * @param mixed[] $properties Event properties. |
| 69 | */ |
| 70 | private function log_event( string $event, array $properties ): void { |
| 71 | if ( ! $this->is_debug() ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | 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 |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Track an event in Mixpanel |
| 80 | * |
| 81 | * @param string $event Event name. |
| 82 | * @param mixed[] $properties Event properties. |
| 83 | */ |
| 84 | public function track( string $event, array $properties ): void { |
| 85 | $this->log_event( $event, $properties ); |
| 86 | |
| 87 | $this->mixpanel->track( $event, $properties ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Identify a user in Mixpanel |
| 92 | * |
| 93 | * @param string $user_id User ID. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function identify( string $user_id ): void { |
| 98 | $this->mixpanel->identify( $this->hash( $user_id ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set a user property in Mixpanel |
| 103 | * |
| 104 | * @param string $user_id User ID. |
| 105 | * @param string $property Property name. |
| 106 | * @param mixed $value Property value. |
| 107 | */ |
| 108 | public function set_user_property( string $user_id, string $property, $value ): void { |
| 109 | $this->mixpanel->people->set( |
| 110 | $user_id, |
| 111 | [ |
| 112 | $property => $value, |
| 113 | ], |
| 114 | '0' |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Hash a value using sha224 |
| 120 | * |
| 121 | * @param string $value Value to hash. |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public function hash( string $value ): string { |
| 126 | return hash( 'sha224', $value ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the WordPress version |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | public function get_wp_version(): string { |
| 135 | $version = preg_replace( '@^(\d\.\d+).*@', '\1', get_bloginfo( 'version' ) ); |
| 136 | |
| 137 | if ( null === $version ) { |
| 138 | $version = '0.0'; |
| 139 | } |
| 140 | |
| 141 | return $version; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the PHP version |
| 146 | * |
| 147 | * @return string |
| 148 | */ |
| 149 | public function get_php_version(): string { |
| 150 | $version = preg_replace( '@^(\d\.\d+).*@', '\1', phpversion() ); |
| 151 | |
| 152 | if ( null === $version ) { |
| 153 | $version = '0.0'; |
| 154 | } |
| 155 | |
| 156 | return $version; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the active theme |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | public function get_current_theme(): string { |
| 165 | $theme = wp_get_theme(); |
| 166 | |
| 167 | return $theme->get( 'Name' ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get list of active plugins names |
| 172 | * |
| 173 | * @return string[] |
| 174 | */ |
| 175 | public function get_active_plugins(): array { |
| 176 | $plugins = []; |
| 177 | $active_plugins = (array) get_option( 'active_plugins', [] ); |
| 178 | $all_plugins = get_plugins(); |
| 179 | |
| 180 | foreach ( $active_plugins as $plugin_path ) { |
| 181 | if ( ! is_string( $plugin_path ) ) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | if ( ! isset( $all_plugins[ $plugin_path ] ) ) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | $plugins[] = $all_plugins[ $plugin_path ]['Name']; |
| 190 | } |
| 191 | |
| 192 | return $plugins; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get the Mixpanel token |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | public function get_token(): string { |
| 201 | return $this->token; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Add the Mixpanel script & initialize it |
| 206 | */ |
| 207 | public function add_script(): void { |
| 208 | ?> |
| 209 | <!-- start Mixpanel --><script> |
| 210 | const MIXPANEL_CUSTOM_LIB_URL = "https://<?php echo esc_js( self::HOST ); ?>/lib.min.js"; |
| 211 | (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 || []); |
| 212 | mixpanel.init( '<?php echo esc_js( $this->token ); ?>', { |
| 213 | api_host: "https://<?php echo esc_js( self::HOST ); ?>", |
| 214 | id: false, |
| 215 | property_blacklist: ['$initial_referrer', '$current_url', '$initial_referring_domain', '$referrer', '$referring_domain'] |
| 216 | } ); |
| 217 | </script><!-- end Mixpanel --> |
| 218 | <?php |
| 219 | } |
| 220 | } |
| 221 |