diff --git a/plugins/woocommerce/changelog/fix-tracks-filtered-properties b/plugins/woocommerce/changelog/fix-tracks-filtered-properties new file mode 100644 index 00000000000..19fe5411b1f --- /dev/null +++ b/plugins/woocommerce/changelog/fix-tracks-filtered-properties @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Move tracks _ui and _ut properties out of filtered tracks properties #33621 diff --git a/plugins/woocommerce/includes/tracks/class-wc-site-tracking.php b/plugins/woocommerce/includes/tracks/class-wc-site-tracking.php index 865fedb534d..7190829183c 100644 --- a/plugins/woocommerce/includes/tracks/class-wc-site-tracking.php +++ b/plugins/woocommerce/includes/tracks/class-wc-site-tracking.php @@ -262,9 +262,8 @@ class WC_Site_Tracking { : array(); $server_details = self::get_server_details(); - $identity = WC_Tracks_Client::get_identity( $user->ID ); $blog_details = self::get_blog_details( $user->ID ); - return array_merge( $data, $properties, $server_details, $identity, $blog_details ); + return array_merge( $data, $properties, $server_details, $blog_details ); } } diff --git a/plugins/woocommerce/includes/tracks/class-wc-tracks.php b/plugins/woocommerce/includes/tracks/class-wc-tracks.php index d04eb44b46c..bd7d797aced 100644 --- a/plugins/woocommerce/includes/tracks/class-wc-tracks.php +++ b/plugins/woocommerce/includes/tracks/class-wc-tracks.php @@ -20,10 +20,10 @@ class WC_Tracks { * Note: the event request won't be made if $properties has a member called `error`. * * @param string $event_name The name of the event. - * @param array $properties Custom properties to send with the event. + * @param array $event_properties Custom properties to send with the event. * @return bool|WP_Error True for success or WP_Error if the event pixel could not be fired. */ - public static function record_event( $event_name, $properties = array() ) { + public static function record_event( $event_name, $event_properties = array() ) { /** * Don't track users who don't have tracking enabled. */ @@ -38,15 +38,8 @@ class WC_Tracks { return false; } $prefixed_event_name = self::PREFIX . $event_name; - - // Allow event props to be filtered to enable adding site-wide props. - $filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $properties, $prefixed_event_name ); - - // Delete _ui and _ut protected properties. - unset( $filtered_properties['_ui'] ); - unset( $filtered_properties['_ut'] ); - - $event_obj = new WC_Tracks_Event( $filtered_properties ); + $properties = self::get_properties( $prefixed_event_name, $event_properties ); + $event_obj = new WC_Tracks_Event( $properties ); if ( is_wp_error( $event_obj->error ) ) { return $event_obj->error; @@ -54,4 +47,28 @@ class WC_Tracks { return $event_obj->record(); } + + /** + * Get all properties for the event including filtered and identity properties. + * + * @param string $event_name Event name. + * @param array $event_properties Event specific properties. + * @return array + */ + public static function get_properties( $event_name, $event_properties ) { + /** + * Allow event props to be filtered to enable adding site-wide props. + * + * @since 4.1.0 + */ + $properties = apply_filters( 'woocommerce_tracks_event_properties', $event_properties, $event_name ); + $user = wp_get_current_user(); + $identity = WC_Tracks_Client::get_identity( $user->ID ); + + // Delete _ui and _ut protected properties. + unset( $properties['_ui'] ); + unset( $properties['_ut'] ); + + return array_merge( $properties, $identity ); + } } diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php new file mode 100644 index 00000000000..d0a787c7e20 --- /dev/null +++ b/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php @@ -0,0 +1,63 @@ + 5, + ) + ); + $this->assertContains( 'test_property', array_keys( $properties ) ); + $this->assertEquals( 5, $properties['test_property'] ); + } + + + /** + * Test that identity properties are added to the properties. + */ + public function test_addition_of_identity() { + $properties = \WC_Tracks::get_properties( + 'test_event', + array( + 'test_property' => 5, + ) + ); + $this->assertContains( '_ui', array_keys( $properties ) ); + $this->assertContains( '_ut', array_keys( $properties ) ); + } + + /** + * Test that custom identity properties cannot be added. + */ + public function test_invalid_identity() { + $properties = \WC_Tracks::get_properties( + 'test_event', + array( + '_ui' => 'bad', + '_ut' => 'bad', + ) + ); + $this->assertNotEquals( 'bad', $properties['_ui'] ); + $this->assertNotEquals( 'bad', $properties['_ut'] ); + } + +}