Skip to content

Conversation

@rtio
Copy link
Member

@rtio rtio commented Dec 5, 2025

Fixes #9677

Note: This PR builds on top of Phase 1 (#11188) and should be merged after it.

Changes proposed in this Pull Request

This PR implements request batching for shopper tracking events to dramatically reduce server load on high-traffic stores. This is Phase 2 of the solution for WOOPMNT-4451.

Problem:
Each shopper tracking event generates a separate Ajax request to /wp-admin/admin-ajax.php. For high-traffic stores, this creates significant server load:

  • 5-10+ separate requests per checkout session
  • Each request loads the full WordPress admin context
  • During promotions, this compounds exponentially
  • Customer reported this as a major issue during high-traffic periods

Solution:

Client-side batching:

  • Queue tracking events instead of sending immediately
  • Debounced flush after 2 seconds of inactivity
  • Batch multiple events into single request
  • Immediate flush on page unload to prevent data loss
  • Preserve accurate event timestamps from client

Server-side batch handler:

  • New platform_tracks_batch Ajax action
  • Process multiple events in single handler
  • Support client-provided timestamps (no data loss)
  • Efficient validation and processing

Express checkout improvements:

  • Add tracking enablement check for Apple/Google Pay events
  • Respect wcpaySettings.enableShopperTracking flag
  • Prevent hardcoded tracking when opt-out is enabled

Why this approach:

  • Minimal code changes, leverages existing infrastructure
  • Debouncing groups rapid events naturally (button clicks, page views)
  • Page unload handler ensures no events are lost
  • Client timestamps preserve accurate event timing
  • Backward compatible (falls back to individual requests if needed)

Trade-offs:

  • 2-second delay before events are sent (acceptable for analytics)
  • Slightly more complex client-side code
  • Additional server endpoint to maintain

How this code can break:

  • Page unload handler might not fire in all browsers (rare edge case)
  • Debounce timing could affect real-time dashboards
  • Batched requests could timeout if queue gets too large

What we're doing to prevent breakage:

  • Comprehensive unit tests for batch handler
  • Error handling for failed batch requests
  • beforeunload handler for reliable flushing
  • Timestamps preserve event accuracy
  • Existing single-event endpoint still works

Testing instructions

Test 1: Verify batching reduces requests

  1. Open browser DevTools → Network tab
  2. Filter for admin-ajax.php
  3. Visit product page, add to cart, go to checkout (multiple tracking events)
  4. Wait 2 seconds after last action
  5. Verify you see:
    • ONE request to action=platform_tracks_batch instead of 5+ separate requests
    • Request payload contains multiple events with timestamps

Test 2: Verify events are still tracked correctly

  1. Complete a checkout with batching enabled
  2. Verify all expected events are recorded (product view, cart view, checkout view, etc.)
  3. Check that event timestamps are accurate (not all the same server time)

Test 3: Test page unload flushing

  1. Visit product page (triggers event)
  2. Immediately navigate away (before 2-second debounce)
  3. Check network tab - event should be sent immediately on navigation
  4. Verify no events are lost

Test 4: Express checkout tracking respects opt-out

  1. Add filter: add_filter( 'wcpay_enable_shopper_tracking', '__return_false' );
  2. Load a page with Apple Pay / Google Pay buttons
  3. Click the buttons
  4. Verify NO tracking requests are made (previously these were hardcoded)

Test 5: Backward compatibility

  1. Verify old platform_tracks endpoint still works
  2. Test with and without batching
  3. Ensure no JS errors in console

Test 6: Unit tests

npm run test:php -- --filter=WooPay_Tracker_Test

Test 7: High-traffic simulation

  1. Rapidly click around site (product → cart → checkout)
  2. Verify events are batched appropriately
  3. Check server load is reduced vs individual requests

Expected Results:

  • 70-85% reduction in tracking-related Ajax requests
  • 5-10 requests reduced to 1-2 batched requests per session
  • All events still accurately recorded with correct timestamps
  • No impact on user experience

  • Run npm run changelog to add a changelog file, choose patch to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.
  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Post merge

Client-side changes:
- Batch multiple tracking events into single requests
- Use debounced flush (2s) to group rapid events
- Flush immediately on page unload to prevent data loss
- Preserve accurate event timestamps from client

Server-side changes:
- Add platform_tracks_batch Ajax action to handle batch requests
- Support custom client timestamps in tracks_build_event_obj
- Process multiple events efficiently in single handler

Express checkout:
- Add tracking enablement check for Apple/Google Pay events
- Respect wcpaySettings.enableShopperTracking flag
- Maintain backward compatibility when flag not present

Testing:
- Add unit tests for batch handler validation
- Tests for nonce, events format, and error cases

Expected impact: 70-85% reduction in tracking-related requests

Fixes WOOPMNT-4451 (Phase 2)
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 11189 or branch name woopmnt-4451-update-shopper-tracks-events-enablement-criteria-2 in your-test.site/wp-admin/admin.php?page=jetpack-beta&plugin=woocommerce-payments

Option 2. Jurassic Ninja - available for logged-in A12s

🚀 Launch a JN site with this branch 🚀

ℹ️ Install this Tampermonkey script to get more options.


Build info:

  • Latest commit: 8027db5
  • Build time: 2025-12-05 14:27:01 UTC

Note: the build is updated when a new commit is pushed to this PR.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

Size Change: +4.44 kB (0%)

Total Size: 930 kB

Filename Size Change
release/woocommerce-payments/dist/blocks-checkout.js 54.8 kB +170 B (0%)
release/woocommerce-payments/dist/cart.js 5.37 kB +98 B (+2%)
release/woocommerce-payments/dist/checkout.js 34.7 kB +110 B (0%)
release/woocommerce-payments/dist/express-checkout.js 17 kB +189 B (+1%)
release/woocommerce-payments/dist/frontend-tracks.js 980 B +147 B (+18%) ⚠️
release/woocommerce-payments/dist/index.js 158 kB +403 B (0%)
release/woocommerce-payments/dist/multi-currency-switcher-block.js 19.3 kB +408 B (+2%)
release/woocommerce-payments/dist/multi-currency.js 25.7 kB +398 B (+2%)
release/woocommerce-payments/dist/order.js 22.4 kB +391 B (+2%)
release/woocommerce-payments/dist/settings.js 152 kB +113 B (0%)
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 2.39 kB +418 B (+21%) 🚨
release/woocommerce-payments/dist/subscriptions-empty-state.js 2.32 kB +417 B (+22%) 🚨
release/woocommerce-payments/dist/tos.js 3.4 kB +396 B (+13%) ⚠️
release/woocommerce-payments/dist/wc-payments-settings-spotlight.js 28.2 kB +370 B (+1%)
release/woocommerce-payments/dist/woopay-direct-checkout.js 5.83 kB +145 B (+3%)
release/woocommerce-payments/dist/woopay-express-button.js 22.9 kB +119 B (+1%)
release/woocommerce-payments/dist/woopay.js 71 kB +150 B (0%)
ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 1.45 kB
release/woocommerce-payments/assets/css/admin.rtl.css 1.45 kB
release/woocommerce-payments/assets/css/success.css 1.06 kB
release/woocommerce-payments/assets/css/success.rtl.css 1.06 kB
release/woocommerce-payments/dist/blocks-checkout-rtl.css 3.05 kB
release/woocommerce-payments/dist/blocks-checkout.css 3.05 kB
release/woocommerce-payments/dist/cart-block-rtl.css 113 B
release/woocommerce-payments/dist/cart-block.css 112 B
release/woocommerce-payments/dist/cart-block.js 16.7 kB
release/woocommerce-payments/dist/checkout-rtl.css 1.13 kB
release/woocommerce-payments/dist/checkout.css 1.13 kB
release/woocommerce-payments/dist/express-checkout-rtl.css 367 B
release/woocommerce-payments/dist/express-checkout.css 367 B
release/woocommerce-payments/dist/index-rtl.css 21.8 kB
release/woocommerce-payments/dist/index.css 21.8 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.08 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 3.82 kB
release/woocommerce-payments/dist/multi-currency.css 3.83 kB
release/woocommerce-payments/dist/order-rtl.css 740 B
release/woocommerce-payments/dist/order.css 740 B
release/woocommerce-payments/dist/plugins-page-rtl.css 484 B
release/woocommerce-payments/dist/plugins-page.css 484 B
release/woocommerce-payments/dist/plugins-page.js 2.64 kB
release/woocommerce-payments/dist/product-details-rtl.css 433 B
release/woocommerce-payments/dist/product-details.css 436 B
release/woocommerce-payments/dist/product-details.js 12.3 kB
release/woocommerce-payments/dist/settings-rtl.css 12.4 kB
release/woocommerce-payments/dist/settings.css 12.2 kB
release/woocommerce-payments/dist/subscription-edit-page.js 703 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal-rtl.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 527 B
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 730 B
release/woocommerce-payments/dist/subscriptions-empty-state-rtl.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 120 B
release/woocommerce-payments/dist/success.js 6.03 kB
release/woocommerce-payments/dist/tos-rtl.css 235 B
release/woocommerce-payments/dist/tos.css 235 B
release/woocommerce-payments/dist/wc-payments-settings-spotlight-rtl.css 1.59 kB
release/woocommerce-payments/dist/wc-payments-settings-spotlight.css 1.59 kB
release/woocommerce-payments/dist/woopay-rtl.css 4.27 kB
release/woocommerce-payments/dist/woopay.css 4.25 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 625 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 814 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.46 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/jetpack-script-data.js 957 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.02 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/script-data.js 69 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/babel.config.js 163 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.css 2.47 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.js 14.3 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.rtl.css 2.47 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.css 10.1 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.js 29.7 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.rtl.css 10.1 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.css 198 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.js 280 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.rtl.css 198 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.css 625 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.js 333 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.rtl.css 626 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-users.js 417 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-users-connection.js 161 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-ajax.js 521 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-callables.js 585 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-admin-create-user.css 215 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-admin-create-user.js 521 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-login.css 721 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-login.js 412 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-users.js 625 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.04 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-empty-state.css 294 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 408 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.59 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 301 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 746 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 574 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 414 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 543 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.4 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.78 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.84 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 545 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.52 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.2 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.7 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 507 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 358 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 428 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 782 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.09 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.26 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 391 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.04 kB

compressed-size-action

@rtio
Copy link
Member Author

rtio commented Dec 5, 2025

Closing since this is not working as I expected.

@rtio rtio closed this Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants