Skip to content

Commit

Permalink
Update how Cart/Checkout is tracked to be realtime. (#35139)
Browse files Browse the repository at this point in the history
* Update how Cart/Checkout is tracked

* add extra to see from checkout is coming

* address changes
  • Loading branch information
senadir authored Feb 9, 2024
1 parent 463e1ac commit 30544af
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: We updated how we track Cart/Checkout


Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,49 @@ public function capture_order_confirmation_view() {

$order = wc_get_order( $order_id );

$order_source = $order->get_created_via();
$checkout_page_contains_checkout_block = '0';
$checkout_page_contains_checkout_shortcode = '0';

if ( 'store-api' === $order_source ) {
$checkout_page_contains_checkout_block = '1';
$checkout_page_contains_checkout_shortcode = '0';
} elseif ( 'checkout' === $order_source ) {
$checkout_page_contains_checkout_block = '0';
$checkout_page_contains_checkout_shortcode = '1';
}

$coupons = $order->get_coupons();
$coupon_used = 0;
if ( is_countable( $coupons ) ) {
$coupon_used = count( $coupons ) ? 1 : 0;
}

if ( is_object( WC()->session ) ) {
$create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No';
$create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No';
$checkout_page_used = true === WC()->session->get( 'checkout_page_used' ) ? 'Yes' : 'No';
} else {
$create_account = 'No';
$create_account = 'No';
$checkout_page_used = 'No';
}

$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',
'oi' => $order->get_id(),
'order_value' => $order->get_total(),
'payment_option' => $order->get_payment_method(),
'products_count' => $order->get_item_count(),
'products' => $this->format_items_to_json( $order->get_items() ),
'order_note' => $order->get_customer_note(),
'shipping_option' => $order->get_shipping_method(),
'coupon_used' => $coupon_used,
'create_account' => $create_account,
'express_checkout' => 'null', // TODO: not solved yet.
'guest_checkout' => $order->get_customer_id() ? 'No' : 'Yes',
'oi' => $order->get_id(),
'order_value' => $order->get_total(),
'payment_option' => $order->get_payment_method(),
'products_count' => $order->get_item_count(),
'products' => $this->format_items_to_json( $order->get_items() ),
'order_note' => $order->get_customer_note(),
'shipping_option' => $order->get_shipping_method(),
'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 All @@ -124,10 +141,35 @@ public function capture_cart_view() {
* Track the checkout page view
*/
public function capture_checkout_view() {
if ( ! is_checkout() ) {
global $post;
$checkout_page_id = wc_get_page_id( 'checkout' );

$is_checkout = $checkout_page_id && is_page( $checkout_page_id )
|| wc_post_content_has_shortcode( 'woocommerce_checkout' )
|| has_block( 'woocommerce/checkout', $post )
|| has_block( 'woocommerce/classic-shortcode', $post )
|| apply_filters( 'woocommerce_is_checkout', false )
|| \Automattic\Jetpack\Constants::is_defined( 'WOOCOMMERCE_CHECKOUT' );

if ( ! $is_checkout ) {
return;
}

$is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No';
$checkout_page_contains_checkout_block = '0';
$checkout_page_contains_checkout_shortcode = '1';

$session = WC()->session;
if ( is_object( $session ) ) {
$session->set( 'checkout_page_used', true );
$session->save_data();
$draft_order_id = $session->get( 'store_api_draft_order', 0 );
if ( $draft_order_id ) {
$checkout_page_contains_checkout_block = '1';
$checkout_page_contains_checkout_shortcode = '0';
}
}

// Order received page is also a checkout page, so we need to bail out if we are on that page.
if ( is_order_received_page() ) {
return;
Expand All @@ -137,7 +179,11 @@ public function capture_checkout_view() {
'woocommerceanalytics_checkout_view',
array_merge(
$this->get_cart_checkout_shared_data(),
array()
array(
'from_checkout' => $is_in_checkout_page,
'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block,
'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode,
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ function ( $payment_gateway ) {
);

$enabled_payment_options = array_keys( $enabled_payment_options );

$shared_data = array(
$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->get_cart_total(),
'order_value' => $cart_total,
'shipping_options_count' => 'null', // TODO: not solved yet.
'coupon_used' => $coupon_used,
'payment_options' => $enabled_payment_options,
Expand Down Expand Up @@ -387,8 +387,8 @@ public function process_event_properties( $event_name, $properties = array(), $p
$all_props = apply_filters(
'jetpack_woocommerce_analytics_event_props',
array_merge(
$properties,
$this->get_common_properties()
$this->get_common_properties(), // We put this here to allow override of common props.
$properties
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ public function get_shipping_option_for_item( $cart_item_key ) {
* On the Checkout page, trigger an event for each product in the cart
*/
public function checkout_process() {
$cart = WC()->cart->get_cart();
global $post;
$checkout_page_id = wc_get_page_id( 'checkout' );
$cart = WC()->cart->get_cart();

$enabled_payment_options = array_filter(
WC()->payment_gateways->get_available_payment_gateways(),
Expand All @@ -197,16 +199,12 @@ function ( $payment_gateway ) {
);

$enabled_payment_options = array_keys( $enabled_payment_options );
$include_express_payment = false;

$wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' );
$has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' );
// Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled.
if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) {
$wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() );
if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) {
$include_express_payment = true;
}

$is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No';
$session = WC()->session;
if ( is_object( $session ) ) {
$session->set( 'checkout_page_used', true );
$session->save_data();
}

foreach ( $cart as $cart_item_key => $cart_item ) {
Expand All @@ -221,6 +219,8 @@ function ( $payment_gateway ) {

$data = $this->get_cart_checkout_shared_data();

$data['from_checkout'] = $is_in_checkout_page;

if ( ! empty( $data['products'] ) ) {
unset( $data['products'] );
}
Expand All @@ -237,30 +237,57 @@ function ( $payment_gateway ) {
$product->get_id()
);

if ( true === $include_express_payment ) {
wc_enqueue_js(
"
// wcpay.payment-request.availability event gets fired twice.
// make sure we push only one event.
var cartItem_{$cart_item_key}_logged = false;
wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) {
if ( true === cartItem_{$cart_item_key}_logged ) {
return;
}
var properties = {$properties};
properties.express_checkout = args.paymentRequestType;
_wca.push(properties);
cartItem_{$cart_item_key}_logged = true;
});
wc_enqueue_js(
"
);
} else {
$this->record_event(
'woocommerceanalytics_product_checkout',
$data,
$product->get_id()
);
}
var cartItem_{$cart_item_key}_logged = false;
var properties = {$properties};
// Check if jQuery is available
if ( typeof jQuery !== 'undefined' ) {
// This is only triggered on the checkout shortcode.
jQuery( document.body ).on( 'init_checkout', function () {
if ( true === cartItem_{$cart_item_key}_logged ) {
return;
}
wp.hooks.addAction( 'wcpay.payment-request.availability', 'wcpay', function ( args ) {
properties.express_checkout = args.paymentRequestType;
} );
properties.checkout_page_contains_checkout_block = '0';
properties.checkout_page_contains_checkout_shortcode = '1';
_wca.push( properties );
cartItem_{$cart_item_key}_logged = true;
} );
}
if (
typeof wp !== 'undefined' &&
typeof wp.data !== 'undefined' &&
typeof wp.data.subscribe !== 'undefined'
) {
wp.data.subscribe( function () {
if ( true === cartItem_{$cart_item_key}_logged ) {
return;
}
const checkoutDataStore = wp.data.select( 'wc/store/checkout' );
// Ensures we're not in Cart, but in Checkout page.
if (
typeof checkoutDataStore !== 'undefined' &&
checkoutDataStore.getOrderId() !== 0
) {
properties.express_checkout = Object.keys( wc.wcBlocksRegistry.getExpressPaymentMethods() );
properties.checkout_page_contains_checkout_block = '1';
properties.checkout_page_contains_checkout_shortcode = '0';
_wca.push( properties );
cartItem_{$cart_item_key}_logged = true;
}
} );
}
"
);

}
}

Expand All @@ -282,9 +309,12 @@ public function order_process( $order_id ) {
$payment_option = $order->get_payment_method();

if ( is_object( WC()->session ) ) {
$create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No';
$create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No';
$checkout_page_used = true === WC()->session->get( 'checkout_page_used' ) ? 'Yes' : 'No';

} else {
$create_account = 'No';
$create_account = 'No';
$checkout_page_used = 'No';
}

$guest_checkout = $order->get_user() ? 'No' : 'Yes';
Expand All @@ -301,6 +331,18 @@ public function order_process( $order_id ) {
}
}

$checkout_page_contains_checkout_block = '0';
$checkout_page_contains_checkout_shortcode = '0';

$order_source = $order->get_created_via();
if ( 'store-api' === $order_source ) {
$checkout_page_contains_checkout_block = '1';
$checkout_page_contains_checkout_shortcode = '0';
} elseif ( 'checkout' === $order_source ) {
$checkout_page_contains_checkout_block = '0';
$checkout_page_contains_checkout_shortcode = '1';
}

// loop through products in the order and queue a purchase event.
foreach ( $order->get_items() as $order_item ) {
$product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1;
Expand All @@ -327,6 +369,9 @@ public function order_process( $order_id ) {
'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,
),
$product_id
);
Expand Down

0 comments on commit 30544af

Please sign in to comment.