You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在WooCommerce订单确认页创建微软广告订单总额统计函数

How to Track WooCommerce Order Revenue for Microsoft Ads

Hey there, let's get this Microsoft Ads revenue tracking set up on your WooCommerce order confirmation page. I've done this a few times, so here's a straightforward approach that works reliably:

Step 1: Use WooCommerce's woocommerce_thankyou Hook

The order confirmation (thank you) page has a dedicated WooCommerce hook called woocommerce_thankyou that fires after the order is confirmed. This is the perfect place to inject our tracking code because it gives us direct access to the full order data.

Step 2: Add the Tracking Function to Your Site

You'll want to add this code either to your child theme's functions.php file or a custom plugin (using a plugin is safer to avoid losing changes when your theme updates):

add_action( 'woocommerce_thankyou', 'track_microsoft_ads_purchase_revenue', 20 );
function track_microsoft_ads_purchase_revenue( $order_id ) {
    // Skip if no order ID is provided
    if ( ! $order_id ) {
        return;
    }

    // Fetch the WooCommerce order object
    $order = wc_get_order( $order_id );
    
    // Make sure we have a valid order object
    if ( ! is_a( $order, 'WC_Order' ) ) {
        return;
    }

    // Get the final order total (includes taxes, shipping, discounts)
    $order_revenue = $order->get_total();
    // Get the order's currency code (e.g., USD, EUR)
    $order_currency = $order->get_currency();
?>
    <script>
        // Ensure the Microsoft Ads uetq object exists to prevent errors
        window.uetq = window.uetq || [];
        // Push the purchase event with revenue data
        window.uetq.push('event', 'purchase', {
            'revenue_value': <?php echo esc_js( $order_revenue ); ?>,
            'currency': '<?php echo esc_js( $order_currency ); ?>'
        });
    </script>
<?php
}

Key Details to Note:

  • Event Name: I swapped the empty event string for purchase because Microsoft Ads recommends using specific event names for better tracking accuracy. This will make it easier to segment and analyze your data in the Microsoft Ads dashboard.
  • Revenue Value: $order->get_total() pulls the final amount the customer paid (including taxes, shipping, and applied discounts). If you only want to track product costs before extra fees, use $order->get_subtotal() instead.
  • Security: We use esc_js() to sanitize values before outputting them into JavaScript—this prevents cross-site scripting (XSS) vulnerabilities.
  • Fallback: The window.uetq = window.uetq || []; line ensures the tracking code won't throw errors if the base Microsoft Ads UET script hasn't loaded yet.

Testing the Setup

Place a test order on your site, then visit the order confirmation page. View the page source (right-click > View Page Source) and search for the uetq.push code. You should see the correct order total and currency code populated in the script.

Just make sure you've already added the base Microsoft Ads universal event tracking (UET) code to your site's header or footer—this snippet relies on that base code to function properly.

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:50:16