From 695e1913b9692eb8ffe1ec40698e842ca8bcb514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Pellicer?= <5908855+puntope@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:09:40 +0400 Subject: [PATCH] Support for Search and Landing Page --- .../changelog/add-search-event | 4 ++ .../src/class-universal.php | 41 +++++++++++++++++-- .../src/class-woo-analytics-trait.php | 13 +++++- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 projects/packages/woocommerce-analytics/changelog/add-search-event diff --git a/projects/packages/woocommerce-analytics/changelog/add-search-event b/projects/packages/woocommerce-analytics/changelog/add-search-event new file mode 100644 index 0000000000000..c247b7ff0e508 --- /dev/null +++ b/projects/packages/woocommerce-analytics/changelog/add-search-event @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add Search Event support diff --git a/projects/packages/woocommerce-analytics/src/class-universal.php b/projects/packages/woocommerce-analytics/src/class-universal.php index 713657256420e..1d40020b6a169 100644 --- a/projects/packages/woocommerce-analytics/src/class-universal.php +++ b/projects/packages/woocommerce-analytics/src/class-universal.php @@ -33,6 +33,9 @@ public function init_hooks() { // Initialize session add_action( 'send_headers', array( $this, 'initialize_woocommerceanalytics_session' ) ); + // Capture search + add_action( 'send_headers', array( $this, 'capture_search_query' ), 11 ); + // Capture cart events. add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); add_action( 'woocommerce_cart_item_removed', array( $this, 'capture_remove_from_cart' ), 10, 2 ); @@ -62,10 +65,24 @@ public function init_hooks() { * Set a UUID for the current session if is not yet loaded and record the session started event */ public function initialize_woocommerceanalytics_session() { - if ( ! isset( $_COOKIE['woocommerceanalytics_session_id'] ) ) { - $session_id = wp_generate_uuid4(); - $this->session_id = $session_id; - setcookie( 'woocommerceanalytics_session_id', $session_id, 0, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); + if ( ! isset( $_COOKIE['woocommerceanalytics_session'] ) ) { + $session_id = wp_generate_uuid4(); + $this->session_id = $session_id; + $this->landing_page = esc_url_raw( wp_unslash( ( empty( $_SERVER['HTTPS'] ) ? 'http' : 'https' ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ) ); + setcookie( + 'woocommerceanalytics_session', + wp_json_encode( + array( + 'session_id' => $this->session_id, + 'landing_page' => $this->landing_page, + ) + ), + 0, + COOKIEPATH, + COOKIE_DOMAIN, + is_ssl(), + true + ); $this->record_event( 'woocommerceanalytics_session_started' ); } } @@ -523,4 +540,20 @@ public function capture_post_checkout_created_customer( $customer_id, $new_custo ); } } + + /** + * Capture a search event. + */ + public function capture_search_query() { + if ( is_search() ) { + global $wp_query; + $this->record_event( + 'woocommerceanalytics_search', + array( + 'search_query' => $wp_query->get( 's' ), + 'qty' => $wp_query->found_posts, + ) + ); + } + } } diff --git a/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php b/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php index b7bcf0b3446f4..a336eae01655b 100644 --- a/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php +++ b/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php @@ -59,6 +59,13 @@ trait Woo_Analytics_Trait { */ protected $session_id; + /** + * Landing page where session started. + * + * @var string + */ + protected $landing_page; + /** * Format Cart Items or Order Items to an array * @@ -256,12 +263,16 @@ public function find_cart_checkout_content_sources() { * @return array Array of standard event props. */ public function get_common_properties() { + $session_data = json_decode( wp_unslash( $_COOKIE['woocommerceanalytics_session'] ?? '' ), true ) ?? array(); + $session_id = sanitize_text_field( $session_data['session_id'] ?? $this->session_id ); + $landing_page = sanitize_url( $session_data['landing_page'] ?? $this->landing_page ); $site_info = array( - 'session_id' => sanitize_text_field( wp_unslash( $_COOKIE['woocommerceanalytics_session_id'] ?? $this->session_id ) ), + 'session_id' => $session_id, 'blog_id' => Jetpack_Connection::get_site_id(), 'store_id' => defined( '\\WC_Install::STORE_ID_OPTION' ) ? get_option( \WC_Install::STORE_ID_OPTION ) : false, 'ui' => $this->get_user_id(), 'url' => home_url(), + 'landing_page' => $landing_page, 'woo_version' => WC()->version, 'wp_version' => get_bloginfo( 'version' ), 'store_admin' => in_array( array( 'administrator', 'shop_manager' ), wp_get_current_user()->roles, true ) ? 1 : 0,