Skip to content

Commit b4309d2

Browse files
authored
WooCommerce Analytics: Fix duplicate product purchase events (#44973)
* Refactor event tracking in Universal class: update order processing hooks and improve capture_event_in_session_data method to accept event properties. This enhances the accuracy of event data captured during checkout and cart actions. * changelog
1 parent 9eb8c10 commit b4309d2

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fix duplicate product purchase events on order confirmation page

projects/packages/woocommerce-analytics/src/class-universal.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ public function init_hooks() {
5050
// Send events after checkout block.
5151
add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', array( $this, 'checkout_process' ) );
5252

53-
// order confirmed.
54-
add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 );
53+
// order processed.
54+
add_action( 'woocommerce_checkout_order_processed', array( $this, 'order_process' ), 10, 1 );
55+
add_action( 'woocommerce_store_api_checkout_order_processed', array( $this, 'order_process' ), 10, 1 );
5556

5657
add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 );
5758

@@ -153,7 +154,7 @@ public function remove_from_cart() {
153154
*/
154155
public function capture_remove_from_cart( $cart_item_key, $cart ) {
155156
$item = $cart->removed_cart_contents[ $cart_item_key ] ?? null;
156-
$this->capture_event_in_session_data( (int) $item['product_id'], (int) $item['quantity'], 'woocommerceanalytics_remove_from_cart' );
157+
$this->capture_event_in_session_data( 'woocommerceanalytics_remove_from_cart', (int) $item['product_id'], (int) $item['quantity'] );
157158
}
158159

159160
/**
@@ -169,13 +170,13 @@ public function capture_remove_from_cart( $cart_item_key, $cart ) {
169170
public function capture_cart_quantity_update( $cart_item_key, $quantity, $old_quantity, $cart ) {
170171
$product_id = $cart->cart_contents[ $cart_item_key ]['product_id'];
171172
if ( $quantity > $old_quantity ) {
172-
$this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
173+
$this->capture_event_in_session_data( 'woocommerceanalytics_add_to_cart', $product_id, $quantity );
173174
$this->lock_add_to_cart_events = true;
174175
return;
175176
}
176177

177178
if ( $quantity < $old_quantity ) {
178-
$this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_remove_from_cart' );
179+
$this->capture_event_in_session_data( 'woocommerceanalytics_remove_from_cart', $product_id, $quantity );
179180
return;
180181
}
181182
}
@@ -291,12 +292,16 @@ public function checkout_process() {
291292
}
292293

293294
/**
294-
* After the checkout process, fire an event for each item in the order
295+
* After the order processed, fire an event for each item in the order
295296
*
296-
* @param string $order_id Order Id.
297+
* @param string|WC_Order $order_id_or_order Order Id or Order object.
297298
*/
298-
public function order_process( $order_id ) {
299-
$order = wc_get_order( $order_id );
299+
public function order_process( $order_id_or_order ) {
300+
if ( is_string( $order_id_or_order ) ) {
301+
$order = wc_get_order( $order_id_or_order );
302+
} else {
303+
$order = $order_id_or_order;
304+
}
300305

301306
if (
302307
! $order
@@ -359,8 +364,11 @@ public function order_process( $order_id ) {
359364
if ( is_array( $order_coupons ) ) {
360365
$order_coupons_count = count( $order_coupons );
361366
}
362-
$this->record_event(
367+
368+
$this->capture_event_in_session_data(
363369
'woocommerceanalytics_product_purchase',
370+
$product_id,
371+
$order_item->get_quantity(),
364372
array(
365373
'oi' => $order->get_order_number(),
366374
'pq' => $order_item->get_quantity(),
@@ -379,8 +387,7 @@ public function order_process( $order_id ) {
379387
'from_checkout' => $checkout_page_used,
380388
'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block,
381389
'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode,
382-
),
383-
$product_id
390+
)
384391
);
385392
}
386393
}
@@ -455,23 +462,23 @@ public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $va
455462
if ( $this->lock_add_to_cart_events ) {
456463
return;
457464
}
458-
$this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
465+
$this->capture_event_in_session_data( 'woocommerceanalytics_add_to_cart', $product_id, $quantity );
459466
}
460467

461468
/**
462469
* Track in-session data.
463470
*
471+
* @param string $event Fired event.
464472
* @param int $product_id Product ID.
465473
* @param int $quantity Quantity.
466-
* @param string $event Fired event.
474+
* @param array $properties Event properties.
467475
*/
468-
public function capture_event_in_session_data( $product_id, $quantity, $event ) {
476+
public function capture_event_in_session_data( $event, $product_id, $quantity, $properties = array() ) {
469477

470478
$product = wc_get_product( $product_id );
471479
if ( ! $product instanceof WC_Product ) {
472480
return;
473481
}
474-
475482
$quantity = ( 0 === $quantity ) ? 1 : $quantity;
476483

477484
// check for existing data.
@@ -484,14 +491,21 @@ public function capture_event_in_session_data( $product_id, $quantity, $event )
484491
$data = array();
485492
}
486493

494+
$event_properties = array_merge(
495+
array(
496+
'quantity' => (string) $quantity,
497+
'session_id' => $this->get_session_id(),
498+
'landing_page' => $this->get_landing_page(),
499+
'is_engaged' => $this->is_engaged_session(),
500+
),
501+
$properties
502+
);
503+
487504
// extract new event data.
488505
$new_data = array(
489-
'event' => $event,
490-
'product_id' => (string) $product_id,
491-
'quantity' => (string) $quantity,
492-
'session_id' => $this->get_session_id(),
493-
'landing_page' => $this->get_landing_page(),
494-
'is_engaged' => $this->is_engaged_session(),
506+
'event' => $event,
507+
'product_id' => (string) $product_id,
508+
'properties' => $event_properties,
495509
);
496510

497511
// append new data.

0 commit comments

Comments
 (0)