πŸš€ Advanced Guide to Facebook Conversion API (CAPI) Setup via GTM Server for eCommerce

Standard

🧠 Why Use Facebook CAPI?

With growing privacy restrictions (like iOS14+ and browser limitations), relying solely on Facebook Pixel results in lost conversions and incomplete event data. Facebook Conversion API (CAPI) sends events directly from your server to Facebook, ensuring higher event match quality and more accurate tracking.

🧰 Prerequisites

Before starting, ensure you have the following:

  • Facebook Business Manager access
  • Facebook Pixel ID
  • Facebook Events Manager API access
  • Google Tag Manager (Web + Server Container)
  • GTM server container deployed (e.g., on Google Cloud, Vercel, AWS)
  • Your eCommerce platform access (e.g., Shopify, WooCommerce, Magento)
  • Basic knowledge of JavaScript, GTM, HTTP requests

βš™οΈ Step 1: Deploy GTM Server-Side Container

Option 1: Using Google Cloud Platform (GCP)

  1. Go to GTM server setup
  2. Create a new Server Container
  3. Choose Google Cloud Platform
  4. Select App Engine Standard, region (e.g., us-central), and proceed
  5. Once deployed, you’ll get a GTM Server URL, e.g., https://gtm.yourdomain.com

πŸ’‘ Tip: Set up a custom subdomain like gtm.yourstore.com and point it to your GCP instance for better domain consistency and debugging.

🧱 Step 2: Configure GTM Web Container

A. Add GTM Web Container to Site

Install your GTM web container in your site’s <head> and <body> sections. Use the standard code from the GTM interface.

B. Create Events to Forward

  1. In GTM Web, create tags that capture key eCommerce events:
  • PageView
  • ViewContent
  • AddToCart
  • InitiateCheckout
  • Purchase

These can be either through built-in triggers or using dataLayer.push() from your platform.

Example Data Layer for Purchase:

<!-- wp:code -->
<pre class="wp-block-code"><code>/window.dataLayer = window.dataLayer || &#91;];
window.dataLayer.push({
  event: 'purchase',
  ecommerce: {
    transaction_id: 'T12345',
    value: 99.99,
    currency: 'USD',
    content_ids: &#91;'sku123'],
    contents: &#91;{id: 'sku123', quantity: 1}],
  }
});
</code></pre>
<!-- /wp:code -->

πŸ›°οΈ Step 3: Enable GA4 to Server-Side Forwarding (Optional)

To avoid duplicate tagging:

  1. Enable GA4 event forwarding in Web GTM to Server GTM.
  2. Use the GA4 Configuration Tag, and set transport_url to your server container:

πŸ› οΈ Step 4: Set Up Facebook Tag in GTM Server Container

A. Install Facebook Tag Template

  1. Go to Server Container β†’ Templates
  2. Search the Community Template Gallery
  3. Install: Facebook Conversion API Tag by Stape.io or other verified authors
  4. Click β€œAdd to Workspace”

B. Configure the Facebook CAPI Tag

Create a new Tag in the server container:

  • Tag Type: Facebook CAPI
  • Pixel ID: Your Facebook Pixel ID
  • Access Token: From Facebook Events Manager (see below)
  • Event Name: dynamic from incoming request
  • Test Event Code: (for initial debugging)
  • Event ID: Use event_id from web layer (to deduplicate)
  • User Data: Email, phone, IP, user agent, etc.

C. Get Access Token

  1. Go to Facebook Events Manager
  2. Choose your Pixel β†’ Settings β†’ Conversion API
  3. Generate a System User Access Token

πŸ”„ Step 5: Send Events from Web to Server

A. Modify Web GTM to Send HTTP Requests

Use the GA4 Client in the Server GTM container to receive incoming hits

In Web GTM, set GA4 Configuration Tag:

{
  measurement_id: "G-XXXXXXX",
  transport_url: "https://gtm.yourdomain.com",
}

All events (purchase, add to cart, etc.) will be sent via GA4 to the server GTM.

B. Create Triggers for Events

In the server container, you need to create triggers that match incoming event names.

  1. Trigger Type: Custom Event
  2. Event Name: match purchase, add_to_cart, etc.
  3. Set Variables: capture user data, event parameters from payload

🧬 Step 6: Data Enrichment and Matching

To increase match quality, include:

  • Client IP Address
  • User-Agent
  • fbc/fbp cookies
  • Hashed user data: email, phone number

You can use the built-in Facebook User Data variable or create custom JavaScript variables.

Example: Hashed Email

function() {
  var email = {{User Email}};
  return email ? sha256(email.trim().toLowerCase()) : undefined;
}

πŸ§ͺ Step 7: Test with Facebook Events Manager

  1. Go to Events Manager
  2. Select your Pixel β†’ Test Events
  3. Enter your Test Event Code in GTM Tag configuration
  4. Perform actions on your site (purchase, add to cart)
  5. Events should appear in real-time

🧹 Step 8: Finalize and Publish

  • Remove test code
  • Turn on deduplication using event_id across both browser and server
  • Publish both Web and Server containers
  • Monitor in Events Manager for real-time and historical performance

🧾 Bonus: Sample CAPI Tag Code in GTM Server (Custom)

If you’re writing a fully custom HTTP tag in Server GTM, here’s a basic fetch example using JavaScript:

const payload = {
  event_name: 'Purchase',
  event_time: Math.floor(Date.now() / 1000),
  user_data: {
    em: ["HASHED_EMAIL"],
    ph: ["HASHED_PHONE"],
    client_ip_address: request.headers['x-forwarded-for'],
    client_user_agent: request.headers['user-agent'],
  },
  custom_data: {
    value: 99.99,
    currency: 'USD',
    content_ids: ['sku123'],
    contents: [{id: 'sku123', quantity: 1}]
  },
  event_source_url: request.referer,
  action_source: 'website'
};

fetch('https://graph.facebook.com/v18.0/<PIXEL_ID>/events?access_token=<ACCESS_TOKEN>', {
  method: 'POST',
  body: JSON.stringify({ data: [payload] }),
  headers: {
    'Content-Type': 'application/json'
  }
});

🧠 Pro Tips

Use event deduplication (same event_id in pixel and CAPI)

Enable debugging with Test Event Code

Use server logs in GTM Server (Preview β†’ Network tab)

Set up automatic retries for failed CAPI requests using templates

Monitor Event Match Quality in Events Manager regularly

🧾 Summary


Step Action
1 Deploy Server GTM
2 Track events in Web GTM
3 Configure GA4/HTTP forwarding
4 Install and configure Facebook Tag in Server GTM
5 Send events from Web to Server
6 Enrich user data
7 Test using Test Event Code
8 Publish and Monitor

Leave a Reply

Your email address will not be published. Required fields are marked *