Guide
How to track WooCommerce revenue in Umami
Umami has a Revenue report, but it does not read WooCommerce orders by itself. Something has to send each order to Umami as an event. Here are two ways to do that, and what to expect from the numbers.
Short answer
To track WooCommerce revenue in Umami, send an event that carries a revenue and a currency property each time an order is placed. Umami's Revenue report (Umami 2.14 or newer) adds those up. Print a small script on the WooCommerce order confirmation page that calls umami.track('purchase', { revenue, currency }) once per order, or switch on WooCommerce in Connect for Umami.
How Umami's Revenue report works
Umami builds its Revenue report from events, not from your store. An event counts as revenue when its data includes two properties: revenue, a number, and currency, an ISO 4217 code such as USD or EUR. If Umami does not recognize the currency code, it falls back to USD. The event name is up to you. This guide uses purchase.
Revenue reporting is available from Umami 2.14.0. In the report you choose a currency, and Umami adds up the events in that currency for the period you select.
umami.track('purchase', { revenue: 36.5, currency: 'USD', order_id: '1042' });The two methods compared
| PHP snippet | Connect for Umami | |
|---|---|---|
| Needs code | Yes | No |
| Sends revenue and currency for each order | Yes | Yes |
| Counts each order once | Yes | Yes |
| Skips failed, canceled and refunded orders | Yes | Yes |
| Cart and checkout events | No | Yes |
| Tax, shipping, discount, coupon and payment method on the purchase | Only if you add them | Yes |
| Survives a theme change or update | Only in a child theme or code-snippets plugin | Yes |
Method 1: send WooCommerce revenue to Umami with a PHP snippet
This works on any WooCommerce site where the Umami tracker is already loading.
Check that Umami is tracking the store
The snippet uses the Umami tracker that is already on the page. If you have not added it yet, follow how to add Umami to WordPress first.
Add the snippet
Copy the code below into a child theme's
functions.phpor into a code-snippets plugin. Do not put it in a parent theme, or the next theme update will remove it.Place a test order
Open the store in a private browser window, so nothing excludes you from tracking, and check out with a test payment method.
Check Umami
In Umami, open the website and look for
purchaseunder Events. Then open the Revenue report and choose your currency. Reload the confirmation page once: the order must not be counted a second time.
/**
* Send each WooCommerce order to Umami once, as a purchase event with revenue.
*/
add_action( 'wp_footer', function () {
// Only on the order confirmation ("order received") page.
if ( ! function_exists( 'is_order_received_page' ) || ! is_order_received_page() ) {
return;
}
global $wp;
$order_id = isset( $wp->query_vars['order-received'] ) ? absint( $wp->query_vars['order-received'] ) : 0;
$order = $order_id ? wc_get_order( $order_id ) : false;
if ( ! $order ) {
return;
}
// The order key in the URL proves this visitor may see this order.
$key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : '';
if ( '' === $key || ! hash_equals( $order->get_order_key(), $key ) ) {
return;
}
// Skip orders that did not go through, and orders that were already sent.
if ( $order->has_status( array( 'failed', 'cancelled', 'refunded' ) ) || $order->get_meta( '_umami_purchase_sent' ) ) {
return;
}
$data = array(
'revenue' => round( (float) $order->get_total(), 2 ),
'currency' => $order->get_currency(),
'order_id' => (string) $order->get_order_number(),
);
// Umami's script is deferred: wait until it exists, for up to 10 seconds.
$script = sprintf(
'(function () {
var data = %s, tries = 0;
var timer = setInterval(function () {
if (window.umami && window.umami.track) {
clearInterval(timer);
window.umami.track("purchase", data);
} else if (++tries >= 100) {
clearInterval(timer);
}
}, 100);
})();',
wp_json_encode( $data, JSON_HEX_TAG )
);
wp_print_inline_script_tag( $script );
// Flag the order so a reload or a later visit is not counted again.
$order->update_meta_data( '_umami_purchase_sent', time() );
$order->save();
}, 20 );What the snippet does
- Runs only on the order confirmation page. It hooks
wp_footerand checksis_order_received_page(), so it does not depend on which confirmation template the store uses, classic or block-based. - Checks the order key. The
keyin the URL has to match the order. That is WooCommerce's own proof that this visitor may see it. - Skips orders that did not go through. Failed, canceled and refunded orders are left out.
- Counts each order once. After printing the script it saves a flag on the order with
update_meta_data()andsave(). These work with High-Performance Order Storage too. - Waits for Umami. The tracker loads with
defer, so the script looks forwindow.umamievery 100 milliseconds and gives up after 10 seconds. - Escapes the data. The values pass through
wp_json_encode()before they are printed.
The snippet needs WordPress 5.7 or newer for wp_print_inline_script_tag(). To send more detail, add keys to the $data array, for example a tax key filled from $order->get_total_tax().
Why Umami revenue can read lower than WooCommerce
Both methods on this page count the purchase in the customer's browser, on the confirmation page. A customer who pays on an external payment page and never returns to your site, or whose browser blocks the Umami script, is not counted. Expect Umami revenue to read slightly lower than WooCommerce's own reports.
What you get in return: the sale lands in the same Umami session as the visit that led to it, so you can see which referrer, campaign or landing page it came from. Use Umami to see where sales come from, and WooCommerce as the record of what was sold.
Method 2: track revenue with Connect for Umami
No code. The plugin sends the purchase event and three more store events.
Connect the site to Umami
In WordPress, go to Settings → WPUmami → Tracking. Paste your Umami website ID and script URL, then save.
Switch on WooCommerce
Open the Store tab, tick WooCommerce and save.
Place a test order and check Umami
Use a private browser window, because logged-in administrators are not tracked by default. In Umami you should see
add_to_cart,begin_checkoutandpurchaseunder Events, and the order total in the Revenue report.
What Connect for Umami sends for WooCommerce
| Event | When it is sent | Data |
|---|---|---|
purchase | The order confirmation page is shown for a new order. Once per order. | revenue, currency, order_id, items, tax, shipping, discount, coupon, payment_method |
add_to_cart | A product is added, or its quantity is increased. | product, product_id, sku, quantity, price, value, currency |
remove_from_cart | A line is removed, or its quantity is reduced. | Same as add_to_cart |
begin_checkout | The checkout page is shown with items in the cart. | value, currency, items |
Works with the classic cart and checkout, the block cart and checkout, and the Store API. No customer names, email addresses, addresses or payment details are sent.
The WooCommerce tracking page describes every event and data key in full.
Frequently asked questions
Does Umami track WooCommerce revenue automatically?
No. Umami has a Revenue report, but it only adds up events that carry revenue and currency properties. Umami does not send those events from WooCommerce by itself. You send them with code, as in the snippet on this page, or with a plugin such as Connect for Umami.
Which Umami version do I need for revenue tracking?
Umami 2.14.0 or newer. On an older self-hosted version the purchase event is still recorded as an event, but there is no Revenue report to add it up.
What should the WooCommerce purchase event be called in Umami?
Any name of up to 50 characters. Umami's Revenue report reads the revenue and currency properties, not the event name. This guide and Connect for Umami both use purchase.
Why is revenue in Umami lower than in WooCommerce?
The purchase is counted in the customer's browser on the order confirmation page. Customers who pay on an external page and never return to your site, or who block the Umami script, are not counted. Failed, canceled and refunded orders are left out as well.
How do I stop a reload of the thank-you page from counting an order twice?
Mark the order when you send the event. The snippet saves a flag in the order's meta data with $order->update_meta_data() and $order->save(), and skips any order that already has it. Connect for Umami does the same for you.
Does this work with the WooCommerce block checkout and HPOS?
Yes. The snippet checks is_order_received_page() and reads and writes the order through WooCommerce's order object, so it works with the block checkout and with High-Performance Order Storage. Connect for Umami supports the classic and block cart and checkout, the Store API and HPOS.
Connect WordPress to Umami in about two minutes
One plugin: the tracking script, events, store revenue and your stats inside WordPress. Plans start at $12 a year, with a 14-day money-back guarantee.