← Back to Insights

WordPress: OpenAI Ads Pixel and CAPI for WordPress: Complete Lead & Event Setup

Step-by-step technical guide to integrating OpenAI Ads Measurement Pixel and Conversions API (CAPI) on WordPress for lead generation forms and events.

2026-03-29 • CMS & Website Builders

WordPress powers a major portion of the web, but configuring paid ad telemetry often leads to data discrepancies.

Caching plugins can cache dynamic JavaScript, security tools may block API endpoints, and visitors filling out forms often navigate away before client scripts finish execution. Relying solely on a theme tracking snippet frequently undercounts conversions by 20% or more.

Accurate measurement on WordPress requires a hybrid architecture. You inject the browser pixel via your child theme's wp_head hook for front-end session attribution, and attach PHP action hooks to fire server-side Conversions API events directly from WordPress whenever forms or orders clear.

Here is how to set up both layers on WordPress without unnecessary plugin overhead.

Adding the base pixel via wp_head

In your child theme's functions.php file (or a custom site snippet plugin), insert:

<?php
// Inject OpenAI Ads Measurement Pixel into wp_head
add_action('wp_head', function () {
    $pixel_id = 'YOUR_OPENAI_ADS_PIXEL_ID';
    if (empty($pixel_id) || is_admin()) {
        return;
    }
    ?>
    
    <script>
      !function(w,d,e,u,t,s){if(w.openaiAds)return;t=w.openaiAds=function(){
      t.callMethod?t.callMethod.apply(t,arguments):t.queue.push(arguments)};
      if(!w._openaiAds)w._openaiAds=t;t.push=t;t.loaded=!0;t.version='1.0';
      t.queue=[];s=d.createElement(e);s.async=!0;s.src=u;
      var f=d.getElementsByTagName(e)[0];f.parentNode.insertBefore(s,f);
      }(window,document,'script','https://bzrcdn.openai.com/pixel.js');

      openaiAds('init', '<?php echo esc_js($pixel_id); ?>');
      openaiAds('track', 'PageView');
    </script>
    <noscript>
      <img height="1" width="1" style="display:none" 
           src="https://bzr.openai.com/v1/events?pixel_id=<?php echo esc_attr($pixel_id); ?>&event=PageView&noscript=1" />
    </noscript>
    <?php
}, 1);

Server-side form submission hooks

To capture leads from ad-blocked users, attach server hooks to your form plugin.

For WPForms (wpforms_process_complete)

add_action('wpforms_process_complete', function ($fields, $entry, $form_data, $entry_id) {
    $email = '';
    $phone = '';

    foreach ($fields as $field) {
        if ($field['type'] === 'email') {
            $email = sanitize_email($field['value']);
        }
        if ($field['type'] === 'phone') {
            $phone = preg_replace('/[^0-9]/', '', $field['value']);
        }
    }

    if (empty($email)) {
        return;
    }

    $eventId = 'wp_lead_' . $form_data['id'] . '_' . $entry_id;

    $payload = [
        'data' => [
            [
                'event_name' => 'Lead',
                'event_time' => time(),
                'event_id' => $eventId,
                'user_data' => array_filter([
                    'em' => hash('sha256', strtolower(trim($email))),
                    'ph' => !empty($phone) ? hash('sha256', $phone) : null,
                    'client_user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : null,
                    'client_ip_address' => isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field($_SERVER['REMOTE_ADDR']) : null,
                ]),
                'custom_data' => [
                    'content_name' => sanitize_text_field($form_data['settings']['form_title'] ?? 'Lead Form'),
                    'form_id' => (string) $form_data['id'],
                ],
                'action_source' => 'system_generated'
            ]
        ]
    ];

    wp_remote_post('https://bzr.openai.com/v1/events', [
        'method'      => 'POST',
        'timeout'     => 5,
        'blocking'    => false,
        'headers'     => [
            'Authorization' => 'Bearer ' . OPENAI_ADS_CAPI_TOKEN,
            'Content-Type'  => 'application/json',
        ],
        'body'        => wp_json_encode($payload),
    ]);
}, 10, 4);

For Gravity Forms (gform_after_submission)

add_action('gform_after_submission', function ($entry, $form) {
    $email = '';
    $phone = '';

    foreach ($form['fields'] as $field) {
        if ($field->type === 'email') {
            $email = rgar($entry, (string) $field->id);
        }
        if ($field->type === 'phone') {
            $phone = preg_replace('/[^0-9]/', '', rgar($entry, (string) $field->id));
        }
    }

    if (empty($email)) {
        return;
    }

    $eventId = 'wp_lead_' . $form['id'] . '_' . $entry['id'];

    $payload = [
        'data' => [
            [
                'event_name' => 'Lead',
                'event_time' => time(),
                'event_id' => $eventId,
                'user_data' => array_filter([
                    'em' => hash('sha256', strtolower(trim($email))),
                    'ph' => !empty($phone) ? hash('sha256', $phone) : null,
                ]),
                'custom_data' => [
                    'content_name' => $form['title'],
                    'form_id' => (string) $form['id'],
                ],
                'action_source' => 'system_generated'
            ]
        ]
    ];

    wp_remote_post('https://bzr.openai.com/v1/events', [
        'method'   => 'POST',
        'timeout'  => 5,
        'blocking' => false,
        'headers'  => [
            'Authorization' => 'Bearer ' . OPENAI_ADS_CAPI_TOKEN,
            'Content-Type'  => 'application/json',
        ],
        'body'     => wp_json_encode($payload),
    ]);
}, 10, 2);

Front-end form event matching

When using AJAX form submissions, emit the matching event_id from the browser once validation passes:

document.addEventListener('submit', function (e) {
  var form = e.target.closest('form');
  if (!form || !form.classList.contains('track-openai-lead')) return;

  var formId = form.getAttribute('data-form-id') || 'general';
  var submissionTimestamp = Date.now();
  var dedupeId = 'wp_lead_' + formId + '_' + submissionTimestamp;

  if (window.openaiAds) {
    window.openaiAds('track', 'Lead', {
      content_name: form.getAttribute('data-form-name') || 'Lead Form'
    }, {
      event_id: dedupeId
    });
  }
});

Verification checklist

Confirm that non-blocking HTTP requests (blocking => false) execute without adding latency to form submissions.

Verify that all customer emails are trimmed and converted to lowercase before applying SHA-256 hashing.

Check OpenAI Ads Event Manager to confirm that browser and server events share identical event_id keys and merge cleanly.

Frequently asked questions

Where should the pixel snippet be added in WordPress?

Add the script to wp_head via your child theme's functions.php or a dedicated snippet plugin to avoid third-party plugin overhead.

How do I track form leads on WordPress with CAPI?

Attach action hooks to form plugins like Gravity Forms (gform_after_submission) or WPForms (wpforms_process_complete), sending an asynchronous POST request to https://bzr.openai.com/v1/events.

How do I deduplicate lead events on WordPress?

Generate a unique submission ID (such as wp_lead_ plus entry ID) and pass that identical token to both the browser tag and the server-side payload.

Does CAPI work alongside caching plugins?

Yes. Server-side hooks run during form POST submission on the server, which operates outside the scope of HTML page caching plugins.

Need ChatGPT tracking installed?

Configured, tested, documented.