From 62f8d7c45d7a351e262dc13fd813c98f1368680a Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Wed, 11 Nov 2026 15:00:41 +0100 Subject: [PATCH 1/8] feat(analytics): Track feature adoption with setting values and a config snapshot `settings_saved` only reported which keys changed, and features left at their defaults were never reported at all, so adoption of lazy loading, responsive images, formats or adaptive streaming couldn't be measured. - `settings_saved` now carries `changed_values` for an allowlist of image, lazy loading, responsive and video settings. Anything outside the list, including the connection string, is never sent. - Values are normalised: toggles to booleans, numbers to ints, free-text transformations to a presence flag, and crop sizes to a count of custom crops. - A one-time `feature_config_snapshot` sends the full feature config on the first admin load once connected, once per cloud name. It covers wizard setups, constant-based connections, account switches and existing installs upgrading. --- php/class-admin.php | 35 +++++++--- php/class-analytics.php | 144 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 11 deletions(-) diff --git a/php/class-admin.php b/php/class-admin.php index 70b2ebb3e..0fb672cd4 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -461,9 +461,10 @@ public function init_setting_save() { * @param array $data The data to save. */ protected function save_settings( $submission, $data ) { - $page = $this->settings->get_setting( $submission ); - $pending = false; - $changed_keys = array(); + $page = $this->settings->get_setting( $submission ); + $pending = false; + $changed_keys = array(); + $changed_values = array(); foreach ( $data as $key => $value ) { $slug = $submission . $page->separator . $key; $current = $this->settings->get_value( $slug ); @@ -479,6 +480,8 @@ protected function save_settings( $submission, $data ) { } $changed_keys[] = $key; $pending = true; + + $changed_values[ $key ] = $value; } if ( true === $pending ) { @@ -488,16 +491,26 @@ protected function save_settings( $submission, $data ) { $analytics = $this->plugin->get_component( 'analytics' ); if ( $analytics ) { - $analytics->track( - 'settings_saved', - 'settings', - null, - array( - 'page' => $submission, - 'changed_keys' => $changed_keys, - ) + $params = array( + 'page' => $submission, + 'changed_keys' => $changed_keys, ); + // Values only for allowlisted feature settings; secrets + // and free text never leave the site. + $feature_values = array(); + foreach ( $changed_values as $key => $value ) { + $normalized = $analytics->normalize_feature_value( $key, $value ); + if ( null !== $normalized ) { + $feature_values[ $key ] = $normalized; + } + } + if ( ! empty( $feature_values ) ) { + $params['changed_values'] = $feature_values; + } + + $analytics->track( 'settings_saved', 'settings', null, $params ); + if ( 'gallery' === $submission ) { $this->track_gallery_configured( $analytics, $data ); } diff --git a/php/class-analytics.php b/php/class-analytics.php index 61df3b29c..50414d6fa 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -65,6 +65,7 @@ public function __construct( Plugin $plugin ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script_data' ) ); add_action( 'admin_init', array( $this, 'maybe_send_smoke_event' ) ); add_action( 'admin_init', array( $this, 'maybe_send_pending_activation' ) ); + add_action( 'admin_init', array( $this, 'maybe_send_feature_snapshot' ) ); add_action( 'cloudinary_uploaded_asset', array( $this, 'maybe_first_api_consumption' ), 10, 2 ); } @@ -75,6 +76,149 @@ public function __construct( Plugin $plugin ) { const LAST_ACTIVE = '_cloudinary_last_active'; const FIRST_API_FLAG = '_cloudinary_first_api_emitted'; + /** + * Option key marking the feature-config snapshot as sent (per cloud_name). + */ + const FEATURE_SNAPSHOT_FLAG = '_cloudinary_feature_snapshot_emitted'; + + /** + * Allowlist of feature settings whose values may be emitted, mapped to how + * each value is normalised before it leaves the site. + * + * Anything not listed here is never sent as a value. Free-text fields are + * reduced to a presence boolean, since they can hold public IDs, overlay + * text or URLs. + * + * - bool: `on`/`off` toggle → true/false. + * - int: numeric field → int. + * - string: select/radio enum → string. + * - presence: free text → whether it is non-empty. + * - crops: per-size crop map → number of sizes with a custom crop. + */ + const FEATURE_SETTINGS = array( + // Image. + 'image_delivery' => 'bool', + 'image_optimization' => 'bool', + 'image_format' => 'string', + 'image_quality' => 'string', + 'image_freeform' => 'presence', + 'svg_support' => 'bool', + 'crop_sizes' => 'crops', + // Lazy loading. + 'use_lazy_load' => 'bool', + 'lazy_threshold' => 'string', + 'lazy_placeholder' => 'string', + 'lazy_animate' => 'bool', + 'dpr' => 'string', + // Responsive images. + 'enable_breakpoints' => 'bool', + 'pixel_step' => 'int', + 'breakpoints' => 'int', + 'max_width' => 'int', + 'min_width' => 'int', + // Video. + 'video_delivery' => 'bool', + 'video_player' => 'string', + 'adaptive_streaming' => 'bool', + 'adaptive_streaming_mode' => 'string', + 'video_controls' => 'bool', + 'video_loop' => 'bool', + 'video_autoplay_mode' => 'string', + 'video_optimization' => 'bool', + 'video_format' => 'string', + 'video_quality' => 'string', + 'video_freeform' => 'presence', + ); + + /** + * Normalises a feature setting value for emission. + * + * @param string $key The setting slug. + * @param mixed $value The raw setting value. + * + * @return bool|int|string|null The normalised value, or null when the key is + * not allowlisted or the value can't be read. + */ + public function normalize_feature_value( $key, $value ) { + if ( ! isset( self::FEATURE_SETTINGS[ $key ] ) ) { + return null; + } + + switch ( self::FEATURE_SETTINGS[ $key ] ) { + case 'bool': + return 'on' === $value || true === $value; + case 'int': + return is_numeric( $value ) ? (int) $value : null; + case 'presence': + return is_string( $value ) && '' !== trim( $value ); + case 'crops': + // Each size maps to '' (default crop), '--' (crop disabled) or + // a custom transformation string. + if ( ! is_array( $value ) ) { + return 0; + } + + return count( + array_filter( + $value, + static function ( $crop ) { + return is_string( $crop ) && '' !== trim( $crop ) && '--' !== $crop; + } + ) + ); + default: + return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : null; + } + } + + /** + * Emits the one-time `feature_config_snapshot` adoption baseline. + * + * Captures the full state of the delivery features, including those left + * at their defaults, which `settings_saved` never reports. Sent on the + * first admin load once the site is connected, then suppressed per + * cloud_name. That covers wizard setups, sites connected through the + * `CLOUDINARY_CONNECTION_STRING` constant, existing installs upgrading to + * this version, and account switches. + * + * @return void + */ + public function maybe_send_feature_snapshot() { + if ( ! $this->is_enabled() ) { + return; + } + + $connect = $this->plugin->get_component( 'connect' ); + if ( ! $connect || ! $connect->is_connected() ) { + return; + } + + $cloud = (string) $connect->get_cloud_name(); + if ( '' === $cloud || get_option( self::FEATURE_SNAPSHOT_FLAG ) === $cloud ) { + return; + } + update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, false ); + + $this->track( 'feature_config_snapshot', 'features', null, $this->get_feature_snapshot() ); + } + + /** + * Reads the current, normalised value of every allowlisted feature setting. + * + * @return array + */ + public function get_feature_snapshot() { + $snapshot = array(); + foreach ( array_keys( self::FEATURE_SETTINGS ) as $key ) { + $value = $this->normalize_feature_value( $key, $this->plugin->settings->get_value( $key ) ); + if ( null !== $value ) { + $snapshot[ $key ] = $value; + } + } + + return $snapshot; + } + /** * Records the activation type on plugin activation (funnel step 1). * From bb21831b7879e3ef4557a86a5f0ec087e5d20124 Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Wed, 11 Nov 2026 15:00:43 +0100 Subject: [PATCH 2/8] test: Cover feature values in settings_saved and the config snapshot --- tests/e2e/features-analytics.spec.js | 36 ++++++- tests/e2e/settings-analytics.spec.js | 3 + .../tests/test-analytics-feature-values.php | 100 ++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/test-analytics-feature-values.php diff --git a/tests/e2e/features-analytics.spec.js b/tests/e2e/features-analytics.spec.js index 16e83f6f8..f5dfdc557 100644 --- a/tests/e2e/features-analytics.spec.js +++ b/tests/e2e/features-analytics.spec.js @@ -6,7 +6,7 @@ const { test, expect } = require( './fixtures' ); /** * Internal dependencies */ -const { wpEvalFile } = require( './utils/wizard' ); +const { wpCli, wpEvalFile } = require( './utils/wizard' ); const { fakeCloudinaryConnected } = require( './utils/connection' ); const { clearAnalyticsEvents, @@ -81,4 +81,38 @@ test.describe( 'Extensions & gallery analytics', () => { expect( events[ 0 ].layout ).toBe( 'expand' ); expect( events[ 0 ].media_count ).toBe( 2 ); } ); + + test( 'the first admin load once connected emits feature_config_snapshot once', async ( { + admin, + } ) => { + // Every other spec's admin loads already sent the snapshot for this + // cloud_name, so reset the one-time flag to simulate a fresh setup. + wpCli( [ 'option', 'delete', '_cloudinary_feature_snapshot_emitted' ] ); + clearAnalyticsEvents(); + + await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); + await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); + + const events = findAnalyticsEvents( + readAnalyticsEvents(), + 'feature_config_snapshot' + ); + expect( events.length ).toBe( 1 ); + expect( events[ 0 ].event_category ).toBe( 'features' ); + + // Booleans, ints and enums are normalised, and default values are + // reported even though they were never explicitly saved. + expect( typeof events[ 0 ].image_optimization ).toBe( 'boolean' ); + expect( typeof events[ 0 ].use_lazy_load ).toBe( 'boolean' ); + expect( typeof events[ 0 ].enable_breakpoints ).toBe( 'boolean' ); + expect( typeof events[ 0 ].adaptive_streaming ).toBe( 'boolean' ); + expect( typeof events[ 0 ].pixel_step ).toBe( 'number' ); + expect( typeof events[ 0 ].crop_sizes ).toBe( 'number' ); + expect( typeof events[ 0 ].image_format ).toBe( 'string' ); + expect( typeof events[ 0 ].video_player ).toBe( 'string' ); + + // Free-text transformations are reduced to a presence flag. + expect( typeof events[ 0 ].image_freeform ).toBe( 'boolean' ); + expect( typeof events[ 0 ].video_freeform ).toBe( 'boolean' ); + } ); } ); diff --git a/tests/e2e/settings-analytics.spec.js b/tests/e2e/settings-analytics.spec.js index 27e294816..bf8aa37d9 100644 --- a/tests/e2e/settings-analytics.spec.js +++ b/tests/e2e/settings-analytics.spec.js @@ -79,6 +79,9 @@ test.describe( 'Settings & navigation analytics', () => { expect( savedEvents.length ).toBe( 1 ); expect( savedEvents[ 0 ].page ).toBe( 'image_settings' ); expect( savedEvents[ 0 ].changed_keys ).toContain( 'image_quality' ); + expect( savedEvents[ 0 ].changed_values.image_quality ).toBe( + nextValue + ); } ); test( 'dismissing an admin notice emits notice_dismissed', async ( { diff --git a/tests/phpunit/tests/test-analytics-feature-values.php b/tests/phpunit/tests/test-analytics-feature-values.php new file mode 100644 index 000000000..17a42fb91 --- /dev/null +++ b/tests/phpunit/tests/test-analytics-feature-values.php @@ -0,0 +1,100 @@ +analytics = get_plugin_instance()->get_component( 'analytics' ); + } + + /** + * Settings outside the allowlist, including secrets, are never emitted. + * + * @return void + */ + public function test_non_allowlisted_settings_are_dropped() { + $this->assertNull( $this->analytics->normalize_feature_value( 'cloudinary_url', 'cloudinary://key:secret@demo' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'cloudinary_folder', 'my-folder' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_custom_color', 'rgba(0,0,0,1)' ) ); + } + + /** + * On/off toggles become booleans. + * + * @return void + */ + public function test_toggles_become_booleans() { + $this->assertTrue( $this->analytics->normalize_feature_value( 'use_lazy_load', 'on' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'use_lazy_load', 'off' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'adaptive_streaming', '' ) ); + } + + /** + * Numeric fields become ints, and unreadable ones are dropped. + * + * @return void + */ + public function test_numbers_become_ints() { + $this->assertSame( 200, $this->analytics->normalize_feature_value( 'pixel_step', '200' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'breakpoints', '' ) ); + } + + /** + * Enum values pass through as strings. + * + * @return void + */ + public function test_enums_stay_strings() { + $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_format', 'avif' ) ); + $this->assertSame( 'auto:good', $this->analytics->normalize_feature_value( 'image_quality', 'auto:good' ) ); + } + + /** + * Free-text transformations are reduced to a presence flag. + * + * @return void + */ + public function test_freeform_is_reduced_to_presence() { + $this->assertTrue( $this->analytics->normalize_feature_value( 'image_freeform', 'l_text:Arial_20:Jane Doe' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'video_freeform', ' ' ) ); + } + + /** + * Crop sizes are summarised as the number of sizes with a custom crop. + * + * @return void + */ + public function test_crop_sizes_count_custom_crops() { + $crops = array( + '150x150' => 'c_thumb,g_face', + '300x300' => '', + '1024x768' => '--', + '768x0' => 'c_fill,g_auto', + ); + + $this->assertSame( 2, $this->analytics->normalize_feature_value( 'crop_sizes', $crops ) ); + $this->assertSame( 0, $this->analytics->normalize_feature_value( 'crop_sizes', '' ) ); + } +} From 8be4aeccdcd1a1ce976b6be9c10c8be9c85614af Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Thu, 12 Nov 2026 09:12:50 +0100 Subject: [PATCH 3/8] fix(analytics): Harden the feature config snapshot and settings values - Send the snapshot only on admin page loads by administrators, never from admin-ajax requests or for other roles. - Autoload the one-time flag so checking it costs no query, and gate the first send with add_option() so concurrent requests can't both send it. - Leave missing setting values out instead of reporting them as off. - Send the lazy-loading threshold only when it is a CSS length; it is a free-text field. - Allowlist feature settings per settings page. - Report `changed_values` as read back after the save, so they reflect any save filters. - Remove the analytics one-time flags on uninstall. --- php/class-admin.php | 16 ++--- php/class-analytics.php | 143 +++++++++++++++++++++++-------------- php/class-deactivation.php | 2 + 3 files changed, 97 insertions(+), 64 deletions(-) diff --git a/php/class-admin.php b/php/class-admin.php index 0fb672cd4..9bec1020b 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -461,10 +461,9 @@ public function init_setting_save() { * @param array $data The data to save. */ protected function save_settings( $submission, $data ) { - $page = $this->settings->get_setting( $submission ); - $pending = false; - $changed_keys = array(); - $changed_values = array(); + $page = $this->settings->get_setting( $submission ); + $pending = false; + $changed_keys = array(); foreach ( $data as $key => $value ) { $slug = $submission . $page->separator . $key; $current = $this->settings->get_value( $slug ); @@ -480,8 +479,6 @@ protected function save_settings( $submission, $data ) { } $changed_keys[] = $key; $pending = true; - - $changed_values[ $key ] = $value; } if ( true === $pending ) { @@ -497,10 +494,11 @@ protected function save_settings( $submission, $data ) { ); // Values only for allowlisted feature settings; secrets - // and free text never leave the site. + // and free text never leave the site. Read back after the + // save so the value reflects any save filters. $feature_values = array(); - foreach ( $changed_values as $key => $value ) { - $normalized = $analytics->normalize_feature_value( $key, $value ); + foreach ( $changed_keys as $key ) { + $normalized = $analytics->normalize_feature_value( $submission, $key, $this->settings->get_value( $key ) ); if ( null !== $normalized ) { $feature_values[ $key ] = $normalized; } diff --git a/php/class-analytics.php b/php/class-analytics.php index 50414d6fa..27d1f1ad5 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -82,73 +82,85 @@ public function __construct( Plugin $plugin ) { const FEATURE_SNAPSHOT_FLAG = '_cloudinary_feature_snapshot_emitted'; /** - * Allowlist of feature settings whose values may be emitted, mapped to how - * each value is normalised before it leaves the site. + * Allowlist of feature settings whose values may be emitted, per settings + * page, mapped to how each value is normalised before it leaves the site. * * Anything not listed here is never sent as a value. Free-text fields are * reduced to a presence boolean, since they can hold public IDs, overlay * text or URLs. * - * - bool: `on`/`off` toggle → true/false. - * - int: numeric field → int. - * - string: select/radio enum → string. - * - presence: free text → whether it is non-empty. - * - crops: per-size crop map → number of sizes with a custom crop. + * - bool: `on`/`off` toggle → true/false. + * - int: numeric field → int. + * - string: select/radio enum → string. + * - css_length: free-text CSS length → the length, or dropped when it is + * anything else. + * - presence: free text → whether it is non-empty. + * - crops: per-size crop map → number of sizes with a custom crop. */ const FEATURE_SETTINGS = array( - // Image. - 'image_delivery' => 'bool', - 'image_optimization' => 'bool', - 'image_format' => 'string', - 'image_quality' => 'string', - 'image_freeform' => 'presence', - 'svg_support' => 'bool', - 'crop_sizes' => 'crops', - // Lazy loading. - 'use_lazy_load' => 'bool', - 'lazy_threshold' => 'string', - 'lazy_placeholder' => 'string', - 'lazy_animate' => 'bool', - 'dpr' => 'string', - // Responsive images. - 'enable_breakpoints' => 'bool', - 'pixel_step' => 'int', - 'breakpoints' => 'int', - 'max_width' => 'int', - 'min_width' => 'int', - // Video. - 'video_delivery' => 'bool', - 'video_player' => 'string', - 'adaptive_streaming' => 'bool', - 'adaptive_streaming_mode' => 'string', - 'video_controls' => 'bool', - 'video_loop' => 'bool', - 'video_autoplay_mode' => 'string', - 'video_optimization' => 'bool', - 'video_format' => 'string', - 'video_quality' => 'string', - 'video_freeform' => 'presence', + 'image_settings' => array( + 'image_delivery' => 'bool', + 'image_optimization' => 'bool', + 'image_format' => 'string', + 'image_quality' => 'string', + 'image_freeform' => 'presence', + 'svg_support' => 'bool', + 'crop_sizes' => 'crops', + ), + 'lazy_loading' => array( + 'use_lazy_load' => 'bool', + 'lazy_threshold' => 'css_length', + 'lazy_placeholder' => 'string', + 'lazy_animate' => 'bool', + 'dpr' => 'string', + ), + 'responsive' => array( + 'enable_breakpoints' => 'bool', + 'pixel_step' => 'int', + 'breakpoints' => 'int', + 'max_width' => 'int', + 'min_width' => 'int', + ), + 'video_settings' => array( + 'video_delivery' => 'bool', + 'video_player' => 'string', + 'adaptive_streaming' => 'bool', + 'adaptive_streaming_mode' => 'string', + 'video_controls' => 'bool', + 'video_loop' => 'bool', + 'video_autoplay_mode' => 'string', + 'video_optimization' => 'bool', + 'video_format' => 'string', + 'video_quality' => 'string', + 'video_freeform' => 'presence', + ), ); /** * Normalises a feature setting value for emission. * + * @param string $page The settings page slug the setting belongs to. * @param string $key The setting slug. * @param mixed $value The raw setting value. * - * @return bool|int|string|null The normalised value, or null when the key is - * not allowlisted or the value can't be read. + * @return bool|int|string|null The normalised value, or null when the + * setting is not allowlisted for that page or + * its value is missing or unusable. */ - public function normalize_feature_value( $key, $value ) { - if ( ! isset( self::FEATURE_SETTINGS[ $key ] ) ) { + public function normalize_feature_value( $page, $key, $value ) { + if ( ! isset( self::FEATURE_SETTINGS[ $page ][ $key ] ) || null === $value ) { return null; } - switch ( self::FEATURE_SETTINGS[ $key ] ) { + switch ( self::FEATURE_SETTINGS[ $page ][ $key ] ) { case 'bool': return 'on' === $value || true === $value; case 'int': return is_numeric( $value ) ? (int) $value : null; + case 'css_length': + $value = is_string( $value ) ? trim( $value ) : ''; + + return preg_match( '/^\d+(\.\d+)?(px|%|r?em|vh|vw)?$/', $value ) ? $value : null; case 'presence': return is_string( $value ) && '' !== trim( $value ); case 'crops': @@ -176,15 +188,17 @@ static function ( $crop ) { * * Captures the full state of the delivery features, including those left * at their defaults, which `settings_saved` never reports. Sent on the - * first admin load once the site is connected, then suppressed per - * cloud_name. That covers wizard setups, sites connected through the - * `CLOUDINARY_CONNECTION_STRING` constant, existing installs upgrading to - * this version, and account switches. + * first admin page load by an administrator once the site is connected, + * then suppressed per cloud_name. That covers wizard setups, sites + * connected through the `CLOUDINARY_CONNECTION_STRING` constant, existing + * installs upgrading to this version, and account switches. * * @return void */ public function maybe_send_feature_snapshot() { - if ( ! $this->is_enabled() ) { + // `admin_init` also runs for admin-ajax requests, including logged-out + // ones, and for non-admin users visiting their profile. + if ( wp_doing_ajax() || ! Utils::user_can( 'analytics', 'manage_options' ) || ! $this->is_enabled() ) { return; } @@ -194,10 +208,27 @@ public function maybe_send_feature_snapshot() { } $cloud = (string) $connect->get_cloud_name(); - if ( '' === $cloud || get_option( self::FEATURE_SNAPSHOT_FLAG ) === $cloud ) { + if ( '' === $cloud ) { + return; + } + + // Autoloaded, so this check costs no query on later admin loads. + $sent = get_option( self::FEATURE_SNAPSHOT_FLAG ); + if ( $sent === $cloud ) { return; } - update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, false ); + + if ( false === $sent ) { + // Only one of several concurrent first requests sends: add_option() + // upserts, and a request writing the same cloud name as an earlier + // one changes no row, so add_option() returns false for it. + if ( ! add_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, '', true ) ) { + return; + } + } else { + // Account switched. + update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, true ); + } $this->track( 'feature_config_snapshot', 'features', null, $this->get_feature_snapshot() ); } @@ -209,10 +240,12 @@ public function maybe_send_feature_snapshot() { */ public function get_feature_snapshot() { $snapshot = array(); - foreach ( array_keys( self::FEATURE_SETTINGS ) as $key ) { - $value = $this->normalize_feature_value( $key, $this->plugin->settings->get_value( $key ) ); - if ( null !== $value ) { - $snapshot[ $key ] = $value; + foreach ( self::FEATURE_SETTINGS as $page => $settings ) { + foreach ( array_keys( $settings ) as $key ) { + $value = $this->normalize_feature_value( $page, $key, $this->plugin->settings->get_value( $key ) ); + if ( null !== $value ) { + $snapshot[ $key ] = $value; + } } } diff --git a/php/class-deactivation.php b/php/class-deactivation.php index 04cf82d69..a60fa7ea0 100644 --- a/php/class-deactivation.php +++ b/php/class-deactivation.php @@ -630,6 +630,8 @@ protected function cleanup_options() { '_cld_disable_http_upload', Report::REPORT_KEY, Media::GLOBAL_VIDEO_TRANSFORMATIONS, + Analytics::FIRST_API_FLAG, + Analytics::FEATURE_SNAPSHOT_FLAG, self::CLEANING_KEY, ) ); From e8536702865701c8ce60342896ccde292bfbcbee Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Thu, 12 Nov 2026 09:12:56 +0100 Subject: [PATCH 4/8] test: Cover the snapshot rules in PHPUnit and every feature page in e2e --- tests/e2e/features-analytics.spec.js | 30 ++- tests/e2e/settings-analytics.spec.js | 61 +++++ .../tests/test-analytics-feature-snapshot.php | 237 ++++++++++++++++++ .../tests/test-analytics-feature-values.php | 62 +++-- 4 files changed, 360 insertions(+), 30 deletions(-) create mode 100644 tests/phpunit/tests/test-analytics-feature-snapshot.php diff --git a/tests/e2e/features-analytics.spec.js b/tests/e2e/features-analytics.spec.js index f5dfdc557..24ada2df3 100644 --- a/tests/e2e/features-analytics.spec.js +++ b/tests/e2e/features-analytics.spec.js @@ -81,15 +81,23 @@ test.describe( 'Extensions & gallery analytics', () => { expect( events[ 0 ].layout ).toBe( 'expand' ); expect( events[ 0 ].media_count ).toBe( 2 ); } ); +} ); - test( 'the first admin load once connected emits feature_config_snapshot once', async ( { - admin, - } ) => { - // Every other spec's admin loads already sent the snapshot for this - // cloud_name, so reset the one-time flag to simulate a fresh setup. +// @serial: the snapshot's one-time flag is shared by the whole site, so a +// parallel worker's admin load could send it into that worker's log instead. +// The sending rules are covered by PHPUnit; this checks the admin wiring. +test.describe( 'Feature config snapshot', { tag: '@serial' }, () => { + test.beforeEach( () => { + fakeCloudinaryConnected(); + // Earlier admin loads already sent the snapshot for this cloud_name, + // so reset the one-time flag to simulate a fresh setup. wpCli( [ 'option', 'delete', '_cloudinary_feature_snapshot_emitted' ] ); clearAnalyticsEvents(); + } ); + test( 'the first admin load once connected emits feature_config_snapshot once', async ( { + admin, + } ) => { await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); await admin.visitAdminPage( 'admin.php', 'page=cloudinary' ); @@ -100,19 +108,9 @@ test.describe( 'Extensions & gallery analytics', () => { expect( events.length ).toBe( 1 ); expect( events[ 0 ].event_category ).toBe( 'features' ); - // Booleans, ints and enums are normalised, and default values are - // reported even though they were never explicitly saved. - expect( typeof events[ 0 ].image_optimization ).toBe( 'boolean' ); + // Default values are reported even though they were never saved. expect( typeof events[ 0 ].use_lazy_load ).toBe( 'boolean' ); - expect( typeof events[ 0 ].enable_breakpoints ).toBe( 'boolean' ); - expect( typeof events[ 0 ].adaptive_streaming ).toBe( 'boolean' ); expect( typeof events[ 0 ].pixel_step ).toBe( 'number' ); - expect( typeof events[ 0 ].crop_sizes ).toBe( 'number' ); expect( typeof events[ 0 ].image_format ).toBe( 'string' ); - expect( typeof events[ 0 ].video_player ).toBe( 'string' ); - - // Free-text transformations are reduced to a presence flag. - expect( typeof events[ 0 ].image_freeform ).toBe( 'boolean' ); - expect( typeof events[ 0 ].video_freeform ).toBe( 'boolean' ); } ); } ); diff --git a/tests/e2e/settings-analytics.spec.js b/tests/e2e/settings-analytics.spec.js index bf8aa37d9..4ea7ff984 100644 --- a/tests/e2e/settings-analytics.spec.js +++ b/tests/e2e/settings-analytics.spec.js @@ -84,6 +84,67 @@ test.describe( 'Settings & navigation analytics', () => { ); } ); + // One feature page each, flipping a field no other spec writes to, so + // parallel workers can't change it between this test's read and save. + for ( const { page: slug, tag, field, values, expected } of [ + { + page: 'video_settings', + tag: 'select', + field: 'video_quality', + values: [ 'auto', '80' ], + expected: ( value ) => value, + }, + { + page: 'lazy_loading', + tag: 'select', + field: 'dpr', + values: [ '2X', 'max' ], + expected: ( value ) => value, + }, + { + page: 'responsive', + tag: 'input', + field: 'breakpoints', + values: [ '10', '12' ], + expected: ( value ) => Number( value ), + }, + ] ) { + test( `saving the ${ slug } page emits settings_saved with its values`, async ( { + admin, + page, + } ) => { + await admin.visitAdminPage( + 'admin.php', + `page=cloudinary_${ slug }` + ); + + // Set through the DOM: some of these fields are hidden until + // their feature is toggled on, but are still submitted. + const input = page.locator( `${ tag }[name$="[${ field }]"]` ); + const current = await input.inputValue(); + const nextValue = + values[ 0 ] === current ? values[ 1 ] : values[ 0 ]; + await input.evaluate( ( el, value ) => { + el.value = value; + el.dispatchEvent( new Event( 'input', { bubbles: true } ) ); + el.dispatchEvent( new Event( 'change', { bubbles: true } ) ); + }, nextValue ); + + await page.locator( SEL.saveButton ).click(); + await page.waitForLoadState( 'networkidle' ); + + const savedEvents = findAnalyticsEvents( + readAnalyticsEvents(), + 'settings_saved' + ); + expect( savedEvents.length ).toBe( 1 ); + expect( savedEvents[ 0 ].page ).toBe( slug ); + expect( savedEvents[ 0 ].changed_values[ field ] ).toBe( + expected( nextValue ) + ); + } ); + } + test( 'dismissing an admin notice emits notice_dismissed', async ( { admin, page, diff --git a/tests/phpunit/tests/test-analytics-feature-snapshot.php b/tests/phpunit/tests/test-analytics-feature-snapshot.php new file mode 100644 index 000000000..bdac8e62b --- /dev/null +++ b/tests/phpunit/tests/test-analytics-feature-snapshot.php @@ -0,0 +1,237 @@ +analytics = $plugin->get_component( 'analytics' ); + + $this->original_connected = $plugin->settings->get_param( 'connected', null ); + $this->original_cloud_name = $plugin->get_component( 'connect' )->get_cloud_name(); + $this->connect_as( 'first-cloud' ); + + delete_option( Analytics::FEATURE_SNAPSHOT_FLAG ); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $this->sent = array(); + add_filter( 'pre_http_request', array( $this, 'capture_event' ), 1, 3 ); + } + + /** + * Restores the connection state. + * + * @return void + */ + public function tear_down() { + remove_filter( 'pre_http_request', array( $this, 'capture_event' ), 1 ); + + $plugin = get_plugin_instance(); + $plugin->settings->set_param( 'connected', $this->original_connected ); + $plugin->get_component( 'connect' )->set_credentials( array( 'cloud_name' => $this->original_cloud_name ) ); + + parent::tear_down(); + } + + /** + * Captures analytics events instead of sending them. + * + * @param false|array $preempt Whether to preempt the request. + * @param array $args The request arguments. + * @param string $url The request URL. + * + * @return false|array + */ + public function capture_event( $preempt, $args, $url ) { + if ( CLOUDINARY_ENDPOINTS_ANALYTICS !== $url ) { + return $preempt; + } + $this->sent[] = json_decode( $args['body'], true ); + + return array( + 'headers' => array(), + 'body' => '', + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + 'cookies' => array(), + ); + } + + /** + * Marks the plugin as connected to the given cloud. + * + * @param string $cloud_name The cloud name. + * + * @return void + */ + protected function connect_as( $cloud_name ) { + $plugin = get_plugin_instance(); + $plugin->settings->set_param( 'connected', true ); + $plugin->get_component( 'connect' )->set_credentials( array( 'cloud_name' => $cloud_name ) ); + } + + /** + * Returns the captured snapshot events. + * + * @return array[] + */ + protected function snapshots() { + return array_values( + array_filter( + $this->sent, + static function ( $event ) { + return 'feature_config_snapshot' === $event['event_name']; + } + ) + ); + } + + /** + * The snapshot is sent once per account, with the feature values. + * + * @return void + */ + public function test_sends_once_per_account() { + $this->analytics->maybe_send_feature_snapshot(); + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'features', $snapshots[0]['event_category'] ); + $this->assertIsBool( $snapshots[0]['use_lazy_load'] ); + $this->assertArrayNotHasKey( 'cloudinary_url', $snapshots[0] ); + $this->assertSame( 'first-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * Switching to another account sends a new snapshot. + * + * @return void + */ + public function test_sends_again_after_an_account_switch() { + $this->analytics->maybe_send_feature_snapshot(); + $this->connect_as( 'second-cloud' ); + $this->analytics->maybe_send_feature_snapshot(); + + $this->assertCount( 2, $this->snapshots() ); + $this->assertSame( 'second-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * Nothing is sent before the site is connected. + * + * @return void + */ + public function test_not_sent_while_disconnected() { + get_plugin_instance()->settings->set_param( 'connected', false ); + $this->analytics->maybe_send_feature_snapshot(); + + $this->assertCount( 0, $this->snapshots() ); + $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + + /** + * With analytics disabled nothing is sent, and the snapshot is still sent + * once they are enabled again. + * + * @return void + */ + public function test_deferred_while_analytics_are_disabled() { + add_filter( 'cloudinary_analytics_enabled', '__return_false' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'cloudinary_analytics_enabled', '__return_false' ); + + $this->assertCount( 0, $this->snapshots() ); + + $this->analytics->maybe_send_feature_snapshot(); + $this->assertCount( 1, $this->snapshots() ); + } + + /** + * Admin loads by non-administrators don't send the snapshot. + * + * @return void + */ + public function test_not_sent_for_non_administrators() { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) ); + $this->analytics->maybe_send_feature_snapshot(); + + $this->assertCount( 0, $this->snapshots() ); + } + + /** + * Admin-ajax requests don't send the snapshot. + * + * @return void + */ + public function test_not_sent_during_ajax_requests() { + add_filter( 'wp_doing_ajax', '__return_true' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'wp_doing_ajax', '__return_true' ); + + $this->assertCount( 0, $this->snapshots() ); + } + + /** + * When another request already added the flag, this one doesn't send. + * + * @return void + */ + public function test_only_the_request_that_adds_the_flag_sends() { + // Simulates a concurrent request having added the flag after this one + // read it: the add_option() gate must fail and nothing be sent. + add_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); + add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); + + $this->assertCount( 0, $this->snapshots() ); + } +} diff --git a/tests/phpunit/tests/test-analytics-feature-values.php b/tests/phpunit/tests/test-analytics-feature-values.php index 17a42fb91..f205e0f83 100644 --- a/tests/phpunit/tests/test-analytics-feature-values.php +++ b/tests/phpunit/tests/test-analytics-feature-values.php @@ -35,9 +35,30 @@ public function set_up() { * @return void */ public function test_non_allowlisted_settings_are_dropped() { - $this->assertNull( $this->analytics->normalize_feature_value( 'cloudinary_url', 'cloudinary://key:secret@demo' ) ); - $this->assertNull( $this->analytics->normalize_feature_value( 'cloudinary_folder', 'my-folder' ) ); - $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_custom_color', 'rgba(0,0,0,1)' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'connect', 'cloudinary_url', 'cloudinary://key:secret@demo' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'connect', 'cloudinary_folder', 'my-folder' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_custom_color', 'rgba(0,0,0,1)' ) ); + } + + /** + * A setting is only allowlisted on its own settings page. + * + * @return void + */ + public function test_settings_are_allowlisted_per_page() { + $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_settings', 'image_format', 'avif' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'video_settings', 'image_format', 'avif' ) ); + } + + /** + * A missing value is left out rather than reported as off. + * + * @return void + */ + public function test_missing_values_are_dropped() { + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', null ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'image_freeform', null ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', null ) ); } /** @@ -46,9 +67,9 @@ public function test_non_allowlisted_settings_are_dropped() { * @return void */ public function test_toggles_become_booleans() { - $this->assertTrue( $this->analytics->normalize_feature_value( 'use_lazy_load', 'on' ) ); - $this->assertFalse( $this->analytics->normalize_feature_value( 'use_lazy_load', 'off' ) ); - $this->assertFalse( $this->analytics->normalize_feature_value( 'adaptive_streaming', '' ) ); + $this->assertTrue( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', 'on' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'lazy_loading', 'use_lazy_load', 'off' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'video_settings', 'adaptive_streaming', '' ) ); } /** @@ -57,8 +78,8 @@ public function test_toggles_become_booleans() { * @return void */ public function test_numbers_become_ints() { - $this->assertSame( 200, $this->analytics->normalize_feature_value( 'pixel_step', '200' ) ); - $this->assertNull( $this->analytics->normalize_feature_value( 'breakpoints', '' ) ); + $this->assertSame( 200, $this->analytics->normalize_feature_value( 'responsive', 'pixel_step', '200' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'breakpoints', '' ) ); } /** @@ -67,8 +88,21 @@ public function test_numbers_become_ints() { * @return void */ public function test_enums_stay_strings() { - $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_format', 'avif' ) ); - $this->assertSame( 'auto:good', $this->analytics->normalize_feature_value( 'image_quality', 'auto:good' ) ); + $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_settings', 'image_format', 'avif' ) ); + $this->assertSame( 'auto:good', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', 'auto:good' ) ); + } + + /** + * The lazy-loading threshold is only sent when it is a CSS length. + * + * @return void + */ + public function test_lazy_threshold_only_keeps_css_lengths() { + $this->assertSame( '100px', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', ' 100px ' ) ); + $this->assertSame( '1.5em', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '1.5em' ) ); + $this->assertSame( '20%', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '20%' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', 'jane@example.com' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '' ) ); } /** @@ -77,8 +111,8 @@ public function test_enums_stay_strings() { * @return void */ public function test_freeform_is_reduced_to_presence() { - $this->assertTrue( $this->analytics->normalize_feature_value( 'image_freeform', 'l_text:Arial_20:Jane Doe' ) ); - $this->assertFalse( $this->analytics->normalize_feature_value( 'video_freeform', ' ' ) ); + $this->assertTrue( $this->analytics->normalize_feature_value( 'image_settings', 'image_freeform', 'l_text:Arial_20:Jane Doe' ) ); + $this->assertFalse( $this->analytics->normalize_feature_value( 'video_settings', 'video_freeform', ' ' ) ); } /** @@ -94,7 +128,7 @@ public function test_crop_sizes_count_custom_crops() { '768x0' => 'c_fill,g_auto', ); - $this->assertSame( 2, $this->analytics->normalize_feature_value( 'crop_sizes', $crops ) ); - $this->assertSame( 0, $this->analytics->normalize_feature_value( 'crop_sizes', '' ) ); + $this->assertSame( 2, $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', $crops ) ); + $this->assertSame( 0, $this->analytics->normalize_feature_value( 'image_settings', 'crop_sizes', '' ) ); } } From d176c5751bdbb264985a7a8696adcfaa6bfcca42 Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Mon, 16 Nov 2026 09:26:39 +0100 Subject: [PATCH 5/8] feat(analytics): Tell setups from upgrades and count max images in the feature snapshot The feature_config_snapshot event now carries a trigger param: setup when the site was installed or seen unconnected by this version before being connected (or switched account), upgrade when it was already connected. Account switches also go through the same one-request-only gate. "Max images" is derived by the settings page from the breakpoint distance and the min/max widths, and only stored once that page is saved, so the snapshot now reports the count the page shows. --- php/class-analytics.php | 83 +++++++++--- tests/e2e/features-analytics.spec.js | 3 + .../tests/test-analytics-feature-snapshot.php | 128 +++++++++++++++++- 3 files changed, 192 insertions(+), 22 deletions(-) diff --git a/php/class-analytics.php b/php/class-analytics.php index 27d1f1ad5..10f812ca7 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -193,44 +193,55 @@ static function ( $crop ) { * connected through the `CLOUDINARY_CONNECTION_STRING` constant, existing * installs upgrading to this version, and account switches. * + * The `trigger` param tells the two cases apart: + * + * - `setup`: the site was installed, or seen unconnected, by a version + * that sends the snapshot, then connected (or switched + * account). The flag holds an empty string until then. + * - `upgrade`: the site was already connected the first time a version + * that sends the snapshot ran, so the flag was never set. + * * @return void */ public function maybe_send_feature_snapshot() { // `admin_init` also runs for admin-ajax requests, including logged-out // ones, and for non-admin users visiting their profile. - if ( wp_doing_ajax() || ! Utils::user_can( 'analytics', 'manage_options' ) || ! $this->is_enabled() ) { + if ( wp_doing_ajax() || ! Utils::user_can( 'analytics', 'manage_options' ) ) { return; } + // Autoloaded, so this check costs no query on later admin loads. + $sent = get_option( self::FEATURE_SNAPSHOT_FLAG ); + $connect = $this->plugin->get_component( 'connect' ); - if ( ! $connect || ! $connect->is_connected() ) { + $cloud = $connect && $connect->is_connected() ? (string) $connect->get_cloud_name() : ''; + if ( '' === $cloud ) { + if ( false === $sent ) { + // Remembers the site as not yet set up, so connecting it later + // counts as a setup rather than an upgrade. + add_option( self::FEATURE_SNAPSHOT_FLAG, '', '', true ); + } return; } - $cloud = (string) $connect->get_cloud_name(); - if ( '' === $cloud ) { + if ( $sent === $cloud || ! $this->is_enabled() ) { return; } - // Autoloaded, so this check costs no query on later admin loads. - $sent = get_option( self::FEATURE_SNAPSHOT_FLAG ); - if ( $sent === $cloud ) { + // Only one of several concurrent requests sends: add_option() upserts + // and update_option() updates, and a request writing the same cloud + // name as an earlier one changes no row, so it gets false back. + $claimed = false === $sent + ? add_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, '', true ) + : update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, true ); + if ( ! $claimed ) { return; } - if ( false === $sent ) { - // Only one of several concurrent first requests sends: add_option() - // upserts, and a request writing the same cloud name as an earlier - // one changes no row, so add_option() returns false for it. - if ( ! add_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, '', true ) ) { - return; - } - } else { - // Account switched. - update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, true ); - } + $params = $this->get_feature_snapshot(); + $params['trigger'] = false === $sent ? 'upgrade' : 'setup'; - $this->track( 'feature_config_snapshot', 'features', null, $this->get_feature_snapshot() ); + $this->track( 'feature_config_snapshot', 'features', null, $params ); } /** @@ -249,9 +260,40 @@ public function get_feature_snapshot() { } } + // "Max images" is only ever what the settings page derives from the + // other responsive fields, and is stored only once that page is saved. + $snapshot['breakpoints'] = $this->get_breakpoint_count(); + return $snapshot; } + /** + * Counts the responsive image widths, as the settings page shows them in + * "Max images" (see `breakpoints-preview.js`): from the max width down to + * the min width, one image per breakpoint distance. + * + * @return int + */ + public function get_breakpoint_count() { + $settings = $this->plugin->settings; + $max = (int) $settings->get_value( 'max_width' ); + $min = (int) $settings->get_value( 'min_width' ); + $step = (int) $settings->get_value( 'pixel_step' ); + + // The same fallbacks as the settings page. + if ( $max < 1 ) { + $max = (int) $this->plugin->get_component( 'media' )->default_max_width(); + } + if ( $min < 1 ) { + $min = 100; + } + if ( $step < 1 ) { + $step = 50; + } + + return $max < $min ? 1 : 1 + (int) floor( ( $max - $min ) / $step ); + } + /** * Records the activation type on plugin activation (funnel step 1). * @@ -271,6 +313,9 @@ public static function stash_activation() { if ( empty( $db_version ) ) { $type = 'fresh_install'; $previous = null; + // A fresh install's first feature snapshot is a setup, even + // when it is connected through the constant from the start. + add_option( self::FEATURE_SNAPSHOT_FLAG, '', '', true ); } elseif ( version_compare( $db_version, $current, '<' ) ) { $type = 'upgrade'; $previous = $db_version; diff --git a/tests/e2e/features-analytics.spec.js b/tests/e2e/features-analytics.spec.js index 24ada2df3..e034ecef7 100644 --- a/tests/e2e/features-analytics.spec.js +++ b/tests/e2e/features-analytics.spec.js @@ -107,6 +107,9 @@ test.describe( 'Feature config snapshot', { tag: '@serial' }, () => { ); expect( events.length ).toBe( 1 ); expect( events[ 0 ].event_category ).toBe( 'features' ); + // The flag was deleted while the site was connected, as on a site + // that was already set up before upgrading. + expect( events[ 0 ].trigger ).toBe( 'upgrade' ); // Default values are reported even though they were never saved. expect( typeof events[ 0 ].use_lazy_load ).toBe( 'boolean' ); diff --git a/tests/phpunit/tests/test-analytics-feature-snapshot.php b/tests/phpunit/tests/test-analytics-feature-snapshot.php index bdac8e62b..24d97c660 100644 --- a/tests/phpunit/tests/test-analytics-feature-snapshot.php +++ b/tests/phpunit/tests/test-analytics-feature-snapshot.php @@ -6,6 +6,7 @@ */ use Cloudinary\Analytics; +use Cloudinary\Sync; use function Cloudinary\get_plugin_instance; /** @@ -117,6 +118,21 @@ protected function connect_as( $cloud_name ) { $plugin->get_component( 'connect' )->set_credentials( array( 'cloud_name' => $cloud_name ) ); } + /** + * Restores the plugin's install marker. + * + * @param string|false $db_version The value before the test. + * + * @return void + */ + protected function restore_db_version( $db_version ) { + if ( false === $db_version ) { + delete_option( Sync::META_KEYS['db_version'] ); + } else { + update_option( Sync::META_KEYS['db_version'], $db_version ); + } + } + /** * Returns the captured snapshot events. * @@ -147,9 +163,45 @@ public function test_sends_once_per_account() { $this->assertSame( 'features', $snapshots[0]['event_category'] ); $this->assertIsBool( $snapshots[0]['use_lazy_load'] ); $this->assertArrayNotHasKey( 'cloudinary_url', $snapshots[0] ); + $this->assertSame( 'upgrade', $snapshots[0]['trigger'] ); $this->assertSame( 'first-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); } + /** + * "Max images" is reported as the settings page shows it, derived from + * the other responsive fields, rather than as stored. + * + * @return void + */ + public function test_breakpoints_are_counted_like_the_settings_page() { + $settings = get_plugin_instance()->settings; + $original = array(); + $values = array( + 'max_width' => 2048, + 'min_width' => 200, + 'pixel_step' => 200, + 'breakpoints' => 42, + ); + foreach ( $values as $key => $value ) { + $original[ $key ] = $settings->get_value( $key ); + $settings->get_setting( $key )->set_value( $value ); + } + + $snapshot = $this->analytics->get_feature_snapshot(); + + $settings->get_setting( 'pixel_step' )->set_value( 500 ); + $coarser = $this->analytics->get_breakpoint_count(); + + foreach ( $original as $key => $value ) { + $settings->get_setting( $key )->set_value( $value ); + } + + // 2048, 1848, … 248. + $this->assertSame( 10, $snapshot['breakpoints'] ); + // 2048, 1548, 1048, 548. + $this->assertSame( 4, $coarser ); + } + /** * Switching to another account sends a new snapshot. * @@ -160,10 +212,63 @@ public function test_sends_again_after_an_account_switch() { $this->connect_as( 'second-cloud' ); $this->analytics->maybe_send_feature_snapshot(); - $this->assertCount( 2, $this->snapshots() ); + $snapshots = $this->snapshots(); + $this->assertCount( 2, $snapshots ); + $this->assertSame( 'setup', $snapshots[1]['trigger'] ); $this->assertSame( 'second-cloud', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); } + /** + * A site seen unconnected before it is connected sends a setup snapshot. + * + * @return void + */ + public function test_connecting_after_an_unconnected_admin_load_is_a_setup() { + get_plugin_instance()->settings->set_param( 'connected', false ); + $this->analytics->maybe_send_feature_snapshot(); + $this->connect_as( 'first-cloud' ); + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'setup', $snapshots[0]['trigger'] ); + } + + /** + * A fresh install's first snapshot is a setup, even when the site is + * connected from the start through the connection string constant. + * + * @return void + */ + public function test_fresh_install_is_a_setup() { + $db_version = get_option( Sync::META_KEYS['db_version'] ); + delete_option( Sync::META_KEYS['db_version'] ); + Analytics::stash_activation(); + delete_transient( Analytics::PENDING_ACTIVATION ); + $this->restore_db_version( $db_version ); + + $this->analytics->maybe_send_feature_snapshot(); + + $snapshots = $this->snapshots(); + $this->assertCount( 1, $snapshots ); + $this->assertSame( 'setup', $snapshots[0]['trigger'] ); + } + + /** + * An upgrade or reactivation doesn't mark the site as not set up. + * + * @return void + */ + public function test_upgrade_activation_leaves_the_flag_unset() { + $db_version = get_option( Sync::META_KEYS['db_version'] ); + update_option( Sync::META_KEYS['db_version'], '1.0.0' ); + Analytics::stash_activation(); + delete_transient( Analytics::PENDING_ACTIVATION ); + $this->restore_db_version( $db_version ); + + $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + } + /** * Nothing is sent before the site is connected. * @@ -174,7 +279,7 @@ public function test_not_sent_while_disconnected() { $this->analytics->maybe_send_feature_snapshot(); $this->assertCount( 0, $this->snapshots() ); - $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); + $this->assertSame( '', get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); } /** @@ -228,10 +333,27 @@ public function test_only_the_request_that_adds_the_flag_sends() { // Simulates a concurrent request having added the flag after this one // read it: the add_option() gate must fail and nothing be sent. add_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); - add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud' ); + add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud', '', true ); $this->analytics->maybe_send_feature_snapshot(); remove_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); $this->assertCount( 0, $this->snapshots() ); } + + /** + * When another request already claimed a site marked as not set up, this + * one doesn't send. + * + * @return void + */ + public function test_only_the_request_that_updates_the_flag_sends() { + // This request read the "not set up" marker, but a concurrent one has + // since stored the cloud name: the update_option() gate must fail. + add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud', '', true ); + add_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_empty_string' ); + $this->analytics->maybe_send_feature_snapshot(); + remove_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_empty_string' ); + + $this->assertCount( 0, $this->snapshots() ); + } } From 93526c471b45c142b20bc41fb2934ac97cf12238 Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Mon, 23 Nov 2026 08:58:45 +0100 Subject: [PATCH 6/8] feat(analytics): Send the full feature configuration with settings_saved Every settings save now carries the whole allowlisted feature configuration as flat fields, the same as feature_config_snapshot, next to page and changed_keys. Each save is then a baseline, and changed_keys shows which settings were deliberately overridden. This replaces the nested changed_values object. --- php/class-admin.php | 28 +++++++++++----------------- php/class-analytics.php | 2 +- tests/e2e/settings-analytics.spec.js | 17 +++++++++-------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/php/class-admin.php b/php/class-admin.php index 9bec1020b..674a254b2 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -488,25 +488,19 @@ protected function save_settings( $submission, $data ) { $analytics = $this->plugin->get_component( 'analytics' ); if ( $analytics ) { - $params = array( - 'page' => $submission, - 'changed_keys' => $changed_keys, + // The full feature configuration as it stands after the + // save, flat like `feature_config_snapshot`, so every save + // is a baseline and the changed keys show what was + // deliberately overridden. Only allowlisted settings are + // included: secrets and free text never leave the site. + $params = array_merge( + $analytics->get_feature_snapshot(), + array( + 'page' => $submission, + 'changed_keys' => $changed_keys, + ) ); - // Values only for allowlisted feature settings; secrets - // and free text never leave the site. Read back after the - // save so the value reflects any save filters. - $feature_values = array(); - foreach ( $changed_keys as $key ) { - $normalized = $analytics->normalize_feature_value( $submission, $key, $this->settings->get_value( $key ) ); - if ( null !== $normalized ) { - $feature_values[ $key ] = $normalized; - } - } - if ( ! empty( $feature_values ) ) { - $params['changed_values'] = $feature_values; - } - $analytics->track( 'settings_saved', 'settings', null, $params ); if ( 'gallery' === $submission ) { diff --git a/php/class-analytics.php b/php/class-analytics.php index 10f812ca7..cac217900 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -187,7 +187,7 @@ static function ( $crop ) { * Emits the one-time `feature_config_snapshot` adoption baseline. * * Captures the full state of the delivery features, including those left - * at their defaults, which `settings_saved` never reports. Sent on the + * at their defaults, for sites that never save their settings. Sent on the * first admin page load by an administrator once the site is connected, * then suppressed per cloud_name. That covers wizard setups, sites * connected through the `CLOUDINARY_CONNECTION_STRING` constant, existing diff --git a/tests/e2e/settings-analytics.spec.js b/tests/e2e/settings-analytics.spec.js index 4ea7ff984..4661d2180 100644 --- a/tests/e2e/settings-analytics.spec.js +++ b/tests/e2e/settings-analytics.spec.js @@ -79,9 +79,11 @@ test.describe( 'Settings & navigation analytics', () => { expect( savedEvents.length ).toBe( 1 ); expect( savedEvents[ 0 ].page ).toBe( 'image_settings' ); expect( savedEvents[ 0 ].changed_keys ).toContain( 'image_quality' ); - expect( savedEvents[ 0 ].changed_values.image_quality ).toBe( - nextValue - ); + expect( savedEvents[ 0 ].image_quality ).toBe( nextValue ); + // The whole feature configuration is sent, not just this page's. + expect( typeof savedEvents[ 0 ].use_lazy_load ).toBe( 'boolean' ); + expect( typeof savedEvents[ 0 ].breakpoints ).toBe( 'number' ); + expect( savedEvents[ 0 ] ).not.toHaveProperty( 'changed_values' ); } ); // One feature page each, flipping a field no other spec writes to, so @@ -104,8 +106,8 @@ test.describe( 'Settings & navigation analytics', () => { { page: 'responsive', tag: 'input', - field: 'breakpoints', - values: [ '10', '12' ], + field: 'pixel_step', + values: [ '200', '250' ], expected: ( value ) => Number( value ), }, ] ) { @@ -139,9 +141,8 @@ test.describe( 'Settings & navigation analytics', () => { ); expect( savedEvents.length ).toBe( 1 ); expect( savedEvents[ 0 ].page ).toBe( slug ); - expect( savedEvents[ 0 ].changed_values[ field ] ).toBe( - expected( nextValue ) - ); + expect( savedEvents[ 0 ].changed_keys ).toContain( field ); + expect( savedEvents[ 0 ][ field ] ).toBe( expected( nextValue ) ); } ); } From be65494331ebe0492a28da84d0862066e9a4f2ac Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Mon, 23 Nov 2026 09:39:57 +0100 Subject: [PATCH 7/8] fix(analytics): Only send known options and harden the feature values - Select and radio values are only sent when they are one of the setting's options, and as "other" otherwise, since they are only sanitised as text and filters can return anything. - The lazy-loading threshold is lowercased, so "100PX" is kept. - "Max images" is marked as derived in the allowlist, since the stored value is never sent. - settings_saved no longer builds the feature configuration while analytics are disabled. - The concurrency test for the first snapshot now really reaches the add_option() gate. - The snapshot docs now say it is re-sent on every account change, and where the setup/upgrade trigger can be wrong. --- php/class-admin.php | 2 +- php/class-analytics.php | 55 +++++++++++++------ .../tests/test-analytics-feature-snapshot.php | 16 ++++-- .../tests/test-analytics-feature-values.php | 31 ++++++++++- 4 files changed, 79 insertions(+), 25 deletions(-) diff --git a/php/class-admin.php b/php/class-admin.php index 674a254b2..e3ffb9260 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -487,7 +487,7 @@ protected function save_settings( $submission, $data ) { $this->add_admin_notice( 'error_notice', __( 'Settings updated successfully', 'cloudinary' ), 'success' ); $analytics = $this->plugin->get_component( 'analytics' ); - if ( $analytics ) { + if ( $analytics && $analytics->is_enabled() ) { // The full feature configuration as it stands after the // save, flat like `feature_config_snapshot`, so every save // is a baseline and the changed keys show what was diff --git a/php/class-analytics.php b/php/class-analytics.php index cac217900..27c6ee9d8 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -91,18 +91,21 @@ public function __construct( Plugin $plugin ) { * * - bool: `on`/`off` toggle → true/false. * - int: numeric field → int. - * - string: select/radio enum → string. - * - css_length: free-text CSS length → the length, or dropped when it is - * anything else. + * - enum: select/radio → the value when it is one of the setting's + * own options, `other` when it isn't. + * - css_length: free-text CSS length → the length, lowercased, or dropped + * when it is anything else. * - presence: free text → whether it is non-empty. * - crops: per-size crop map → number of sizes with a custom crop. + * - derived: not sent as stored; get_feature_snapshot() works the value + * out from other settings. */ const FEATURE_SETTINGS = array( 'image_settings' => array( 'image_delivery' => 'bool', 'image_optimization' => 'bool', - 'image_format' => 'string', - 'image_quality' => 'string', + 'image_format' => 'enum', + 'image_quality' => 'enum', 'image_freeform' => 'presence', 'svg_support' => 'bool', 'crop_sizes' => 'crops', @@ -110,28 +113,28 @@ public function __construct( Plugin $plugin ) { 'lazy_loading' => array( 'use_lazy_load' => 'bool', 'lazy_threshold' => 'css_length', - 'lazy_placeholder' => 'string', + 'lazy_placeholder' => 'enum', 'lazy_animate' => 'bool', - 'dpr' => 'string', + 'dpr' => 'enum', ), 'responsive' => array( 'enable_breakpoints' => 'bool', 'pixel_step' => 'int', - 'breakpoints' => 'int', + 'breakpoints' => 'derived', 'max_width' => 'int', 'min_width' => 'int', ), 'video_settings' => array( 'video_delivery' => 'bool', - 'video_player' => 'string', + 'video_player' => 'enum', 'adaptive_streaming' => 'bool', - 'adaptive_streaming_mode' => 'string', + 'adaptive_streaming_mode' => 'enum', 'video_controls' => 'bool', 'video_loop' => 'bool', - 'video_autoplay_mode' => 'string', + 'video_autoplay_mode' => 'enum', 'video_optimization' => 'bool', - 'video_format' => 'string', - 'video_quality' => 'string', + 'video_format' => 'enum', + 'video_quality' => 'enum', 'video_freeform' => 'presence', ), ); @@ -160,6 +163,8 @@ public function normalize_feature_value( $page, $key, $value ) { case 'css_length': $value = is_string( $value ) ? trim( $value ) : ''; + $value = strtolower( $value ); + return preg_match( '/^\d+(\.\d+)?(px|%|r?em|vh|vw)?$/', $value ) ? $value : null; case 'presence': return is_string( $value ) && '' !== trim( $value ); @@ -178,8 +183,17 @@ static function ( $crop ) { } ) ); + case 'enum': + // Select and radio values are only sanitised as text, and + // filters can return anything, so only known options are sent. + if ( ! is_scalar( $value ) ) { + return null; + } + $options = $this->plugin->settings->get_setting( $key )->get_param( 'options' ); + + return is_array( $options ) && array_key_exists( (string) $value, $options ) ? (string) $value : 'other'; default: - return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : null; + return null; } } @@ -189,9 +203,11 @@ static function ( $crop ) { * Captures the full state of the delivery features, including those left * at their defaults, for sites that never save their settings. Sent on the * first admin page load by an administrator once the site is connected, - * then suppressed per cloud_name. That covers wizard setups, sites - * connected through the `CLOUDINARY_CONNECTION_STRING` constant, existing - * installs upgrading to this version, and account switches. + * then again whenever the connected cloud_name changes (only the last one + * is kept, so switching A → B → A sends three). That covers wizard + * setups, sites connected through the `CLOUDINARY_CONNECTION_STRING` + * constant, existing installs upgrading to this version, and account + * switches. * * The `trigger` param tells the two cases apart: * @@ -201,6 +217,11 @@ static function ( $crop ) { * - `upgrade`: the site was already connected the first time a version * that sends the snapshot ran, so the flag was never set. * + * A site connected through the constant before any admin page loads is + * only recognised as a setup on a fresh install. A reinstall after the + * uninstall clean-up (which keeps the plugin's DB version) and subsites of + * a network activation report `upgrade` instead. + * * @return void */ public function maybe_send_feature_snapshot() { diff --git a/tests/phpunit/tests/test-analytics-feature-snapshot.php b/tests/phpunit/tests/test-analytics-feature-snapshot.php index 24d97c660..f207c7ec8 100644 --- a/tests/phpunit/tests/test-analytics-feature-snapshot.php +++ b/tests/phpunit/tests/test-analytics-feature-snapshot.php @@ -330,12 +330,20 @@ public function test_not_sent_during_ajax_requests() { * @return void */ public function test_only_the_request_that_adds_the_flag_sends() { - // Simulates a concurrent request having added the flag after this one - // read it: the add_option() gate must fail and nothing be sent. - add_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); + // A concurrent request has stored the flag, but this one's option + // caches were loaded before that: get_option() sees no flag, and + // add_option() skips its own existence check, so its upsert hits the + // existing row. The upsert must change nothing and nothing be sent. add_option( Analytics::FEATURE_SNAPSHOT_FLAG, 'first-cloud', '', true ); + $alloptions = wp_load_alloptions(); + unset( $alloptions[ Analytics::FEATURE_SNAPSHOT_FLAG ] ); + wp_cache_set( 'alloptions', $alloptions, 'options' ); + wp_cache_set( 'notoptions', array( Analytics::FEATURE_SNAPSHOT_FLAG => true ), 'options' ); + + $this->assertFalse( get_option( Analytics::FEATURE_SNAPSHOT_FLAG ) ); $this->analytics->maybe_send_feature_snapshot(); - remove_filter( 'pre_option_' . Analytics::FEATURE_SNAPSHOT_FLAG, '__return_false' ); + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); $this->assertCount( 0, $this->snapshots() ); } diff --git a/tests/phpunit/tests/test-analytics-feature-values.php b/tests/phpunit/tests/test-analytics-feature-values.php index f205e0f83..7d7b32d9c 100644 --- a/tests/phpunit/tests/test-analytics-feature-values.php +++ b/tests/phpunit/tests/test-analytics-feature-values.php @@ -79,17 +79,41 @@ public function test_toggles_become_booleans() { */ public function test_numbers_become_ints() { $this->assertSame( 200, $this->analytics->normalize_feature_value( 'responsive', 'pixel_step', '200' ) ); - $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'breakpoints', '' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'pixel_step', '' ) ); } /** - * Enum values pass through as strings. + * Select and radio values pass through when they are one of the + * setting's options. * * @return void */ - public function test_enums_stay_strings() { + public function test_known_options_stay_strings() { $this->assertSame( 'avif', $this->analytics->normalize_feature_value( 'image_settings', 'image_format', 'avif' ) ); $this->assertSame( 'auto:good', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', 'auto:good' ) ); + $this->assertSame( '80', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', 80 ) ); + $this->assertSame( 'on-scroll', $this->analytics->normalize_feature_value( 'video_settings', 'video_autoplay_mode', 'on-scroll' ) ); + } + + /** + * Select and radio values that aren't one of the setting's options are + * reported as `other`, so stored free text never leaves the site. + * + * @return void + */ + public function test_unknown_options_become_other() { + $this->assertSame( 'other', $this->analytics->normalize_feature_value( 'image_settings', 'image_quality', "Jane's shop" ) ); + $this->assertSame( 'other', $this->analytics->normalize_feature_value( 'lazy_loading', 'dpr', '3X' ) ); + $this->assertNull( $this->analytics->normalize_feature_value( 'image_settings', 'image_format', array( 'avif' ) ) ); + } + + /** + * "Max images" is never sent as stored. + * + * @return void + */ + public function test_derived_settings_are_not_normalised() { + $this->assertNull( $this->analytics->normalize_feature_value( 'responsive', 'breakpoints', '12' ) ); } /** @@ -101,6 +125,7 @@ public function test_lazy_threshold_only_keeps_css_lengths() { $this->assertSame( '100px', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', ' 100px ' ) ); $this->assertSame( '1.5em', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '1.5em' ) ); $this->assertSame( '20%', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '20%' ) ); + $this->assertSame( '100px', $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '100PX' ) ); $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', 'jane@example.com' ) ); $this->assertNull( $this->analytics->normalize_feature_value( 'lazy_loading', 'lazy_threshold', '' ) ); } From 90514989259c1b95dcf5bbcbfcb36a407d7d736c Mon Sep 17 00:00:00 2001 From: Gabriel de Tassigny Date: Mon, 23 Nov 2026 14:29:54 +0100 Subject: [PATCH 8/8] fix(analytics): Only attach the feature configuration to feature page saves Saves of the connection, sync, gallery or extensions pages can't change the feature configuration, so their settings_saved events no longer carry it. Also documents one more race where the snapshot trigger can be upgrade instead of setup, corrects the comment on the flag's query cost, and restores shared settings in a finally block in the breakpoints test. --- php/class-admin.php | 25 +++++++++-------- php/class-analytics.php | 10 +++++-- tests/e2e/features-analytics.spec.js | 10 +++++++ .../tests/test-analytics-feature-snapshot.php | 27 ++++++++++--------- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/php/class-admin.php b/php/class-admin.php index e3ffb9260..0b213acf9 100644 --- a/php/class-admin.php +++ b/php/class-admin.php @@ -488,19 +488,22 @@ protected function save_settings( $submission, $data ) { $analytics = $this->plugin->get_component( 'analytics' ); if ( $analytics && $analytics->is_enabled() ) { - // The full feature configuration as it stands after the - // save, flat like `feature_config_snapshot`, so every save - // is a baseline and the changed keys show what was - // deliberately overridden. Only allowlisted settings are - // included: secrets and free text never leave the site. - $params = array_merge( - $analytics->get_feature_snapshot(), - array( - 'page' => $submission, - 'changed_keys' => $changed_keys, - ) + $params = array( + 'page' => $submission, + 'changed_keys' => $changed_keys, ); + // Saves of a feature page carry the full feature + // configuration as it stands after the save, flat like + // `feature_config_snapshot`, so every such save is a + // baseline and the changed keys show what was deliberately + // overridden. Other pages can't change it, so it is left + // out. Only allowlisted settings are included: secrets and + // free text never leave the site. + if ( isset( Analytics::FEATURE_SETTINGS[ $submission ] ) ) { + $params = array_merge( $analytics->get_feature_snapshot(), $params ); + } + $analytics->track( 'settings_saved', 'settings', null, $params ); if ( 'gallery' === $submission ) { diff --git a/php/class-analytics.php b/php/class-analytics.php index 27c6ee9d8..ac5ad4bc8 100644 --- a/php/class-analytics.php +++ b/php/class-analytics.php @@ -220,7 +220,10 @@ static function ( $crop ) { * A site connected through the constant before any admin page loads is * only recognised as a setup on a fresh install. A reinstall after the * uninstall clean-up (which keeps the plugin's DB version) and subsites of - * a network activation report `upgrade` instead. + * a network activation report `upgrade` instead. So can a site whose + * unconnected admin load writes the "not set up" marker while another + * admin load, already connected, is claiming the send: that request + * cached the flag as missing, so its add_option() overwrites the marker. * * @return void */ @@ -231,7 +234,10 @@ public function maybe_send_feature_snapshot() { return; } - // Autoloaded, so this check costs no query on later admin loads. + // Autoloaded, so once the flag is stored this check costs no query. + // It isn't stored while a connected site has analytics disabled, so + // the snapshot is still sent with the right trigger once they are + // enabled: each admin load then runs one option query. $sent = get_option( self::FEATURE_SNAPSHOT_FLAG ); $connect = $this->plugin->get_component( 'connect' ); diff --git a/tests/e2e/features-analytics.spec.js b/tests/e2e/features-analytics.spec.js index e034ecef7..c007c8ff9 100644 --- a/tests/e2e/features-analytics.spec.js +++ b/tests/e2e/features-analytics.spec.js @@ -80,6 +80,16 @@ test.describe( 'Extensions & gallery analytics', () => { expect( events.length ).toBe( 1 ); expect( events[ 0 ].layout ).toBe( 'expand' ); expect( events[ 0 ].media_count ).toBe( 2 ); + + // The gallery page can't change the feature configuration, so its + // settings_saved event doesn't carry it. + const saved = findAnalyticsEvents( + readAnalyticsEvents(), + 'settings_saved' + ); + expect( saved.length ).toBe( 1 ); + expect( saved[ 0 ].page ).toBe( 'gallery' ); + expect( saved[ 0 ] ).not.toHaveProperty( 'use_lazy_load' ); } ); } ); diff --git a/tests/phpunit/tests/test-analytics-feature-snapshot.php b/tests/phpunit/tests/test-analytics-feature-snapshot.php index f207c7ec8..4cc0a0ce4 100644 --- a/tests/phpunit/tests/test-analytics-feature-snapshot.php +++ b/tests/phpunit/tests/test-analytics-feature-snapshot.php @@ -182,18 +182,21 @@ public function test_breakpoints_are_counted_like_the_settings_page() { 'pixel_step' => 200, 'breakpoints' => 42, ); - foreach ( $values as $key => $value ) { - $original[ $key ] = $settings->get_value( $key ); - $settings->get_setting( $key )->set_value( $value ); - } - - $snapshot = $this->analytics->get_feature_snapshot(); - - $settings->get_setting( 'pixel_step' )->set_value( 500 ); - $coarser = $this->analytics->get_breakpoint_count(); - - foreach ( $original as $key => $value ) { - $settings->get_setting( $key )->set_value( $value ); + try { + foreach ( $values as $key => $value ) { + $original[ $key ] = $settings->get_value( $key ); + $settings->get_setting( $key )->set_value( $value ); + } + + $snapshot = $this->analytics->get_feature_snapshot(); + + $settings->get_setting( 'pixel_step' )->set_value( 500 ); + $coarser = $this->analytics->get_breakpoint_count(); + } finally { + // The settings live on the plugin singleton, shared by every test. + foreach ( $original as $key => $value ) { + $settings->get_setting( $key )->set_value( $value ); + } } // 2048, 1848, … 248.