From 5315079a8bf1c24573349a58ea1903447366803c Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Tue, 29 Oct 2024 15:44:02 +0100 Subject: [PATCH] Add/delayed order creation tracking woocommerce checkout (#39939) * add delayed account creation flags * changelog --- ...der-creation-tracking-woocommerce-checkout | 5 ++ .../src/class-checkout-flow.php | 3 ++ .../src/class-universal.php | 53 +++++++++++++++---- .../src/class-woo-analytics-trait.php | 24 +++++---- 4 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 projects/packages/woocommerce-analytics/changelog/add-delayed-order-creation-tracking-woocommerce-checkout diff --git a/projects/packages/woocommerce-analytics/changelog/add-delayed-order-creation-tracking-woocommerce-checkout b/projects/packages/woocommerce-analytics/changelog/add-delayed-order-creation-tracking-woocommerce-checkout new file mode 100644 index 0000000000000..a8fbe2dfb2d7b --- /dev/null +++ b/projects/packages/woocommerce-analytics/changelog/add-delayed-order-creation-tracking-woocommerce-checkout @@ -0,0 +1,5 @@ +Significance: patch +Type: added +Comment: Adds further tracking in WooCommerce for delayed account creation + + diff --git a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php index 9a3ca1f21c7f9..9d7422f530d17 100644 --- a/projects/packages/woocommerce-analytics/src/class-checkout-flow.php +++ b/projects/packages/woocommerce-analytics/src/class-checkout-flow.php @@ -95,6 +95,8 @@ public function capture_order_confirmation_view() { $checkout_page_used = 'No'; } + $delayed_account_creation = ucfirst( get_option( 'woocommerce_enable_delayed_account_creation', 'Yes' ) ); + $this->record_event( 'woocommerceanalytics_order_confirmation_view', array( @@ -102,6 +104,7 @@ public function capture_order_confirmation_view() { 'create_account' => $create_account, 'express_checkout' => 'null', // TODO: not solved yet. 'guest_checkout' => $order->get_customer_id() ? 'No' : 'Yes', + 'delayed_account_creation' => $delayed_account_creation, 'oi' => $order->get_id(), 'order_value' => $order->get_total(), 'payment_option' => $order->get_payment_method(), diff --git a/projects/packages/woocommerce-analytics/src/class-universal.php b/projects/packages/woocommerce-analytics/src/class-universal.php index a2a9a0bc2b11e..5a8fb6587b309 100644 --- a/projects/packages/woocommerce-analytics/src/class-universal.php +++ b/projects/packages/woocommerce-analytics/src/class-universal.php @@ -52,6 +52,8 @@ public function init_hooks() { add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 ); add_action( 'woocommerce_created_customer', array( $this, 'capture_created_customer' ), 10, 2 ); + + add_action( 'woocommerce_created_customer', array( $this, 'capture_post_checkout_created_customer' ), 10, 2 ); } /** @@ -315,6 +317,8 @@ public function order_process( $order_id ) { $checkout_page_used = 'No'; } + $delayed_account_creation = ucfirst( get_option( 'woocommerce_enable_delayed_account_creation', 'Yes' ) ); + $guest_checkout = $order->get_user() ? 'No' : 'Yes'; $express_checkout = 'null'; @@ -359,16 +363,17 @@ public function order_process( $order_id ) { $this->record_event( 'woocommerceanalytics_product_purchase', array( - 'oi' => $order->get_order_number(), - 'pq' => $order_item->get_quantity(), - 'payment_option' => $payment_option, - 'create_account' => $create_account, - 'guest_checkout' => $guest_checkout, - 'express_checkout' => $express_checkout, - 'products_count' => $order_items_count, - 'coupon_used' => $order_coupons_count, - 'order_value' => $order->get_total(), - 'from_checkout' => $checkout_page_used, + 'oi' => $order->get_order_number(), + 'pq' => $order_item->get_quantity(), + 'payment_option' => $payment_option, + 'create_account' => $create_account, + 'guest_checkout' => $guest_checkout, + 'delayed_account_creation' => $delayed_account_creation, + 'express_checkout' => $express_checkout, + 'products_count' => $order_items_count, + 'coupon_used' => $order_coupons_count, + 'order_value' => $order->get_total(), + 'from_checkout' => $checkout_page_used, 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block, 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode, ), @@ -521,4 +526,32 @@ public function capture_created_customer( $customer_id, $new_customer_data ) { } } } + + /** + * Capture the post checkout create account event. + * + * @param int $customer_id Customer ID. + * @param array $new_customer_data New customer data. + */ + public function capture_post_checkout_created_customer( $customer_id, $new_customer_data ) { + if ( + is_array( $new_customer_data ) + && ! empty( $new_customer_data['source'] ) + && str_contains( $new_customer_data['source'], 'delayed-account-creation' ) + ) { + + $checkout_page_used = true === WC()->session->get( 'checkout_page_used' ) ? 'Yes' : 'No'; + $checkout_page_contains_checkout_block = '1'; + $checkout_page_contains_checkout_shortcode = '0'; + + $this->record_event( + 'woocommerceanalytics_post_account_creation', + array( + 'from_checkout' => $checkout_page_used, + 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block, + 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode, + ) + ); + } + } } 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 f10c63e06b18a..1d3a6658001e1 100644 --- a/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php +++ b/projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php @@ -90,8 +90,9 @@ protected function format_items_to_json( $items ) { protected function get_cart_checkout_shared_data() { $cart = WC()->cart; - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); + $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); + $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); + $delayed_account_creation = ucfirst( get_option( 'woocommerce_enable_delayed_account_creation', 'Yes' ) ); $coupons = $cart->get_coupons(); $coupon_used = 0; @@ -113,15 +114,16 @@ function ( $payment_gateway ) { $enabled_payment_options = array_keys( $enabled_payment_options ); $cart_total = wc_prices_include_tax() ? $cart->get_cart_contents_total() + $cart->get_cart_contents_tax() : $cart->get_cart_contents_total(); $shared_data = array( - 'products' => $this->format_items_to_json( $cart->get_cart() ), - 'create_account' => $create_account, - 'guest_checkout' => $guest_checkout, - 'express_checkout' => 'null', // TODO: not solved yet. - 'products_count' => $cart->get_cart_contents_count(), - 'order_value' => $cart_total, - 'shipping_options_count' => 'null', // TODO: not solved yet. - 'coupon_used' => $coupon_used, - 'payment_options' => $enabled_payment_options, + 'products' => $this->format_items_to_json( $cart->get_cart() ), + 'create_account' => $create_account, + 'guest_checkout' => $guest_checkout, + 'delayed_account_creation' => $delayed_account_creation, + 'express_checkout' => 'null', // TODO: not solved yet. + 'products_count' => $cart->get_cart_contents_count(), + 'order_value' => $cart_total, + 'shipping_options_count' => 'null', // TODO: not solved yet. + 'coupon_used' => $coupon_used, + 'payment_options' => $enabled_payment_options, ); return $shared_data;