← All changes
|
core/common/modules/events-manager/module.php
+28
-61
4.3.0-beta3
→
4.1.0-dev3
View file →
| @@ -4,9 +4,8 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | 6 | use Elementor\Core\Common\Modules\Connect\Apps\Base_App; |
| 7 | 7 | use Elementor\Core\Common\Modules\Connect\Apps\Common_App; |
| 8 | -use Elementor\Core\Common\Modules\EventsManager\RestApi\Events_Proxy_REST_API; | |
| 9 | 8 | use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 10 | 9 | use Elementor\Includes\EditorAssetsAPI; |
| 11 | 10 | use Elementor\Utils; |
| 12 | 11 | use Elementor\Plugin; |
| @@ -18,30 +17,23 @@ | ||
| 18 | 17 | |
| 19 | 18 | class Module extends BaseModule { |
| 20 | 19 | |
| 21 | 20 | const EXPERIMENT_NAME = 'editor_events'; |
| 22 | - const DEFAULT_SESSION_RECORDING_PERCENT = 0; | |
| 21 | + | |
| 23 | 22 | const REMOTE_MIXPANEL_CONFIG_URL = 'https://assets.elementor.com/mixpanel/v1/mixpanel.json'; |
| 24 | 23 | |
| 25 | - const API_UPSTREAM_HOST = 'https://api-eu.mixpanel.com'; | |
| 26 | - const LIBS_UPSTREAM_HOST = 'https://cdn.mxpnl.com/libs'; | |
| 27 | - | |
| 28 | - public function __construct() { | |
| 29 | - parent::__construct(); | |
| 30 | - | |
| 31 | - ( new Events_Proxy_REST_API() )->register_hooks(); | |
| 32 | - } | |
| 33 | - | |
| 34 | 24 | public function get_name() { |
| 35 | 25 | return 'events-manager'; |
| 36 | 26 | } |
| 37 | 27 | |
| 38 | 28 | public static function get_editor_events_config() { |
| 39 | - $can_send_events = self::can_send_events(); | |
| 29 | + $can_send_events = ! empty( ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN ) && | |
| 30 | + Tracker::is_allow_track() && | |
| 31 | + ! Tracker::has_terms_changed( '2025-07-07' ) && | |
| 32 | + Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ); | |
| 40 | 33 | |
| 41 | 34 | $is_flags_enabled = false; |
| 42 | - $session_recording_percent = self::DEFAULT_SESSION_RECORDING_PERCENT; | |
| 43 | - $debug = ( defined( 'ELEMENTOR_EDITOR_EVENTS_DEBUG' ) && ELEMENTOR_EDITOR_EVENTS_DEBUG ) ?? false; | |
| 35 | + $session_recording_events = []; | |
| 44 | 36 | |
| 45 | 37 | if ( $can_send_events ) { |
| 46 | 38 | $mixpanel_config = self::get_remote_mixpanel_config(); |
| 47 | 39 | $has_config = EditorAssetsAPI::has_valid_nested_array( $mixpanel_config, [ 0 ] ); |
| @@ -49,15 +41,28 @@ | ||
| 49 | 41 | if ( $has_config ) { |
| 50 | 42 | $is_flags_enabled = (bool) ( $mixpanel_config[0]['flags'] ?? false ); |
| 51 | 43 | |
| 52 | 44 | $session_replays = $mixpanel_config[0]['sessionReplays'] ?? []; |
| 53 | - $session_recording_percent = $session_replays['recordSessionsPercent'] ?? null; | |
| 45 | + $is_session_replays_enabled = (bool) ( $session_replays['enabled'] ?? false ); | |
| 46 | + $raw_events = $session_replays['events'] ?? null; | |
| 47 | + $events_map = is_array( $raw_events ) ? $raw_events : []; | |
| 48 | + | |
| 49 | + if ( $is_session_replays_enabled ) { | |
| 50 | + $session_recording_events = array_values( array_filter( | |
| 51 | + self::get_session_recording_events(), | |
| 52 | + function ( $pair ) use ( $events_map ) { | |
| 53 | + if ( ! isset( $pair['start'] ) ) { | |
| 54 | + return false; | |
| 55 | + } | |
| 56 | + return (bool) ( $events_map[ $pair['start'] ] ?? false ); | |
| 57 | + } | |
| 58 | + ) ); | |
| 59 | + } | |
| 54 | 60 | } |
| 55 | 61 | } |
| 56 | 62 | |
| 57 | 63 | $settings = [ |
| 58 | 64 | 'can_send_events' => $can_send_events, |
| 59 | - 'debug' => $debug, | |
| 60 | 65 | 'elementor_version' => ELEMENTOR_VERSION, |
| 61 | 66 | 'site_url' => hash( 'sha256', get_site_url() ), |
| 62 | 67 | 'wp_version' => get_bloginfo( 'version' ), |
| 63 | 68 | 'user_agent' => esc_html( Utils::get_super_global_value( $_SERVER, 'HTTP_USER_AGENT' ) ), |
| @@ -65,43 +70,16 @@ | ||
| 65 | 70 | 'site_key' => get_option( Base_App::OPTION_CONNECT_SITE_KEY ), |
| 66 | 71 | 'subscription_id' => self::get_subscription_id(), |
| 67 | 72 | 'subscription' => self::get_subscription(), |
| 68 | 73 | 'token' => ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN, |
| 69 | - 'proxy_api_host' => rest_url( Events_Proxy_REST_API::API_NAMESPACE . '/' . Events_Proxy_REST_API::API_BASE . '/api' ), | |
| 70 | - 'proxy_lib_base_path' => rest_url( Events_Proxy_REST_API::API_NAMESPACE . '/' . Events_Proxy_REST_API::API_BASE . '/libs/' ), | |
| 71 | 74 | 'flags_enabled' => $is_flags_enabled, |
| 72 | 75 | 'user_id' => self::get_user_id(), |
| 73 | - 'session_recording_percent' => $session_recording_percent, | |
| 76 | + 'session_recording_events' => $session_recording_events, | |
| 74 | 77 | ]; |
| 75 | 78 | |
| 76 | 79 | return $settings; |
| 77 | 80 | } |
| 78 | 81 | |
| 79 | - public static function dispatch_event( string $event_name, array $properties = [] ): void { | |
| 80 | - if ( ! self::can_send_events() ) { | |
| 81 | - return; | |
| 82 | - } | |
| 83 | - | |
| 84 | - try { | |
| 85 | - $user_id = self::get_user_id(); | |
| 86 | - | |
| 87 | - $context = [ | |
| 88 | - 'user_id' => $user_id, | |
| 89 | - 'distinct_id' => $user_id, | |
| 90 | - 'subscription_id' => self::get_subscription_id(), | |
| 91 | - 'site_key' => get_option( Base_App::OPTION_CONNECT_SITE_KEY ), | |
| 92 | - 'app_version' => ELEMENTOR_VERSION, | |
| 93 | - 'wp_version' => get_bloginfo( 'version' ), | |
| 94 | - 'site_language' => get_locale(), | |
| 95 | - 'source' => 'server', | |
| 96 | - ]; | |
| 97 | - | |
| 98 | - Server_Events_Client::track( $event_name, array_merge( $properties, $context ) ); | |
| 99 | - } catch ( \Throwable $e ) { | |
| 100 | - return; | |
| 101 | - } | |
| 102 | - } | |
| 103 | - | |
| 104 | 82 | public static function get_experimental_data(): array { |
| 105 | 83 | return [ |
| 106 | 84 | 'name' => static::EXPERIMENT_NAME, |
| 107 | 85 | 'title' => esc_html__( 'Elementor Editor Events', 'elementor' ), |
| @@ -115,15 +93,8 @@ | ||
| 115 | 93 | ], |
| 116 | 94 | ]; |
| 117 | 95 | } |
| 118 | 96 | |
| 119 | - private static function can_send_events(): bool { | |
| 120 | - return ! empty( ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN ) && | |
| 121 | - Tracker::is_allow_track() && | |
| 122 | - ! Tracker::has_terms_changed() && | |
| 123 | - Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ); | |
| 124 | - } | |
| 125 | - | |
| 126 | 97 | private static function get_subscription_id() { |
| 127 | 98 | $subscription = self::get_subscription(); |
| 128 | 99 | |
| 129 | 100 | return $subscription['subscription_id'] ?? null; |
| @@ -151,18 +122,14 @@ | ||
| 151 | 122 | |
| 152 | 123 | return $editor_assets_api->get_assets_data(); |
| 153 | 124 | } |
| 154 | 125 | |
| 155 | - public static function get_mixpanel_api_host() { | |
| 156 | - $mixpanel_config = self::get_remote_mixpanel_config(); | |
| 157 | - | |
| 158 | - return wp_http_validate_url( $mixpanel_config[0]['apiHost'] ?? static::API_UPSTREAM_HOST ); | |
| 159 | - } | |
| 160 | - | |
| 161 | - public static function get_mixpanel_lib_host() { | |
| 162 | - $mixpanel_config = self::get_remote_mixpanel_config(); | |
| 163 | - | |
| 164 | - return wp_http_validate_url( $mixpanel_config[0]['libHost'] ?? static::LIBS_UPSTREAM_HOST ); | |
| 126 | + private static function get_session_recording_events(): array { | |
| 127 | + return [ | |
| 128 | + // Each entry defines a recording window: recording starts when 'start' fires | |
| 129 | + // and stops when 'end' fires. 'end' is optional — omit or set to null to record indefinitely. | |
| 130 | + [ 'start' => 'editor_loaded' ], | |
| 131 | + ]; | |
| 165 | 132 | } |
| 166 | 133 | |
| 167 | 134 | private static function get_user_id() { |
| 168 | 135 | $user_common_data = get_user_option( Common_App::OPTION_CONNECT_COMMON_DATA_KEY ); |