Tag Managers, CDPs & Server-Side Automation Google Tag Manager

Google Tag Manager: OpenAI Ads Pixel and Server-Side CAPI via Google Tag Manager (GTM): Complete Setup

By: Comercio Services Engineering Team
Updated: 23/04/26
Share

Google Tag Manager serves as a primary management console for enterprise analytics and tracking tags.

However, client-only GTM setups face mounting hurdles. Browser ad blockers frequently prevent gtm.js from loading, and storage policies like Safari Intelligent Tracking Prevention limit cookie persistence. Passing raw customer identifiers through client tags also triggers serious compliance concerns.

Enterprise teams implement hybrid architectures combining a Web GTM container for browser-side engagement tracking with a Server GTM container (sGTM) to route Conversions API events under a first-party subdomain.

Executing CAPI requests server-side avoids ad-blocker domain blacklists, preserves session state, and cleans customer data before external transmission.

Here is how to configure both Web and Server GTM containers for OpenAI Ads tracking.

Configuring the GTM web container

In your GTM Web Container, create a Custom HTML tag to initialize the base measurement pixel:

Name the tag OpenAI Ads - Base Pixel and set the trigger to Initialization - All Pages:


<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', '{{OpenAI Ads - Pixel ID}}');
  openaiAds('track', 'PageView');
</script>
<noscript>
  <img height="1" width="1" style="display:none" 
       src="https://bzr.openai.com/v1/events?pixel_id={{OpenAI Ads - Pixel ID}}&event=PageView&noscript=1" />
</noscript>

Create a second Custom HTML tag to record purchase events, triggered by the custom event purchase:

<script>
  (function() {
    var ecommerce = {{ecommerce}} || {};
    var txId = ecommerce.transaction_id || ('tx_' + Date.now());
    var dedupeId = {{dataLayer - event_id}} || txId;

    var rawValue = parseFloat(ecommerce.value || 0);
    var valueCents = Math.round(rawValue * 100);

    var items = ecommerce.items || [];
    var contentIds = items.map(function(item) {
      return item.item_id ? String(item.item_id) : '';
    }).filter(Boolean);

    if (window.openaiAds) {
      window.openaiAds('track', 'Purchase', {
        currency: (ecommerce.currency || 'USD').toUpperCase(),
        value: valueCents,
        order_id: String(txId),
        content_ids: contentIds,
        content_type: 'product'
      }, {
        event_id: dedupeId
      });
    }
  })();
</script>

Skip raw Cloud Run maintenance by hosting Server GTM on Stape.io, or execute CAPI calls at the CDN edge with Cloudflare Zaraz.

Configuring the Server GTM container

In your Server GTM container, define variables to extract incoming event data:

Define Event Data variables for event_id, user_data, value, and currency.

In the sGTM Template Editor, create a tag template using sandboxed JavaScript:

const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const sha256 = require('sha256');
const getEventData = require('getEventData');
const log = require('logToConsole');

const apiToken = data.apiToken;
const eventName = data.eventName || getEventData('event_name') || 'Purchase';
const eventId = data.eventId || getEventData('event_id') || getEventData('transaction_id');

const rawEmail = getEventData('user_data.email') || getEventData('email');
const hashedEmail = rawEmail ? sha256(rawEmail.trim().toLowerCase(), { outputEncoding: 'hex' }) : undefined;

const rawPhone = getEventData('user_data.phone') || getEventData('phone');
const hashedPhone = rawPhone ? sha256(rawPhone.replace(/[^0-9]/g, ''), { outputEncoding: 'hex' }) : undefined;

const rawValue = getEventData('value') || getEventData('ecommerce.value') || 0;
const valueCents = Math.round(Number(rawValue) * 100);
const currency = (getEventData('currency') || getEventData('ecommerce.currency') || 'USD').toUpperCase();

const payload = {
  data: [
    {
      event_name: eventName,
      event_time: Math.floor(Date.now() / 1000),
      event_id: String(eventId),
      user_data: {
        em: hashedEmail,
        ph: hashedPhone,
        client_user_agent: getEventData('user_agent'),
        client_ip_address: getEventData('ip_override')
      },
      custom_data: {
        currency: currency,
        value: valueCents,
        order_id: String(eventId),
        content_type: 'product'
      },
      action_source: 'system_generated'
    }
  ]
};

const postUrl = 'https://bzr.openai.com/v1/events';
const postOptions = {
  headers: {
    'Authorization': 'Bearer ' + apiToken,
    'Content-Type': 'application/json'
  },
  method: 'POST',
  timeout: 4000
};

sendHttpRequest(postUrl, (statusCode, headers, body) => {
  if (statusCode >= 200 && statusCode < 300) {
    data.gtmOnSuccess();
  } else {
    log('OpenAI CAPI Error: ' + statusCode + ' ' + body);
    data.gtmOnFailure();
  }
}, postOptions, JSON.stringify(payload));

Data layer deduplication structure

Ensure the client dataLayer push generates a consistent event_id accessible by both tags:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'purchase',
  event_id: 'order_10024',
  ecommerce: {
    transaction_id: '10024',
    value: 149.50,
    currency: 'USD',
    items: [{ item_id: 'PROD_101', item_name: 'Premium Course', price: 149.50 }]
  },
  user_data: {
    email: 'customer@example.com',
    phone: '+15550192834'
  }
});

Verification and testing

Use Tag Assistant to verify that the web purchase tag fires properly and passes the configured event_id.

In sGTM Preview Mode, submit a test order and verify that incoming server tags execute and receive HTTP 200 responses from bzr.openai.com.

Inspect OpenAI Ads Event Manager to confirm that dual events fuse under the shared transaction identifier.

Verify web and server containers pass identical transaction keys using event_id deduplication rules and sign off containers against our pre-launch tracking QA checklist.

Frequently asked questions

How do I set up the OpenAI Ads Pixel in GTM Web Container?

Create a Custom HTML Tag in your web container, insert the initialization script, and trigger it on Initialization - All Pages.

How do I implement CAPI using Server GTM?

Capture incoming GA4 or Data Client payloads in your server container and map properties into an HTTP tag that sends POST requests to https://bzr.openai.com/v1/events.

How are events deduplicated between web and server GTM containers?

Generate a shared event_id in the client dataLayer push and reference that variable in both your web tag and your server tag.

How should user data be hashed inside sGTM?

Use the native sha256() sandboxed JavaScript API in your sGTM template to normalize and hash emails and phone numbers before transmission.

Deploy GTM & Server-Side sGTM OpenAI Ads Tracking

Web container dataLayer mapping, server container HTTP request tags, and bulletproof event_id deduplication.