- Measure ad performance when browser-based tracking is limited
- Optimize campaign delivery
- Deduplicate the same conversion when you also use the X Pixel
This guide is for advertisers using Google Tag Manager. If you are building a custom server integration, use the Web conversions API reference instead.
How the integration works
A typical setup uses both a web container and a server container:- The web container (or
gtag.js) sends events from the browser to your tagging server. Most advertisers use a Google Analytics 4 (GA4) configuration tag for this. - The GA4 Client in the server container receives those events.
- The X Ads Conversion API (Official) tag fires on those events and
POSTs them tohttps://ads-api.x.com/12/measurement/conversions/{pixel_id}with your Pixel access token.
Prerequisites
Before you start, you need:Recommended: Map a first-party custom domain to your tagging server. First-party serving improves cookie durability for the X Click ID (
twclid) and other identifiers.Step 1: Collect your Pixel ID, Event IDs, and access token
Pixel ID
- Go to ads.x.com.
- Open Tools → Events Manager.
- If you do not already have an X Pixel event source, select Add event source and add one.
- Copy the Pixel ID from the event source. You will paste this into the GTM tag.
Conversion events (Event IDs)
Each conversion you want to optimize or report on should exist in Events Manager. Option A — Reuse an existing Pixel event (recommended for Pixel + Conversion API) If the event is already firing from the X Pixel, copy that event’s Event ID. Using the same Event ID on both the Pixel and the Conversion API is required for deduplication. Option B — Create a Conversion API event- Open the X Pixel event source.
- Select Add events.
- Choose Install with Conversion API.
- Copy the Pixel ID and Event ID, then save.
Event ID in Events Manager is the conversion type (for example a Purchase event). It is not the per-occurrence deduplication key. The per-occurrence key is Conversion ID. See Deduplication.
Access token
The official GTM tag authenticates with theX-Pixel-Token header. Generate a Conversion API access token in Events Manager:
- In Events Manager, open Install Pixel.
- Select Manual as the installation method, then continue.
- Select Conversion API, then continue.
- Select Generate access token and copy the token.
The GTM template uses this Pixel access token. You do not need to implement OAuth 1.0a or call the Ads API from GTM. OAuth credentials are only required for custom Conversion API integrations.
Step 2: Create a GTM server container
If you already have a server container and tagging server URL, skip to Step 3.- Open Google Tag Manager.
- Select your account, then Create Container.
- Name the container and set the target platform to Server.
- Create the container and provision a tagging server. Google Cloud is the default path; other clouds use the manual server setup.
- Copy the tagging server URL (for example
https://sgtm.example.com).
/g/collect) and turns them into events the X tag can read.
Step 3: Send website events to the server container
Configure the web container so conversion events reach the server.GA4 Configuration tag
In the web container, open your Google Analytics: GA4 Configuration tag (or create one):- If the tag supports Send to server container, enable it and set Server Container URL to your tagging server URL.
- Otherwise, under Fields to Set, add:
- Field name:
transport_url - Field value: your tagging server URL
- Field name:
- Field name:
first_party_collection - Field value:
true
GA4 Event tags
For each conversion (Purchase, Lead, Sign up, and so on), add or update a Google Analytics: GA4 Event tag:- Set Event Name to a stable value (
purchase,generate_lead,sign_up, and so on). - Under Event Parameters, map value, currency, items, and user data. See Event parameter mapping.
- If you use both Pixel and Conversion API, add
event_idand set it to a unique per-conversion value. The official X tag uses this as Conversion ID unless you override it.
gtag.js event:
You can keep using your existing GA4 event names. If you leave the X tag’s Event ID empty, the template sends
event_name as gtm_ plus the incoming name, lowercased (for example purchase → gtm_purchase). For Pixel + Conversion API deduplication, set Event ID to the Events Manager Event ID instead.Step 4: Install the official X tag in the server container
- Open your server container.
- Go to Templates.
- Under Tag Templates, select Search Gallery.
- Search for X Ads Conversion API (Official) and add it to the workspace.
Use X Ads Conversion API (Official). Community templates with similar names (for example third-party “X (Twitter) Conversion API” tags) are not this integration and may require different authentication.
Step 5: Create and configure the Conversion API tag
- In the server container, go to Tags → New.
- Choose X Ads Conversion API (Official) as the tag type.
Required configuration
Event data overrides (optional)
Use these only when the incoming event does not already contain the value, or when you need to replace it.Logs settings
Step 6: Choose a trigger
Create a trigger that fires when the GA4 Client has claimed the event. One tag per conversion (recommended when you set Event ID in the tag)- Trigger type: Custom
- Condition: Event Name equals
purchase(orgenerate_lead,sign_up, and so on)
- Trigger: All Events, or a regex of the event names you want to send
- The template will send
gtm_purchase,gtm_generate_lead, and so on
Step 7: Send identifiers for matching
X needs at least one strong identifier to match the conversion to an X user. Include as many as you can.Click ID (twclid)
Always pass Click ID when it is available.
- Website click-throughs from X ads append
twclidto the landing-page URL. - With Set first-party cookies enabled, the server tag stores that value in
_twclid(about 390 days) so later conversions on the same domain can still send it. - A first-party tagging domain makes this cookie more reliable.
_twpid, which the server tag also reads.
Email and phone
- Do not salt hashes.
- Email: trim whitespace; the tag lowercases before hashing.
- Phone: normalize to E.164 on your site or in GTM before the event reaches the server. The tag hashes but does not reformat numbers.
- Already-hashed 64-character hex values are sent as-is.
IP address or user agent alone is not enough. Send Click ID, email, or phone whenever possible. More identifiers improve match rate.
Event parameter mapping
The tag maps GA4-style event data to the Conversion API automatically.Suggested GA4 event names
These names work well if you leave Event ID empty (gtm_{name}) or if you trigger one X tag per name:
For Pixel + Conversion API, prefer the Events Manager Event ID on the X tag over relying on
gtm_{name}.
Deduplication
If the same conversion is sent by the X Pixel and the Conversion API, send both of the following on both channels:- The same Events Manager Event ID
- The same Conversion ID for that occurrence (order ID, UUID, or the GA4
event_id)
conversion_id). On the GTM Conversion API tag, set Conversion ID, or send event_id on the GA4 event and let the tag use it.
Use the same trigger in the web container for the Pixel tag and the GA4 Event tag so both sides fire together with the same ID.
You do not need deduplication if Pixel and Conversion API send different events.
Step 8: Test in Preview
Use GTM Preview on both containers before you publish.- In the server container, select Preview. If you use a custom domain, preview against that URL.
- In the web container, select Preview and connect your website.
- Trigger a test conversion (for example complete a test purchase).
- In the web debugger, confirm the GA4 Event tag fired and that
event_id, value, currency, and user data are present. - In the server debugger:
- Confirm the GA4 Client claimed the request.
- Confirm X Ads Conversion API (Official) fired.
- Open the tag → Request and verify an outgoing
POSTtohttps://ads-api.x.com/12/measurement/conversions/{your_pixel_id}. - Confirm the response status is 2xx.
- In Events Manager, confirm the test conversion appears on the event (allow a short delay).
If the tag does not fire
- The web GA4 Event tag did not fire, or is not sending to the server URL.
- The server trigger does not match
event_name. - Preview was started before the latest workspace changes; restart Preview.
If the tag fires and fails
Open the outgoing request in the server debugger and inspect the response body. Common causes:- Invalid Pixel ID or access token
- Missing Event ID and missing event name
- No usable identifier (no
twclid, email, or phone) - Event ID that does not belong to that Pixel
Step 9: Publish
- Remove any test-only overrides.
- Set logs to Do not log (or keep debug-only).
- Submit and publish the server container.
- Submit and publish the web container if you changed it.
- After go-live, confirm Events Manager is receiving production events and that duplicate Pixel + API events are collapsing when Conversion IDs match.
Checklist
- Pixel ID and Conversion API access token stored in GTM variables
- Each conversion has an Events Manager Event ID (especially for Pixel + API)
- Web container sends events to the server container URL
- GA4 Client is enabled in the server container
- Official tag installed (X Ads Conversion API (Official))
- Triggers match the events you intend to send
twclidcaptured (URL and/or_twclidcookie)- Email and/or E.164 phone included when available
- Pixel and API share Conversion ID when both fire
- Preview shows HTTP 2xx to
ads-api.x.com - Events Manager shows the events
- Server container published; first-party domain used in production
Frequently asked questions
Do I still need the X Pixel? The Conversion API can run on its own. We recommend Pixel + Conversion API with shared Event ID and Conversion ID so you keep coverage when one channel drops events. Does this require Ads API access or OAuth? Not for this GTM template. The tag sendsX-Pixel-Token with the Events Manager access token. Custom code integrations still use the Web conversions authentication flow.
Can I host the tagging server outside Google Cloud?
Yes. GTM server-side supports manual provisioning on other clouds.
Why is Click ID missing in Preview?
The test page URL may not include twclid, the _twclid cookie may be blocked on the default tagging domain, or Set first-party cookies is off. Load a URL that includes ?twclid=... and use a first-party server domain.
Can one server tag send every event?
Yes, if you leave Event ID empty and trigger on multiple event names. The API then receives gtm_{event_name}. For named Events Manager events and Pixel deduplication, use one tag (or a lookup table variable) per Event ID.
How is this different from third-party X/Twitter GTM tags?
The official tag is X Ads Conversion API (Official), available in the Google Tag Manager Community Template Gallery. It authenticates with a Pixel access token and is supported for this Conversion API integration.