← Makalelere Dön

WooCommerce: WooCommerce için OpenAI Ads Pikseli ve CAPI: Tam Kurulum Rehberi

WooCommerce mağazanızda OpenAI Ads ölçüm pikseli ve Dönüşümler API'sini (CAPI) doğrulanmış tekilleştirme ile kurmak için adım adım teknik kılavuz.

2026-09-28 • Rehber

Anyone running paid traffic to a WooCommerce store knows how fragile client-side tracking can be. A buyer clicks an ad in ChatGPT, browses your store, and hits the checkout page. But then they pay with Apple Pay or PayPal, get redirected back, and close their mobile tab before the woocommerce_thankyou hook even finishes executing. Or they run an aggressive browser extension that blocks third-party scripts outright.

The result? The sale happens, your bank account registers the revenue, but OpenAI Ads Manager records zero conversions.

Fixing this requires a dual-track strategy. You need the front-end browser pixel (oaiq) to capture early funnel clicks, and a server-side Conversions API (CAPI) dispatch triggered directly from PHP when the payment clears. By tying both sides together with a matching event_id, OpenAI's algorithm gets 100% of the conversion data without double-counting a single order.


1. WooCommerce to OpenAI Ads Standard Event Mapping

OpenAI Ads expects specific standard event names. Here is how WooCommerce actions translate into OpenAI Ads telemetry:

Customer Action on WooCommerceWooCommerce Trigger PointOpenAI Ads EventClient / Server ChannelRequired Parameters
View Product Detailswoocommerce_after_single_productcontents_viewedBrowser Pixelcontent_id, amount, currency
Add Product to CartAJAX added_to_cart / Form Submititems_addedBrowser Pixelcontent_id, quantity, amount, currency
Proceed to CheckoutCheckout Template Loadcheckout_startedBrowser Pixelamount, currency, contents
Complete Purchase (Front-End)woocommerce_thankyouorder_createdBrowser Pixelorder_id, amount, currency, event_id
Complete Purchase (Back-End)woocommerce_payment_completeorder_createdServer CAPIorder_id, amount, currency, event_id, user_data

2. Installing the Client-Side Pixel on WooCommerce

The base pixel script belongs in the <head> of your WordPress site. The cleanest way to inject it is through your child theme's functions.php or a site-specific code snippet plugin like Code Snippets or WPCode.

Add this code to your child theme's functions.php:

add_action('wp_head', 'comercio_enqueue_openai_pixel');
function comercio_enqueue_openai_pixel() {
    $pixel_id = 'YOUR_OPENAI_PIXEL_ID'; // Replace with your Ads Manager 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 Product Views and Cart Additions

To give OpenAI's bidding algorithm the product signals it needs, hook into WooCommerce template actions.

A. View Product Event (contents_viewed)

Add this action hook to fire whenever a shopper views a single product page:

add_action('woocommerce_after_single_product', 'comercio_track_product_view');
function comercio_track_product_view() {
    global $product;
    if (!$product) return;

    $price_cents = round(((float)$product->get_price()) * 100);
    $currency = get_woocommerce_currency();
    $product_id = (string)$product->get_id();
    ?>
    <script>
      oaiq('event', 'contents_viewed', {
        content_id: '<?php echo esc_js($product_id); ?>',
        amount: <?php echo esc_js($price_cents); ?>,
        currency: '<?php echo esc_js($currency); ?>'
      });
    </script>
    <?php
}

B. Thank-You Page Purchase Event (order_created)

When a customer reaches the confirmation screen, WooCommerce fires woocommerce_thankyou. Watch out: shoppers frequently refresh this screen or re-open it from their email receipts. We guard against duplicate client triggers by setting a post meta flag (_openai_tracked_client) as soon as the first event fires:

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

    // Check if already fired to prevent duplicate reload triggers
    if ($order->get_meta('_openai_tracked_client') === 'yes') return;
    $order->update_meta_data('_openai_tracked_client', 'yes');
    $order->save();

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

4. Server-Side CAPI Implementation for WooCommerce

Browser pixels are only half the battle. If a customer uses Brave, runs an ad blocker, or closes their phone the second payment approves, the browser pixel never executes. That is why server-side Conversions API (CAPI) is essential for any production store.

In WooCommerce, the most reliable hook is woocommerce_payment_complete. It triggers on the backend the moment payment gateway confirmation arrives, regardless of what happens in the shopper's browser:

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

    $pixel_id = 'YOUR_OPENAI_PIXEL_ID';
    $capi_key = 'YOUR_OPENAI_CAPI_KEY';
    $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());
    $dedup_key = 'woo_order_' . $order_id; // MATCHES browser event_id exactly

    $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' => $dedup_key,
                '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. Deduplication and Verification

What stops OpenAI from double-counting the order when both browser and server fire?

The answer is the shared event_id.

In our code, both the browser pixel and the server CAPI dispatch pass event_id: "woo_order_" . $order_id. When OpenAI's ingestion pipeline receives both events within a 48-hour window, it matches the keys, merges the browser attribution signal with the verified server-side order total, and counts exactly one purchase.

If the customer's browser script gets blocked by uBlock Origin or Safari ITP, the server CAPI payload guarantees the sale is recorded. If the server request experiences network latency, the browser event claims the conversion immediately. You get complete coverage with zero double-counting.

Need help auditing your WooCommerce tracking setup? Explore our Done-For-You Ecommerce Tracking service.