Skip to content

Commit

Permalink
Support for Search and Landing Page
Browse files Browse the repository at this point in the history
  • Loading branch information
puntope committed Dec 20, 2024
1 parent 95609ee commit 695e191
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add Search Event support
41 changes: 37 additions & 4 deletions projects/packages/woocommerce-analytics/src/class-universal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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]" ) );

Check failure on line 71 in projects/packages/woocommerce-analytics/src/class-universal.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Detected usage of a non-sanitized, non-validated input variable _SERVER: "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" (WordPress.Security.ValidatedSanitizedInput.InputNotValidatedNotSanitized)

Check failure on line 71 in projects/packages/woocommerce-analytics/src/class-universal.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Detected usage of a non-sanitized, non-validated input variable _SERVER: "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" (WordPress.Security.ValidatedSanitizedInput.InputNotValidatedNotSanitized)
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' );
}
}
Expand Down Expand Up @@ -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,
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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();

Check failure on line 266 in projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Detected usage of a non-sanitized input variable: $_COOKIE['woocommerceanalytics_session'] (WordPress.Security.ValidatedSanitizedInput.InputNotSanitized)
$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,
Expand Down

0 comments on commit 695e191

Please sign in to comment.