From c6fb4aefbc6c08105f753b00a9df347fe050423b Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Fri, 8 May 2020 11:49:45 -0400 Subject: [PATCH] Add `wcadmin_categories_view`, `wcadmin_categories_seach`, `wcadmin_tags_view`, and `wcadmin_tags_search` Tracks events. --- .../events/class-wc-products-tracking.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/includes/tracks/events/class-wc-products-tracking.php b/includes/tracks/events/class-wc-products-tracking.php index 3a993b94063..577e1d534a2 100644 --- a/includes/tracks/events/class-wc-products-tracking.php +++ b/includes/tracks/events/class-wc-products-tracking.php @@ -18,6 +18,7 @@ class WC_Products_Tracking { */ public function init() { add_action( 'load-edit.php', array( $this, 'track_products_view' ), 10 ); + add_action( 'load-edit-tags.php', array( $this, 'track_categories_and_tags_view' ), 10, 2 ); add_action( 'edit_post', array( $this, 'track_product_updated' ), 10, 2 ); add_action( 'transition_post_status', array( $this, 'track_product_published' ), 10, 3 ); add_action( 'created_product_cat', array( $this, 'track_product_category_created' ) ); @@ -56,6 +57,47 @@ class WC_Products_Tracking { } } + /** + * Send a Tracks event when the Products Categories and Tags page is viewed. + */ + public function track_categories_and_tags_view() { + // We only record Tracks event when no `_wp_http_referer` query arg is set, since + // when searching, the request gets sent from the browser twice, + // once with the `_wp_http_referer` and once without it. + // + // Otherwise, we would double-record the view and search events. + + // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification + if ( + isset( $_GET['post_type'] ) + && 'product' === wp_unslash( $_GET['post_type'] ) + && isset( $_GET['taxonomy'] ) + && ! isset( $_GET['_wp_http_referer'] ) + ) { + $taxonomy = wp_unslash( $_GET['taxonomy'] ); + // phpcs:enable + + if ( 'product_cat' === $taxonomy ) { + WC_Tracks::record_event( 'categories_view' ); + } elseif ( 'product_tag' === $taxonomy ) { + WC_Tracks::record_event( 'tags_view' ); + } + + // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification + if ( + isset( $_GET['s'] ) + && 0 < strlen( sanitize_text_field( wp_unslash( $_GET['s'] ) ) ) + ) { + // phpcs:enable + + if ( 'product_cat' === $taxonomy ) { + WC_Tracks::record_event( 'categories_search' ); + } elseif ( 'product_tag' === $taxonomy ) { + WC_Tracks::record_event( 'tags_search' ); + } + } + } + } /** * Send a Tracks event when a product is updated.