Skip to content

Commit c73bcdd

Browse files
committed
Enhance WC_Analytics_Tracking: Convert array values to comma-separated strings and URL-encode for pixel URL compatibility. Serialize non-indexed arrays to JSON strings
1 parent c90fff7 commit c73bcdd

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

projects/packages/woocommerce-analytics/src/class-wc-analytics-tracking.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,31 @@ public static function get_properties( $event_name, $event_properties ) {
228228
)
229229
: array();
230230

231-
return array_merge( $properties, $required_properties );
231+
$all_properties = array_merge( $properties, $required_properties );
232+
233+
// Convert array values to a comma-separated string and URL-encode them to ensure compatibility with JavaScript's encodeURIComponent() for pixel URL transmission.
234+
foreach ( $all_properties as $key => $value ) {
235+
if ( ! is_array( $value ) ) {
236+
continue;
237+
}
238+
239+
if ( empty( $value ) ) {
240+
$all_properties[ $key ] = '';
241+
continue;
242+
}
243+
244+
$is_indexed_array = array_keys( $value ) === range( 0, count( $value ) - 1 );
245+
if ( $is_indexed_array ) {
246+
$value_string = implode( ',', $value );
247+
$all_properties[ $key ] = rawurlencode( $value_string );
248+
continue;
249+
}
250+
251+
// Serialize non-indexed arrays to JSON strings.
252+
$all_properties[ $key ] = wp_json_encode( $value );
253+
}
254+
255+
return $all_properties;
232256
}
233257

234258
/**

0 commit comments

Comments
 (0)