WooCommerce 4.0+ ships a dedicated Analytics REST API at /wp-json/wc/v3/reports/ and the newer Analytics endpoints at /wp-json/wc-analytics/ that power the WooCommerce Admin dashboard. These endpoints support date filtering, pagination, extended stats, and segmentation — making it possible to build custom dashboards, export pipelines, or headless reporting tools without direct database access.
Problem: WooCommerce generates revenue, orders, and product analytics data, but accessing it programmatically — for custom dashboards, external BI tools, or automated reports — requires either querying the database directly or using undocumented internal functions.
Solution: Use the WooCommerce Analytics REST API (/wc-analytics/ namespace) to query reports programmatically. Endpoints include /wc-analytics/revenue, /wc-analytics/orders, /wc-analytics/products, and /wc-analytics/customers with date range, grouping, and filtering parameters. Authenticate with Application Passwords and schedule report pulls via a cron job for external BI integration.
The examples below use PHP with the WordPress HTTP API to query revenue, top products, and customer stats from the WooCommerce Analytics API, with authentication via Application Passwords.
[
// Application Password: base64("username:app-password")
'Authorization' => 'Basic ' . base64_encode(
get_option( 'wc_analytics_api_user' ) . ':' .
get_option( 'wc_analytics_api_pass' )
),
],
'timeout' => 30,
] );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
return new WP_Error( 'api_error', "WC Analytics API returned HTTP $code" );
}
return json_decode( wp_remote_retrieve_body( $response ), true ) ?? [];
}
// ── 1. Revenue report: last 30 days ───────────────────────────────────────
$revenue = wc_analytics_request( 'revenue', [
'after' => date( 'Y-m-d', strtotime( '-30 days' ) ),
'before' => date( 'Y-m-d' ),
'interval' => 'day',
'extended_info' => true,
] );
// ── 2. Top 10 products by revenue this month ──────────────────────────────
$top_products = wc_analytics_request( 'products', [
'after' => date( 'Y-m-01' ),
'before' => date( 'Y-m-d' ),
'per_page' => 10,
'orderby' => 'net_revenue',
'order' => 'desc',
] );
foreach ( (array) $top_products as $product ) {
printf(
"%s — %d sold — $%.2f revenue\n",
esc_html( $product['name'] ?? '' ),
(int) ( $product['items_sold'] ?? 0 ),
(float) ( $product['net_revenue'] ?? 0 )
);
}
// ── 3. New customers vs returning this month ──────────────────────────────
$customers = wc_analytics_request( 'customers', [
'registered_before' => date( 'Y-m-d' ),
'registered_after' => date( 'Y-m-01' ),
'per_page' => 1,
] );
$total_new = $customers['total'] ?? 0;
echo "New customers this month: $total_new\n";
NOTE: Application Passwords (WordPress 5.6+) are the recommended authentication mechanism for server-to-server API calls; they can be revoked per-application without changing the user's main password and are logged in the user's security audit trail — never use the main account password in an API credential.