Commit d03ac2b56c1 for woocommerce
commit d03ac2b56c1f508c91c4386d3e5f807fd0975f7b
Author: Luigi Teschio <gigitux@gmail.com>
Date: Thu Jul 16 09:54:31 2026 +0200
Fix legacy Select2 storefront usage tracking (#66652)
* Fix legacy Select2 storefront usage tracking
* Add changelog entry for legacy Select2 tracking fix
diff --git a/plugins/woocommerce/changelog/fix-legacy-select2-frontend-analytics b/plugins/woocommerce/changelog/fix-legacy-select2-frontend-analytics
new file mode 100644
index 00000000000..d8adfe6a86b
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-legacy-select2-frontend-analytics
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Use WooCommerce Analytics to report legacy Select2 usage on storefront pages.
diff --git a/plugins/woocommerce/src/Internal/LegacyAssets/LegacySelect2UsageTracker.php b/plugins/woocommerce/src/Internal/LegacyAssets/LegacySelect2UsageTracker.php
index f2b8d8de0fd..79c2167663e 100644
--- a/plugins/woocommerce/src/Internal/LegacyAssets/LegacySelect2UsageTracker.php
+++ b/plugins/woocommerce/src/Internal/LegacyAssets/LegacySelect2UsageTracker.php
@@ -7,6 +7,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\LegacyAssets;
+use Automattic\Woocommerce_Analytics\WC_Analytics_Tracking;
use Automattic\WooCommerce\Internal\RegisterHooksInterface;
use WP_Scripts;
use WC_Site_Tracking;
@@ -38,9 +39,10 @@ class LegacySelect2UsageTracker implements RegisterHooksInterface {
* @since 11.0.0
*/
public function register() {
+ add_action( 'wp_print_footer_scripts', array( $this, 'handle_wp_print_footer_scripts' ), PHP_INT_MAX );
+
if ( WC_Site_Tracking::is_tracking_enabled() ) {
add_action( 'admin_print_footer_scripts', array( $this, 'handle_admin_print_footer_scripts' ), PHP_INT_MAX );
- add_action( 'wp_print_footer_scripts', array( $this, 'handle_wp_print_footer_scripts' ), PHP_INT_MAX );
}
}
@@ -63,6 +65,10 @@ class LegacySelect2UsageTracker implements RegisterHooksInterface {
* @return void
*/
public function handle_wp_print_footer_scripts(): void {
+ if ( ! $this->is_frontend_tracking_available() ) {
+ return;
+ }
+
$this->track_usage( self::CONTEXT_FRONTEND );
}
@@ -87,6 +93,12 @@ class LegacySelect2UsageTracker implements RegisterHooksInterface {
}
$this->mark_recently_checked( $event );
+
+ if ( self::CONTEXT_FRONTEND === $context ) {
+ $this->record_frontend_event( self::EVENT_NAME, $event );
+ return;
+ }
+
$this->record_event( self::EVENT_NAME, $event );
}
@@ -182,6 +194,34 @@ class LegacySelect2UsageTracker implements RegisterHooksInterface {
\WC_Tracks::record_event( $event_name, $properties );
}
+ /**
+ * Add a storefront event to the WooCommerce Analytics client queue.
+ *
+ * @param string $event_name Event name.
+ * @param array<string, string> $properties Event properties.
+ * @return void
+ *
+ * @since 11.0.0
+ */
+ protected function record_frontend_event( string $event_name, array $properties ): void {
+ if ( ! class_exists( WC_Analytics_Tracking::class ) ) {
+ return;
+ }
+
+ WC_Analytics_Tracking::add_event_to_queue( $event_name, $properties );
+ }
+
+ /**
+ * Whether the WooCommerce Analytics storefront tracker is initialized.
+ *
+ * @return bool
+ *
+ * @since 11.0.0
+ */
+ protected function is_frontend_tracking_available(): bool {
+ return did_action( 'woocommerce_analytics_init' ) && class_exists( WC_Analytics_Tracking::class );
+ }
+
/**
* Whether this usage event was already checked recently.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php b/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
index 44621681346..882776c9f18 100644
--- a/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
@@ -258,6 +258,28 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
$this->assertSame( array(), $this->sut->get_usage_event( 'frontend' ), 'Registered scripts should not be reported until they are enqueued.' );
}
+ /**
+ * @testdox Should register frontend analytics independently of the core Tracks opt-in.
+ */
+ public function test_registers_frontend_analytics_independently_of_core_tracks(): void {
+ add_filter( 'woocommerce_apply_user_tracking', '__return_false' );
+
+ $this->sut->register();
+
+ $this->assertSame(
+ PHP_INT_MAX,
+ has_action( 'wp_print_footer_scripts', array( $this->sut, 'handle_wp_print_footer_scripts' ) ),
+ 'Frontend analytics should be registered when core Tracks is disabled.'
+ );
+ $this->assertFalse(
+ has_action( 'admin_print_footer_scripts', array( $this->sut, 'handle_admin_print_footer_scripts' ) ),
+ 'Admin tracking should remain disabled when core Tracks is disabled.'
+ );
+
+ remove_action( 'wp_print_footer_scripts', array( $this->sut, 'handle_wp_print_footer_scripts' ), PHP_INT_MAX );
+ remove_filter( 'woocommerce_apply_user_tracking', '__return_false' );
+ }
+
/**
* @testdox Should record each detected usage event only once per week.
*/
@@ -284,11 +306,11 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
public int $usage_event_calls = 0;
/**
- * Recorded Tracks events.
+ * Recorded frontend analytics events.
*
* @var array<int, array{name: string, properties: array<string, string>}>
*/
- public array $recorded_events = array();
+ public array $recorded_frontend_events = array();
/**
* Get a legacy Select2 usage event for the current script registry.
@@ -305,18 +327,27 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
}
/**
- * Record a Tracks event.
+ * Add a storefront event to the WooCommerce Analytics client queue.
*
* @param string $event_name Event name.
* @param array<string, string> $properties Event properties.
* @return void
*/
- protected function record_event( string $event_name, array $properties ): void {
- $this->recorded_events[] = array(
+ protected function record_frontend_event( string $event_name, array $properties ): void {
+ $this->recorded_frontend_events[] = array(
'name' => $event_name,
'properties' => $properties,
);
}
+
+ /**
+ * Make frontend analytics available for this test double.
+ *
+ * @return bool
+ */
+ protected function is_frontend_tracking_available(): bool {
+ return true;
+ }
};
wp_register_script(
@@ -342,7 +373,7 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
'properties' => $event,
),
),
- $sut->recorded_events,
+ $sut->recorded_frontend_events,
'Repeated detections of the same legacy Select2 usage event should be rate limited.'
);
$this->assertSame( 2, $sut->usage_event_calls, 'Each detection should scan the script registry before rate limiting the exact usage event.' );