← Voltar aos Artigos

Rastreamento de conversões do OpenAI Ads para WooCommerce: Guia completo

Guia passo a passo para configurar o rastreamento do OpenAI Ads no WooCommerce: Pixel, hooks de ação PHP, relay CAPI e deduplicação.

2026-09-09 • Guia

WooCommerce gives you complete code control, but tracking paid conversions on WordPress can feel like defusing a bomb.

One plugin update breaks your checkout hook. An aggressive caching layer (like LiteSpeed or WP Rocket) caches your dynamic pixel script. A buyer pays via PayPal and closes the tab before the woocommerce_thankyou action fires. Or an ad blocker kills client-side JavaScript entirely.

The result is the same: orders register in your WooCommerce dashboard, but OpenAI Ads Manager records zero conversions.

Fixing this requires a dual-track strategy. You need a lightweight browser pixel (oaiq) to capture catalog browsing, and an asynchronous server-side CAPI pipeline hooked to woocommerce_payment_complete to guarantee conversion delivery.

Here is the complete implementation guide to wiring OpenAI Ads Pixel and CAPI on WooCommerce with verified deduplication.


1. WooCommerce Event Mapping to OpenAI Ads Standards

To train OpenAI's campaign bidding algorithms, map standard WooCommerce customer actions to OpenAI Ads event types:

WooCommerce Customer ActionWooCommerce Trigger PointOpenAI Ads Event TypeRequired Parameters
Single Product Page Viewwoocommerce_after_single_productcontents_viewedcontent_id, amount, currency
Add Item to CartAJAX added_to_cart / form submititems_addedcontent_id, quantity, amount, currency
Initiate CheckoutCheckout template loadcheckout_startedamount, currency, contents
Complete Purchase (Browser)woocommerce_thankyouorder_createdorder_id, amount, currency, event_id
Complete Purchase (Server)woocommerce_payment_completeorder_createdorder_id, amount, currency, event_id, user_data

2. Implementing the Browser Pixel on WooCommerce

The OpenAI Ads JavaScript measurement pixel initializes in the <head> of your WordPress theme. You can inject this via your child theme's functions.php or a dedicated custom plugin:

add_action('wp_head', 'comercio_enqueue_openai_ads_pixel');
function comercio_enqueue_openai_ads_pixel() {
    $pixel_id = 'YOUR_OPENAI_PIXEL_ID';
    ?>
    <script>
      !function(w,d,e,v,n,t,s){if(w.oaiq)return;n=w.oaiq=function(){n.callMethod?
      n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!w._oaiq)w._oaiq=n;
      n.push=n;n.loaded=!0;n.version='1.0';n.queue=[];t=d.createElement(e);t.async=!0;
      t.src=v;s=d.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}
      (window,document,'script','https://bzrcdn.openai.com/pixel.js');
      oaiq('init', '<?php echo esc_js($pixel_id); ?>');
      oaiq('event', 'page_viewed');
    </script>
    <?php
}

3. Tracking Purchases on the Thank You Page (woocommerce_thankyou)

When an order is placed, WooCommerce directs the user to the order-received endpoint. Hook into woocommerce_thankyou to fire the browser conversion event:

add_action('woocommerce_thankyou', 'comercio_track_openai_purchase_browser', 10, 1);
function comercio_track_openai_purchase_browser($order_id) {
    if (!$order_id) return;
    $order = wc_get_order($order_id);
    if (!$order) return;

    // Prevent duplicate firing on page reloads
    if ($order->get_meta('_openai_browser_tracked') === 'yes') {
        return;
    }
    $order->update_meta_data('_openai_browser_tracked', 'yes');
    $order->save();

    $amount_cents = round(((float)$order->get_total()) * 100);
    $currency = $order->get_currency();
    $event_id = 'woo_' . $order_id;
    ?>
    <script>
      oaiq('event', 'order_created', {
        amount: <?php echo esc_js($amount_cents); ?>,
        currency: '<?php echo esc_js($currency); ?>',
        order_id: '<?php echo esc_js($order_id); ?>',
        event_id: '<?php echo esc_js($event_id); ?>'
      });
    </script>
    <?php
}

4. Server-Side CAPI Relay (woocommerce_payment_complete)

Browser scripts will be blocked by ad blockers (uBlock Origin, Brave) or missed if a user closes the tab immediately after completing Stripe or PayPal checkout.

To guarantee 100% order capture, dispatch an HTTP POST request directly from WordPress backend to the OpenAI Ads Conversions API:

add_action('woocommerce_payment_complete', 'comercio_forward_order_to_openai_capi', 10, 1);
function comercio_forward_order_to_openai_capi($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;

    $pixel_id = 'YOUR_OPENAI_PIXEL_ID';
    $capi_key = 'YOUR_CAPI_KEY'; // Store in wp-config.php or secure option
    $endpoint = "https://bzr.openai.com/v1/events?pid=" . $pixel_id;

    $amount_cents = round(((float)$order->get_total()) * 100);
    $email = strtolower(trim($order->get_billing_email()));
    $phone = preg_replace('/\D/', '', $order->get_billing_phone());

    $payload = [
        'validate_only' => false,
        'events' => [
            [
                'id' => 'srv_woo_' . $order_id . '_' . time(),
                'type' => 'order_created',
                'timestamp_ms' => round(microtime(true) * 1000),
                'action_source' => 'website',
                'event_id' => 'woo_' . $order_id, // EXACT SAME event_id for deduplication
                'user_data' => [
                    'em' => hash('sha256', $email),
                    'ph' => $phone ? hash('sha256', $phone) : null,
                    'client_ip_address' => $order->get_customer_ip_address(),
                    'client_user_agent' => $order->get_customer_user_agent()
                ],
                'data' => [
                    'amount' => $amount_cents,
                    'currency' => $order->get_currency(),
                    'order_id' => (string)$order_id
                ]
            ]
        ]
    ];

    wp_remote_post($endpoint, [
        'method' => 'POST',
        'timeout' => 5,
        'headers' => [
            'Authorization' => 'Bearer ' . $capi_key,
            'Content-Type' => 'application/json'
        ],
        'body' => wp_json_encode($payload)
    ]);
}

5. Caching and Performance Optimization

  1. Exclude Critical Endpoints from Page Caching:

In plugins like WP Rocket, LiteSpeed Cache, or Cloudflare, ensure /cart/, /checkout/, and /checkout/order-received/ are excluded from HTML caching. Cached order confirmation pages will fail to emit dynamic transaction IDs.

  1. Asynchronous Non-Blocking Requests:

For high-volume stores, avoid executing synchronous wp_remote_post calls during the customer's checkout thread. Use WooCommerce Action Scheduler (as_schedule_single_action) or a Redis queue worker to dispatch CAPI events asynchronously in the background.

For comprehensive store setups covering product catalogs and automated server deduplication, explore our Ecommerce Tracking Setup service.