Skip to content

Commit 6e14b6e

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 31a2d18 commit 6e14b6e

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
@@ -221,7 +221,31 @@ public static function get_properties( $event_name, $event_properties ) {
221221
)
222222
: array();
223223

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

227251
/**

0 commit comments

Comments
 (0)