-
Notifications
You must be signed in to change notification settings - Fork 839
WooCommerce Analytics: Prevent server-side track event rejections due to array event properties #45544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
…d strings and URL-encode for pixel URL compatibility. Serialize non-indexed arrays to JSON strings
ace29c4 to
16feab5
Compare
When arrays are passed to WC_Tracks::record_event(), http_build_query() creates bracket notation in property names (e.g., prop[0], prop[1]) which violates the required naming pattern and causes event rejection. This fix implements automatic array sanitization in validate_and_sanitize(): - Empty arrays → empty string - Indexed arrays with scalars → comma-separated string (URL-encoded) - Associative/nested arrays → JSON string Aligns with Jetpack consumer-side fix: Automattic/jetpack#45544 Changes: - Add sanitize_property_values() method (PHP 7.4+ compatible) - Integrate into validate_and_sanitize() preserving object return type - Document array handling in WC_Tracks::record_event() PHPDoc - Add 8 comprehensive unit tests for all array scenarios 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Partially fixes WOOA7S-568
Proposed changes:
This PR addresses a bug where server-side events, such as
product_purchase, are being rejected due to invalid event prop names when there are additional blocks present on the cart or checkout page.While the ideal solution would be to address these issues directly in WooCommerce core, the core release schedule means we have to implement this fix here first and subsequently submit a PR to WooCommerce core.
Root cause:
Event prop names are required to match the pattern
^[a-z_][a-z0-9_]*$. However, when arrays are passed as event properties (for example, an array likeadditional_blocks_on_checkout_page), the serialization performed byhttp_build_queryturns them into query parameters with keys such asadditional_blocks_on_checkout_page[0],additional_blocks_on_checkout_page[1], etc. These prop names contain brackets ([0]), which violates the required event prop naming convention and results in events getting rejected.Example:
Here is the method used by
s.jsto construct the query string:How this PR fixes it:
This update encodes event property values when they are arrays, preventing the creation of invalid property names.
Other information:
Jetpack product discussion
n/a
Does this pull request change what data or activity we track or use?
no, this doesn't add/change any data or activity we track or use but just fixes a bug where server-side events are rejected due to invalid event prop names when there are additional blocks present on the cart or checkout page.
Testing instructions:
Prerequisites:
set_transient( 'jetpack_woocommerce_analytics_additional_blocks_on_checkout_page', array( 'core/group', 'core/heading' ), DAY_IN_SECONDS );in any file that gets loaded before the cart or checkout page is loaded.woocommerceanalytics_add_to_cartevent is logged inwp-content/debug.logandadditional_blocks_on_checkout_pageprop value is encoded as a single string with the values separated by commas. Confirm that theadditional_blocks_on_cart_pageprop value is empty string (additional_blocks_on_cart_page=&additional_blocks_on_checkout_page=core%252Fgroup%252Ccore%252Fheading&).woocommerceanalytics_product_purchaseevent is logged inwp-content/debug.logandadditional_blocks_on_checkout_pageprop value is encoded as a single string with the values separated by commas. Confirm that theadditional_blocks_on_cart_pageprop value is empty string (additional_blocks_on_cart_page=&additional_blocks_on_checkout_page=core%252Fgroup%252Ccore%252Fheading&).You can also search the events via the Tracks Live View to confirm that the events are logged correctly.