Added `woocommerce_page_wc-addons_connection_error` action, which records a Track event when the in-app marketplace fails to connect properly to the WCCOM API.

This commit is contained in:
And Finally 2021-11-11 10:12:00 +00:00
parent b7520f3d9a
commit 9a0e91477e
3 changed files with 31 additions and 0 deletions

View File

@ -88,6 +88,7 @@ class WC_Admin_Addons {
);
if ( is_wp_error( $raw_featured ) ) {
do_action( 'woocommerce_page_wc-addons_connection_error', $raw_featured->get_error_message() );
self::output_empty();
return;
@ -95,6 +96,7 @@ class WC_Admin_Addons {
$response_code = intval( wp_remote_retrieve_response_code( $raw_featured ) );
if ( 200 !== $response_code ) {
do_action( 'woocommerce_page_wc-addons_connection_error', $response_code );
self::output_empty();
return;
@ -102,6 +104,7 @@ class WC_Admin_Addons {
$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
if ( empty( $featured ) || ! is_array( $featured ) ) {
do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
self::output_empty();
return;
@ -160,17 +163,20 @@ class WC_Admin_Addons {
);
if ( is_wp_error( $raw_extensions ) ) {
do_action( 'woocommerce_page_wc-addons_connection_error', $raw_extensions->get_error_message() );
return $raw_extensions;
}
$response_code = intval( wp_remote_retrieve_response_code( $raw_extensions ) );
if ( 200 !== $response_code ) {
do_action( 'woocommerce_page_wc-addons_connection_error', $response_code );
return new WP_Error( 'error', __( 'API error', 'woocommerce' ) );
}
$addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) );
if ( ! is_object( $addons ) || ! isset( $addons->products ) ) {
do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
return new WP_Error( 'error', __( 'API error', 'woocommerce' ) );
}

View File

@ -68,6 +68,7 @@ class WC_Tracks {
/**
* Record an event in Tracks - this is the preferred way to record events from PHP.
* 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.

View File

@ -22,6 +22,7 @@ class WC_Extensions_Tracking {
add_action( 'woocommerce_helper_disconnected', array( $this, 'track_helper_disconnected' ) );
add_action( 'woocommerce_helper_subscriptions_refresh', array( $this, 'track_helper_subscriptions_refresh' ) );
add_action( 'woocommerce_addon_installed', array( $this, 'track_addon_install' ), 10, 2 );
add_action( 'woocommerce_page_wc-addons_connection_error', array( $this, 'track_extensions_page_connection_error' ), 10, 1 );
}
/**
@ -47,6 +48,29 @@ class WC_Extensions_Tracking {
WC_Tracks::record_event( $event, $properties );
}
/**
* Send a Tracks event when the Extensions page gets a bad response or no response
* from the WCCOM extensions API.
*
* @param string $error
*/
public function track_extensions_page_connection_error( string $error = '' ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$properties = array(
'section' => empty( $_REQUEST['section'] ) ? '_featured' : wc_clean( wp_unslash( $_REQUEST['section'] ) ),
);
if ( ! empty( $_REQUEST['search'] ) ) {
$properties['search_term'] = wc_clean( wp_unslash( $_REQUEST['search'] ) );
}
// phpcs:enable
if ( ! empty( $error ) ) {
$properties['error_data'] = $error;
}
WC_Tracks::record_event( 'extensions_view_connection_error', $properties );
}
/**
* Send a Tracks even when a Helper connection process is initiated.
*/