Skip to content

Commit

Permalink
Add/delayed order creation tracking woocommerce checkout (#39939)
Browse files Browse the repository at this point in the history
* add delayed account creation flags

* changelog
  • Loading branch information
senadir authored Oct 29, 2024
1 parent 072a9db commit 5315079
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Adds further tracking in WooCommerce for delayed account creation


Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ 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(
'coupon_used' => $coupon_used,
'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(),
Expand Down
53 changes: 43 additions & 10 deletions projects/packages/woocommerce-analytics/src/class-universal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
),
Expand Down Expand Up @@ -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,
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 5315079

Please sign in to comment.