Skip to content

Commit 87f56c7

Browse files
authored
Respect merchant tracking preferences for shopper events (#11188)
1 parent 330a931 commit 87f56c7

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Respect merchant tracking preferences for shopper events

client/tracks/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export const recordUserEvent = (
7171
eventName: ShopperEvent,
7272
eventProperties: Record< string, unknown > = {}
7373
): void => {
74+
// Don't send tracking request if tracking is disabled on the server.
75+
const isShopperTrackingEnabled =
76+
getConfig( 'isShopperTrackingEnabled' ) ??
77+
getExpressCheckoutConfig( 'is_shopper_tracking_enabled' );
78+
if ( isShopperTrackingEnabled === false ) {
79+
return;
80+
}
81+
7482
const nonce =
7583
getConfig( 'platformTrackerNonce' ) ??
7684
getExpressCheckoutConfig( 'nonce' )?.platform_tracker;

includes/class-wc-payments-checkout.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function get_payment_fields_js_config() {
207207
'isWooPayGlobalThemeSupportEnabled' => $this->gateway->is_woopay_global_theme_support_enabled(),
208208
'woopayHost' => WooPay_Utilities::get_woopay_url(),
209209
'platformTrackerNonce' => wp_create_nonce( 'platform_tracks_nonce' ),
210+
'isShopperTrackingEnabled' => apply_filters( 'wcpay_shopper_tracking_enabled', 'no' !== get_option( 'woocommerce_allow_tracking' ) ),
210211
'accountIdForIntentConfirmation' => apply_filters( 'wc_payments_account_id_for_intent_confirmation', '' ),
211212
'wcpayVersionNumber' => WCPAY_VERSION_NUMBER,
212213
'woopaySignatureNonce' => wp_create_nonce( 'woopay_signature_nonce' ),

includes/class-woopay-tracker.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ public function is_country_tracks_eligible() {
217217
* @return bool
218218
*/
219219
public function should_enable_tracking( $is_admin_event = false, $track_on_all_stores = false ) {
220+
// Allow merchants to disable all shopper tracking via filter.
221+
if ( ! apply_filters( 'wcpay_shopper_tracking_enabled', true ) ) {
222+
return false;
223+
}
224+
225+
// Respect WooCommerce global tracking opt-out setting.
226+
// Only disable if explicitly set to 'no' (not false, null, or empty).
227+
$allow_tracking = get_option( 'woocommerce_allow_tracking', '' );
228+
if ( 'no' === $allow_tracking ) {
229+
return false;
230+
}
220231

221232
// Don't track if the gateway is not enabled.
222233
$gateway = \WC_Payments::get_gateway();

includes/express-checkout/class-wc-payments-express-checkout-button-handler.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,17 @@ public function get_express_checkout_params() {
226226
apply_filters(
227227
'wcpay_express_checkout_js_params',
228228
[
229-
'ajax_url' => admin_url( 'admin-ajax.php' ),
230-
'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
231-
'nonce' => [
229+
'ajax_url' => admin_url( 'admin-ajax.php' ),
230+
'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
231+
'is_shopper_tracking_enabled' => apply_filters( 'wcpay_shopper_tracking_enabled', 'no' !== get_option( 'woocommerce_allow_tracking' ) ),
232+
'nonce' => [
232233
'platform_tracker' => wp_create_nonce( 'platform_tracks_nonce' ),
233234
// needed to communicate via the Store API.
234235
'tokenized_cart_nonce' => wp_create_nonce( 'woopayments_tokenized_cart_nonce' ),
235236
'tokenized_cart_session_nonce' => wp_create_nonce( 'woopayments_tokenized_cart_session_nonce' ),
236237
'store_api_nonce' => wp_create_nonce( 'wc_store_api' ),
237238
],
238-
'checkout' => [
239+
'checkout' => [
239240
'currency_code' => strtolower( get_woocommerce_currency() ),
240241
'currency_decimals' => WC_Payments::get_localization_service()->get_currency_format( get_woocommerce_currency() )['num_decimals'],
241242
'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ),
@@ -245,12 +246,12 @@ public function get_express_checkout_params() {
245246
'allowed_shipping_countries' => array_keys( WC()->countries->get_shipping_countries() ?? [] ),
246247
'display_prices_with_tax' => 'incl' === get_option( 'woocommerce_tax_display_cart' ),
247248
],
248-
'button' => $this->get_button_settings(),
249-
'login_confirmation' => $this->get_login_confirmation_settings(),
250-
'button_context' => $this->express_checkout_helper->get_button_context(),
251-
'has_block' => has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ),
252-
'product' => $this->express_checkout_helper->get_product_data(),
253-
'store_name' => get_bloginfo( 'name' ),
249+
'button' => $this->get_button_settings(),
250+
'login_confirmation' => $this->get_login_confirmation_settings(),
251+
'button_context' => $this->express_checkout_helper->get_button_context(),
252+
'has_block' => has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ),
253+
'product' => $this->express_checkout_helper->get_product_data(),
254+
'store_name' => get_bloginfo( 'name' ),
254255
]
255256
),
256257
[

tests/unit/test-class-woopay-tracker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function setUp(): void {
5151
WC_Payments::get_gateway()->enable();
5252

5353
$this->mock_account = $this->createMock( WC_Payments_Account::class );
54+
update_option( 'woocommerce_allow_tracking', 'yes' );
5455
}
5556

5657
public function tearDown(): void {

0 commit comments

Comments
 (0)