Skip to content

Conversation

chihsuan
Copy link
Member

@chihsuan chihsuan commented Oct 18, 2025

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 like additional_blocks_on_checkout_page), the serialization performed by http_build_query turns them into query parameters with keys such as additional_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:

$query_string = http_build_query(array(
	"additional_blocks_on_checkout_page" => array( 1, 2, 3),
	'tmp' => ''
));
echo $query_string;
// Output: additional_blocks_on_checkout_page%5B0%5D=1&additional_blocks_on_checkout_page%5B1%5D=2&additional_blocks_on_checkout_page%5B2%5D=3&tmp=
// `additional_blocks_on_checkout_page%5B0%5D` name is invalid

Here is the method used by s.js to construct the query string:

var serialize = function( obj ) {
	var str = [];
	for ( var p in obj ) {
		if ( obj.hasOwnProperty( p ) ) {
			str.push(
				encodeURIComponent( p ) + '=' + encodeURIComponent( obj[ p ] )
			);
		}
	}
	return str.join( '&' );
};

How this PR fixes it:

This update encodes event property values when they are arrays, preventing the creation of invalid property names.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

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:

  • Generate some products
  • Call 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.
  • Use the following code snippet to log the events:
 function log_remote_pixels( $preempt, $parsed_args, $url ) {
	if ( strpos( $url, WC_Tracks_Client::PIXEL ) === 0 ||  strpos( $url, 'https://pixel.wp.com/w.gif' ) === 0  ) {
		$parsed_url = wp_parse_url( $url );
		parse_str( $parsed_url['query'], $params );
		error_log( $url );
		error_log( $params['_en'] . ' -- ' . print_r( $params, true ));
	}

	return $preempt;
}

add_action( 'pre_http_request', 'log_remote_pixels', 10, 3 );
  1. Go to the cart page and add a product to the cart.
  2. Confirm that woocommerceanalytics_add_to_cart event is logged in wp-content/debug.log and additional_blocks_on_checkout_page prop value is encoded as a single string with the values separated by commas. Confirm that the additional_blocks_on_cart_page prop value is empty string (additional_blocks_on_cart_page=&additional_blocks_on_checkout_page=core%252Fgroup%252Ccore%252Fheading&).
  3. Complete the checkout process and confirm that woocommerceanalytics_product_purchase event is logged in wp-content/debug.log and additional_blocks_on_checkout_page prop value is encoded as a single string with the values separated by commas. Confirm that the additional_blocks_on_cart_page prop value is empty string (additional_blocks_on_cart_page=&additional_blocks_on_checkout_page=core%252Fgroup%252Ccore%252Fheading&).
Screenshot 2025-10-18 at 06 24 41

You can also search the events via the Tracks Live View to confirm that the events are logged correctly.

…d strings and URL-encode for pixel URL compatibility. Serialize non-indexed arrays to JSON strings
@chihsuan chihsuan self-assigned this Oct 18, 2025
Copy link
Contributor

github-actions bot commented Oct 18, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/wa-event-prop-invalid branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/wa-event-prop-invalid

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented Oct 18, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Oct 18, 2025
Copy link

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/woocommerce-analytics/src/class-wc-analytics-tracking.php 0/154 (0.00%) 0.00% 13 💔

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

@chihsuan chihsuan added [Type] Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Oct 18, 2025
@chihsuan chihsuan requested review from a team October 18, 2025 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] WooCommerce Analytics [Package] WooCommerce Analytics Enhanced analytics for WooCommerce users [Status] Needs Review This PR is ready for review. [Type] Bug When a feature is broken and / or not performing as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant