Skip to content
Skip to main content
← Back to Documentation

WooCommerce revenue, without a plugin

A snippet that records an order's value the moment WooCommerce marks it paid, so your revenue appears next to the traffic that produced it. No plugin, no API key: the site id you already pasted into your tracker install is enough.

WooCommerce Analytics reports revenue better than this does

WooCommerce owns the order, so its own Analytics (and Order Attribution on 8.5 and later) gives you a last-touch channel per sale for free. If what you want is revenue reporting, that is the better tool, and this page will not out-report it. What VeritaMetrics adds is the traffic WooCommerce never sees: its own documentation says the attribution cookies are read "only in the event of an order", so a shop with a hundred visits and no sale that day has nothing in WooCommerce's attribution for it at all. With this snippet, that traffic and that day's revenue (zero, honestly) sit on the same chart.


Before you start

This needs the tracker already installed and recording pageviews: any of the three methods on the WordPress install guide. Nothing below installs the tracker; it adds one hook on top of an install that already works.

The snippet

Add this to your child theme's functions.php, the same place method B of the install guide lives. Replace YOUR-SITE-ID with the Site ID from Tracking Code in your dashboard.

functions.php
add_action( 'woocommerce_payment_complete', function ( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }

    // Courtesy only, not the guarantee: skips a repeat network call for an
    // order already sent. The real guard against double counting is the
    // unique index below, which still holds even if this flag is ever lost
    // or two webhooks race (woocommerce_payment_complete is not atomic).
    if ( $order->get_meta( '_veritametrics_revenue_sent' ) ) {
        return;
    }

    $site_id = 'YOUR-SITE-ID'; // from Tracking Code in your dashboard

    $event = array(
        'n'   => 'revenue',
        'u'   => home_url( '/' ),
        'ts'  => round( microtime( true ) * 1000 ),
        // Deterministic, not random: a replayed or duplicated webhook sends
        // the same id, and VeritaMetrics' own unique index on (site, event id)
        // drops the second write rather than counting a sale twice.
        'eid' => 'wc-order-' . $order->get_id(),
        'p'   => array(
            'order_id' => (string) $order->get_id(),
            'amount'   => (float) $order->get_total(),
            'currency' => $order->get_currency() ?: 'USD',
            'product'  => implode(
                ', ',
                array_map( function ( $item ) { return $item->get_name(); }, $order->get_items() )
            ),
        ),
    );

    $response = wp_safe_remote_post(
        'https://www.veritametrics.com/api/track-events?siteId=YOUR-SITE-ID',
        array(
            'timeout' => 5,
            'headers' => array( 'Content-Type' => 'application/json' ),
            'body'    => wp_json_encode( $event ),
        )
    );

    // Written only once the request has actually been sent and answered, not
    // when the order first qualifies. Matomo's own WordPress plugin does the
    // same; Plausible's writes its flag at page render, before the browser
    // has run anything, so a purchase whose script never executes is still
    // marked sent and is lost with no retry.
    if ( ! is_wp_error( $response ) ) {
        $order->update_meta_data( '_veritametrics_revenue_sent', time() );
        $order->save();
    }
} );

Why this hook, and not the thank-you page

woocommerce_payment_complete fires once, from inside WooCommerce's own "mark this order paid" method, and only when a gateway actually confirms payment. The more obvious choice, woocommerce_thankyou, fires on every load of the order-received page, including a declined payment: WooCommerce's own checkout template passes it the order id with no success flag, so a snippet hooked there without checking status records a sale that never happened.

The trade-off is that woocommerce_payment_complete is not atomic: its own "already paid" guard is a read, then a write, with no lock between them, so two gateway webhooks arriving close together can both pass it for the same order. The snippet above does not try to close that race in PHP. It sends a deterministic event id, wc-order-<order id>, and VeritaMetrics' own database rejects a second row for the same site and event id (the unique index behind the retry and replay handling added for ISSUE-770). The WordPress order-meta flag only saves the second network call; the guarantee against double-counting a sale lives on our server, not in your database.

The order id, not the order number, is the key sent. WooCommerce's order number is filtered and can be changed by a sequential-numbering plugin; the id cannot.

How the three guards compare

Three tools solve the same "did I already send this order" problem differently, and the difference is worth knowing before you trust any of them with money:

Tool Guard Weakness
Fathom's published recipe None at any layer Hooks the thank-you page with no guard, so a refresh counts the sale again.
Plausible An order-meta flag, written at page render The flag is set before the browser runs anything. A purchase whose script never executes (a blocker, a closed tab) is marked sent and is lost with no retry.
This snippet, and Matomo's plugin A flag written only after a response Stricter, and still not a guarantee on its own: see the database guard above.

Check it worked

Place a real or test order and let payment complete. In your VeritaMetrics dashboard, switch the layout to editable and add the Revenue Overview card if it is not already there: it is not on the default layout. The order's amount and currency appear within a few seconds, with the product names from the order's line items.

What this does not do

  • No refunds. The snippet only listens for payment completion. If an order is later refunded or cancelled, the amount already sent stays exactly as sent; nothing here reduces it.
  • No funnel. revenue_data stores a site, a timestamp, an amount, a currency, an order id and a product, and no visit id. A sale cannot be traced back to the browsing session that produced it; it shows up as a total for the day, next to your traffic total for the same day, not joined to one visitor's path through the site.
  • No currency conversion. Amounts are stored in whatever currency the order reports. A shop selling in more than one currency sees mixed-currency totals on one chart with nothing converted between them.
  • No retry. One attempt, on one hook. If the request cannot reach VeritaMetrics at all (your server has no outbound network access, or the request times out), that order's revenue is not sent and nothing retries it later.

Explore the documentation

Jump to another guide.