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.

Tracking WooCommerce events

If you would like to capture data for events like purchases, add to cart, checkouts, etc, you can do so using our events feature. Here’s how:

Adding event code to your site

  1. Install and activate the Code Snippets WordPress plugin (if you don’t already have it)
  2. In your WordPress admin, go to Snippets → Add New
  3. Select the Functions (PHP) tab, then paste in any or all of the examples below
  4. Click Save changes and activate.
  5. Test your setup by completing a test order. On the thank you page, check your Fathom dashboard to confirm the event fired with the correct value.
Replace the sample event names in the code examples with names that make sense for you

Track when a product is added to cart

Tracks the product name and total line value when a customer adds an item to their cart.
add_action( 'woocommerce_add_to_cart', 'fathom_track_add_to_cart_event', 10, 6 );

function fathom_track_add_to_cart_event( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    $product = wc_get_product( $product_id );
    $product_name = $product->get_name();
    $line_total = $product->get_price() * $quantity * 100; // in cents

    add_action( 'wp_footer', function() use ( $product_name, $line_total ) {
        ?>
        <script>
            window.addEventListener('load', function() {
                fathom.trackEvent('Added to Cart: <?php echo esc_js( $product_name ); ?>', {
                    _value: '<?php echo esc_js( $line_total ); ?>'
                });
            });
        </script>
        <?php
    });
}

Track completed checkouts (total order value)

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.
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 );
    }
}

Track individual products sold

Tracks when an order is successfully placed and adds each line item’s product name and price to the event. Duplicate event prevention is included.
add_action( 'woocommerce_thankyou', 'sales_order_item_tracking' );
function sales_order_item_tracking( $order_id ) {
    // Get the order details
    $order = wc_get_order( $order_id );

    foreach ($order->get_items() as $item_key => $item) {
        $item_name = $item->get_name();
        $line_total = $item->get_total() * 100;

        // Check if the event for this specific item has already been tracked
        $event_tracked = get_post_meta( $order_id, '_product_sold_' . $item_key . '_tracked', true );

        if ( !$event_tracked ) {
            // Trigger the event on page load and add the item total to the event
            ?>
            <script>
                window.addEventListener('load', function() {
                    fathom.trackEvent('Product Sold: <?php echo $item_name; ?>', {
                        _value: '<?php echo $line_total; ?>',
                    });
                });
            </script>
            <?php

            // Mark this specific item as tracked
            update_post_meta( $order_id, '_product_sold_' . $item_key . '_tracked', true );
        }
    }
}