diff --git a/changelog/woopmnt-4451-update-shopper-tracks-events-enablement-criteria b/changelog/woopmnt-4451-update-shopper-tracks-events-enablement-criteria new file mode 100644 index 00000000000..464fd28b84e --- /dev/null +++ b/changelog/woopmnt-4451-update-shopper-tracks-events-enablement-criteria @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Respect merchant tracking preferences for shopper events diff --git a/client/tracks/index.ts b/client/tracks/index.ts index 0d115a3a67d..6d0409be0da 100644 --- a/client/tracks/index.ts +++ b/client/tracks/index.ts @@ -71,6 +71,14 @@ export const recordUserEvent = ( eventName: ShopperEvent, eventProperties: Record< string, unknown > = {} ): void => { + // Don't send tracking request if tracking is disabled on the server. + const isShopperTrackingEnabled = + getConfig( 'isShopperTrackingEnabled' ) ?? + getExpressCheckoutConfig( 'is_shopper_tracking_enabled' ); + if ( isShopperTrackingEnabled === false ) { + return; + } + const nonce = getConfig( 'platformTrackerNonce' ) ?? getExpressCheckoutConfig( 'nonce' )?.platform_tracker; diff --git a/includes/class-wc-payments-checkout.php b/includes/class-wc-payments-checkout.php index dc2e60e46a8..11f2f833019 100644 --- a/includes/class-wc-payments-checkout.php +++ b/includes/class-wc-payments-checkout.php @@ -207,6 +207,7 @@ public function get_payment_fields_js_config() { 'isWooPayGlobalThemeSupportEnabled' => $this->gateway->is_woopay_global_theme_support_enabled(), 'woopayHost' => WooPay_Utilities::get_woopay_url(), 'platformTrackerNonce' => wp_create_nonce( 'platform_tracks_nonce' ), + 'isShopperTrackingEnabled' => apply_filters( 'wcpay_shopper_tracking_enabled', 'no' !== get_option( 'woocommerce_allow_tracking' ) ), 'accountIdForIntentConfirmation' => apply_filters( 'wc_payments_account_id_for_intent_confirmation', '' ), 'wcpayVersionNumber' => WCPAY_VERSION_NUMBER, 'woopaySignatureNonce' => wp_create_nonce( 'woopay_signature_nonce' ), diff --git a/includes/class-woopay-tracker.php b/includes/class-woopay-tracker.php index a3b9207b686..b321cc7117b 100644 --- a/includes/class-woopay-tracker.php +++ b/includes/class-woopay-tracker.php @@ -217,6 +217,17 @@ public function is_country_tracks_eligible() { * @return bool */ public function should_enable_tracking( $is_admin_event = false, $track_on_all_stores = false ) { + // Allow merchants to disable all shopper tracking via filter. + if ( ! apply_filters( 'wcpay_shopper_tracking_enabled', true ) ) { + return false; + } + + // Respect WooCommerce global tracking opt-out setting. + // Only disable if explicitly set to 'no' (not false, null, or empty). + $allow_tracking = get_option( 'woocommerce_allow_tracking', '' ); + if ( 'no' === $allow_tracking ) { + return false; + } // Don't track if the gateway is not enabled. $gateway = \WC_Payments::get_gateway(); diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php index 2004e5e6261..0f36fb9d079 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-handler.php @@ -226,16 +226,17 @@ public function get_express_checkout_params() { apply_filters( 'wcpay_express_checkout_js_params', [ - 'ajax_url' => admin_url( 'admin-ajax.php' ), - 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), - 'nonce' => [ + 'ajax_url' => admin_url( 'admin-ajax.php' ), + 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), + 'is_shopper_tracking_enabled' => apply_filters( 'wcpay_shopper_tracking_enabled', 'no' !== get_option( 'woocommerce_allow_tracking' ) ), + 'nonce' => [ 'platform_tracker' => wp_create_nonce( 'platform_tracks_nonce' ), // needed to communicate via the Store API. 'tokenized_cart_nonce' => wp_create_nonce( 'woopayments_tokenized_cart_nonce' ), 'tokenized_cart_session_nonce' => wp_create_nonce( 'woopayments_tokenized_cart_session_nonce' ), 'store_api_nonce' => wp_create_nonce( 'wc_store_api' ), ], - 'checkout' => [ + 'checkout' => [ 'currency_code' => strtolower( get_woocommerce_currency() ), 'currency_decimals' => WC_Payments::get_localization_service()->get_currency_format( get_woocommerce_currency() )['num_decimals'], 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), @@ -245,12 +246,12 @@ public function get_express_checkout_params() { 'allowed_shipping_countries' => array_keys( WC()->countries->get_shipping_countries() ?? [] ), 'display_prices_with_tax' => 'incl' === get_option( 'woocommerce_tax_display_cart' ), ], - 'button' => $this->get_button_settings(), - 'login_confirmation' => $this->get_login_confirmation_settings(), - 'button_context' => $this->express_checkout_helper->get_button_context(), - 'has_block' => has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ), - 'product' => $this->express_checkout_helper->get_product_data(), - 'store_name' => get_bloginfo( 'name' ), + 'button' => $this->get_button_settings(), + 'login_confirmation' => $this->get_login_confirmation_settings(), + 'button_context' => $this->express_checkout_helper->get_button_context(), + 'has_block' => has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ), + 'product' => $this->express_checkout_helper->get_product_data(), + 'store_name' => get_bloginfo( 'name' ), ] ), [ diff --git a/tests/unit/test-class-woopay-tracker.php b/tests/unit/test-class-woopay-tracker.php index ba82c272c17..89afa537094 100644 --- a/tests/unit/test-class-woopay-tracker.php +++ b/tests/unit/test-class-woopay-tracker.php @@ -51,6 +51,7 @@ public function setUp(): void { WC_Payments::get_gateway()->enable(); $this->mock_account = $this->createMock( WC_Payments_Account::class ); + update_option( 'woocommerce_allow_tracking', 'yes' ); } public function tearDown(): void {