Accurate WooCommerce purchase tracking depends on sending each order from the server with the order ID as the event_id.
If you want purchase numbers in Google Ads and Meta that match your WooCommerce orders, send the purchase from the server when the order is paid, and use the order ID as the event ID in the browser and on the server. The thank you page alone will always miss some.
Why Browser Tracking Misses Orders
- The customer pays on PayPal or a bank page and never returns to the thank you page.
- They close the tab as soon as the payment goes through.
- An ad blocker or browser privacy setting blocks the tag.
- They reload the thank you page and the purchase is counted twice.
Which Hook to Use
| Hook | When It Fires | Use It For |
|---|---|---|
woocommerce_payment_complete |
When the gateway confirms payment | The server event, for card and wallet payments |
woocommerce_order_status_processing |
When the order moves to processing | The server event, for gateways that don’t call payment complete |
woocommerce_thankyou |
Only if the customer reaches the thank you page | The browser event |
Example: Send the Purchase on a Paid Order
This hooks both server events, makes sure each order is sent once, and hands the work to a background action so checkout isn’t waiting on an ad platform:
add_action( 'woocommerce_payment_complete', 'kl_queue_purchase' );
add_action( 'woocommerce_order_status_processing', 'kl_queue_purchase' );
function kl_queue_purchase( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || $order->get_meta( '_purchase_sent' ) ) {
return;
}
$order->update_meta_data( '_purchase_sent', time() );
$order->save();
as_enqueue_async_action( 'kl_send_purchase', array( $order_id ) );
}
add_action( 'kl_send_purchase', function ( $order_id ) {
$order = wc_get_order( $order_id );
$email = strtolower( trim( $order->get_billing_email() ) );
$event = array(
'event_name' => 'Purchase',
'event_id' => (string) $order->get_id(),
'event_time' => time(),
'action_source' => 'website',
'event_source_url' => $order->get_checkout_order_received_url(),
'user_data' => array(
'em' => array( hash( 'sha256', $email ) ),
'client_ip_address' => $order->get_customer_ip_address(),
'client_user_agent' => $order->get_customer_user_agent(),
'fbc' => $order->get_meta( '_fbc' ),
'fbp' => $order->get_meta( '_fbp' ),
),
'custom_data' => array(
'currency' => $order->get_currency(),
'value' => (float) $order->get_total(),
'order_id' => (string) $order->get_id(),
),
);
wp_remote_post(
'https://graph.facebook.com/v21.0/' . KL_PIXEL_ID . '/events?access_token=' . KL_CAPI_TOKEN,
array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( array( 'data' => array( $event ) ) ),
'timeout' => 10,
)
);
} );
This is the Meta Conversions API. The same order data goes to Google Ads as a conversion upload, with the order ID as the order_id.
Use the Same ID in the Browser
On the thank you page, send the browser event with the same order ID. This is what lets each platform match the two events and keep one:
add_action( 'woocommerce_thankyou', function ( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
?>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
event: 'purchase',
event_id: '<?php echo esc_js( $order->get_id() ); ?>',
ecommerce: {
transaction_id: '<?php echo esc_js( $order->get_id() ); ?>',
value: <?php echo (float) $order->get_total(); ?>,
currency: '<?php echo esc_js( $order->get_currency() ); ?>'
}
});
</script>
<?php
} );
In GTM, map event_id to the Meta Pixel’s eventID and transaction_id to the Google Ads conversion tag’s transaction ID field.
How Each Platform Removes Duplicates
| Platform | Matches On | Notes |
|---|---|---|
| Meta | event_id and event_name |
Both events have to arrive within 48 hours. The event name has to match exactly. |
| Google Ads | Transaction ID / order ID | One conversion per ID for the same conversion action. |
| GA4 | transaction_id |
Send the purchase from one place, browser or server. Don’t rely on GA4 to merge both. |
Use the order ID, not the order number, if a sequential order number plugin is installed. Either works, as long as the browser and server send the same one.
Store Click IDs at Checkout
The server event runs with no browser, so anything it needs has to be saved on the order first:
- The
gclid, for Google Ads. - The
_fbcand_fbpcookie values, for Meta match quality. - The customer’s consent state. Only send the server event for orders where consent was given.
add_action( 'woocommerce_checkout_create_order', function ( $order ) {
$cookies = array(
'_fbc' => '_fbc',
'_fbp' => '_fbp',
'_gclid' => 'gclid',
);
foreach ( $cookies as $key => $cookie ) {
if ( ! empty( $_COOKIE[ $cookie ] ) ) {
$order->update_meta_data( $key, sanitize_text_field( wp_unslash( $_COOKIE[ $cookie ] ) ) );
}
}
} );
That assumes you already save the gclid to a first party cookie named gclid on landing.
Check It
- Place a test order and confirm one server event and one browser event arrive in Meta’s Events Manager, shown as deduplicated.
- Place an order and close the tab before the thank you page. The server event should still arrive.
- Reload the thank you page. No second purchase should be counted.
- After a week, compare platform purchases against WooCommerce orders for the same dates.
Related: server-side tracking event deduplication, Meta CAPI deduplication and match quality and the Facebook Conversions API explained.