Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Respect merchant tracking preferences for shopper events
8 changes: 8 additions & 0 deletions client/tracks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-payments-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
11 changes: 11 additions & 0 deletions includes/class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +220 to +230
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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;
}
// Allow merchants to disable all shopper tracking via filter.
if ( ! apply_filters( 'wcpay_shopper_tracking_enabled', 'no' !== get_option( 'woocommerce_allow_tracking', '' ) ) ) {
return false;
}

I think maybe also this can use the same filter structure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frosso my idea was to create precedence between the filter and the option, allowing the filter to override the option value. Let me know if you feel this is too extra.


// Don't track if the gateway is not enabled.
$gateway = \WC_Payments::get_gateway();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand All @@ -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' ),
]
),
[
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test-class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading