Meta Conversions API via sGTM
A comprehensive guide to setting up Meta Conversions API (CAPI) using Server-Side Google Tag Manager
Overview
Meta Conversions API (CAPI) creates a direct connection between your server and Meta’s systems, helping optimize ad targeting, decrease cost per action, and improve measurement accuracy. By implementing CAPI through Server-Side Google Tag Manager (sGTM) you can maintain data privacy and control.
Prerequisites
Before proceeding with this integration, ensure you have:
- A Google Tag Manager account with administrative access
- Google Analytics 4 (GA4) properly configured
- A Meta Business Manager account with a Pixel ID
If your system uses a version older than GA4, you’ll need to upgrade your existing tag manager setup to use GA4 before proceeding with this integration.
Step-by-Step Implementation
sGTM Setup
- Make sure you have completed Hardal with sGTM Setup
Configure Meta Browser ID and Click ID
To ensure proper tracking and deduplication, you’ll need to pass Meta’s browser ID and click ID to your server:
- In your web container, create two User-Defined Variables
- Meta Browser ID:
- Variable Type: 1st-Party Cookie
- Cookie Name:
_fbp
- Meta Click ID:
- Variable Type: 1st-Party Cookie
- Cookie Name:
_fbc
- In your GA4 Configuration tag (Google Tag), add these fields:
- Field Name:
x-fb-ck-fbp
Value: Your Meta Browser ID variable - Field Name:
x-fb-ck-fbc
Value: Your Meta Click ID variable
- Field Name:
If you’ve set up a custom domain for your server container, these IDs may be passed automatically. However, it’s still recommended to explicitly configure them to ensure proper tracking.
Set Up User Data Parameters
First, you’ll need to create variables in your Google Tag Manager web container for each user data parameter you want to track:
- Go to your GTM web container dashboard
- Navigate to Variables in the left sidebar
- Click New under “User-Defined Variables”
- Select Data Layer Variable as the variable type
- Set the Data Layer Variable Name according to the mapping table (see below)
- Set an appropriate name for the variable (e.g., “User Email”)
- Save the variable
Follow the Parameter Mapping Structure
Use this mapping structure to correctly set up your variables:
Meta Parameter | GA4 Field Name | Data Layer Variable Name |
---|---|---|
email (em) | user_data.email_address | eventModel.user_data.email_address |
phone (ph) | user_data.phone_number | eventModel.user_data.phone_number |
external_id | x-fb-ud-external_id | N/A |
first_name (fn) | user_data.address.first_name | eventModel.user_data.address.first_name |
last_name (ln) | user_data.address.last_name | eventModel.user_data.address.last_name |
city (ct) | user_data.address.city | eventModel.user_data.address.city |
state (st) | user_data.address.region | eventModel.user_data.address.region |
zip (zp) | user_data.address.postal_code | eventModel.user_data.address.postal_code |
country (country) | user_data.address.country | eventModel.user_data.address.country |
After adding all Data Layer Variables to your web container, the Variables section should look like following:
Focus on implementing the high-priority parameters first: email, phone number, and external ID provide the best match rates for Meta’s systems.
Add Variables to Your Google Tag Configuration
Once you’ve created these variables, you need to add them to your Google Tag:
- Open your Google Tag
- Under Configuration settings, click Add parameter
- For each parameter, enter the GA4 Field Name from the table above as the Configuration Parameter
- Set the Value to the corresponding variable you created
Implement Data Collection in Your Website
You’ll need to ensure your website is collecting and passing this data to the data layer. For example:
Consider Important Privacy Requirements
Always handle user data with care and in compliance with privacy regulations like GDPR, CCPA, and others.
When implementing user data parameters:
- Always hash personally identifiable information (PII) before sending it to Meta
- While GA4 and the Meta Conversions API tag will automatically hash this data, it’s best practice to hash it yourself before pushing to the data layer
- Implement proper consent management to comply with privacy regulations
- Only collect and send data that you have permission to use for advertising purposes
These user data parameters will significantly improve the effectiveness of your Meta advertising campaigns by providing better data for attribution and audience targeting.
Create GA4 Event Tags
Set up GA4 Event tags to track specific user actions:
- In your web container, create a new GA4 Event tag
- Select your GA4 Event tag
- Set the Event Name to match one of Meta’s standard events:
Meta Event Name | GA4 Event Name |
---|---|
PageView | page_view |
AddToCart | add_to_cart |
AddPaymentInfo | add_payment_info |
AddToWishlist | add_to_wishlist |
CompleteRegistration | sign_up |
InitiateCheckout | begin_checkout |
Lead | generate_lead |
Purchase | purchase |
Search | search |
ViewContent | view_item |
- Add event parameters based on your tracking needs
- Configure the event_id parameter for deduplication (see below)
- Set appropriate triggers for each event
- Save and publish your tags
Set Up Deduplication
To prevent duplicate events when using both Pixel and CAPI, implement event_id deduplication:
- Create a custom JavaScript variable to generate unique IDs:
- Create a Data Layer Variable (e.g., FBEventIdVar) with Data Layer Variable Name:
eventModel.event_id
- In your Meta Pixel tag:
- In your GA4 Event tag, add an event parameter:
- Parameter Name:
event_id
- Value: Your event ID variable
- Parameter Name:
Use the same trigger for both your Meta Pixel tag and GA4 Event tag to ensure consistent firing and proper deduplication.
Configure the GA4 Client in Server Container
In your server container, ensure the GA4 Client is set up correctly:
- Go to your server container
- Navigate to “Clients”
- Edit the “GA4” client (or create it if it doesn’t exist)
- Enable “Default GA4 paths”
- Enable “Default gtag.js paths” and add your GA4 Measurement ID(s)
- Save the client configuration
The GA4 Client is responsible for processing incoming data from your web container and transforming it into a format that other server-side tags can use.
Install Meta Conversions API Tag
The final step is to set up the Meta Conversions API Tag:
- In your server container, navigate to “Tags”
- Click “New” and navigate to “Discover More”. Search for “Conversions API Tag by facebookincubator”
- Generate Meta Access Token
- Log into your Meta Business Manager
- Go to Events Manager
- Select your Pixel
- Navigate to the Settings tab
-
Copy Dataset ID and save it (marked as step 3)
-
Find the “Conversions API” section
-
Click “Generate access token”
-
Copy the generated token
- Create a trigger that fires on events from the GA4 Client
- Save and publish the tag
Keep your access token secure. It provides direct access to your Meta account and should never be exposed in client-side code.
Testing Your Implementation
- Enable Preview mode in your web container
- Enable Preview mode in your server container
- Navigate to your website and perform actions that should trigger events
- Verify that:
- GA4 events are firing in the web container
- Events are being received by the server container
- Meta Conversions API tag is firing in the server container
- Enable Preview mode in your web container
- Enable Preview mode in your server container
- Navigate to your website and perform actions that should trigger events
- Verify that:
- GA4 events are firing in the web container
- Events are being received by the server container
- Meta Conversions API tag is firing in the server container
- Go to Meta Events Manager > Data Sources > Your Pixel > Test Events
- Generate a Test Event Code
- Add this code to your Meta Conversions API Tag during testing
- Perform test actions on your website
- Verify events appear in the Test Events window
- Remove the test code before publishing to production
If events aren’t showing up correctly:
- Tag Not Firing: Check your trigger configurations and verify GA4 Events are firing in the web container
- Tag Fired but Failed: In the Server-side debugger, check the Response Body for error messages
- Duplicate Events: Verify your event_id implementation and ensure deduplication is working
- Missing Parameters: Check your data layer and variable configurations
Custom Data Parameters
To send additional event data to Meta, use these parameter mappings in your GA4 Event tags:
Meta Parameter | GA4 Parameter |
---|---|
value | value |
currency | currency |
search_string | search_term |
order_id | transaction_id |
content_ids | x-fb-cd-content_ids |
content_type | x-fb-cd-content_type |
content_name | x-fb-cd-content_name |
content_category | x-fb-cd-content_category |
contents | items OR x-fb-cd-contents |
num_items | x-fb-cd-num_items |
predicted_ltv | x-fb-cd-predicted_ltv |
status | x-fb-cd-status |
delivery_category | x-fb-cd-delivery_category |
For array or object parameters like contents
and custom_properties
, remember to JSON.stringify them before sending.
Sample Event Implementation
Here’s an example of a purchase event with all recommended parameters:
Frequently Asked Questions
Was this page helpful?