Before you can start collecting WooCommerce sales data, you’ll need to add the Fathom script to your site. Follow our WordPress guide if you haven’t already done this.
Tracks when an order is successfully placed and includes the total order value in the event. We’ve also added validation to prevent duplicate event completions if the customer refreshes the thank you page.
Copy
Ask AI
add_action( 'woocommerce_thankyou', 'total_sales_order_tracking' );function total_sales_order_tracking( $order_id ) { // Get the order details $order = wc_get_order( $order_id ); // Check if the event has already been tracked for this order $event_tracked = get_post_meta( $order_id, '_total_sales_event_tracked', true ); if ( !$event_tracked ) { // Convert the order total to cents $order_total = $order->get_total() * 100; // Trigger the event on page load and add the order total to the event ?> <script> window.addEventListener('load', function() { fathom.trackEvent('Order Placed', { _value: '<?php echo $order_total; ?>', }); }); </script> <?php // Mark the event as tracked to avoid duplicates update_post_meta( $order_id, '_total_sales_event_tracked', true ); }}