Facebook Tag for Google Tag Manager Server Side

Using Facebook Conversions API, you can send events to Facebook Event Manager. This is the perfect alternative to Facebook SDK because Conversions API speeds up page loading and helps track users more accurately.

We created a Facebook tag for the GTM Server-Side container that uses Facebook Conversions API to track events.

The key feature in writing this tag was easily integrating it into your current GTM Server setup. It can use Universal Analytics or GA4 server client for retrieving event data.

The data flow looks like this:

  1. User open page
  2. gtag.js tracks ‘page_view’ event and sends it to GTM Server Side
  3. GA4 Client on GTM Server Side receives the request and creates EventData object
  4. Facebook Tag receives EventData object and map it to Facebook Conversions API event object and then sends to Facebook

How to setup Facebook event tracking using Facebook GTM Server tag:

  1. Download Facebook tag template from GitHub repository.
  2. Import Facebook server tag template into Google Tag Manager Server Side container. Click on Templates -> Tag Templates -> New. Then on the right upper corner, click on dots, and choose import.
Import Tag
Import GTM Server Tag
  1. Create a trigger that will fire on Universal Analytics or GA4 client. 
    • For Universal Analytics, choose Client Name = “Universal Analytics”
    • For GA4, choose Client Name = “GA4”
Create GTM Server trigger
Create a trigger that will fire on Universal Analytics or GA4 client
  1. Now we need to obtain Facebook API Access Token. There is the official documentation on how to get it.
  2. Finally, we can create a Facebook tag. Click on Tags->New->Tag configuration and choose Facebook tag.
Create a Facebook tag
  1. You need to set the required configuration parameters for the tag (Pixel ID, API Access Token). We recommend creating Custom Variables for storing Pixel ID, API Access Token.
Set the required configuration parameters for the tag
  1. That’s it. Now you can start sending test events.

How to test Google Tag Manager Facebook tag with "Test Your Events" feature

Facebook provides the ability to test Browser and Server events. This is very useful if you set up Facebook pixel using GTM Server for the first time and want to check that all data tracked correctly.

  1. At first, you need to obtain test_event_code. For this, you need to login to your Facebook manager choose the data source that you liked to test, and open the tab “Test events”. There you will find the test event code. For example: TEST32229
obtaining test_event_code
  1. Fill the Test ID field, in your Facebook tag, with the test event code that you get from the previous step.
Filling the Test ID field, in your Facebook tag
  1. Go to preview mode or publish changes of your Server Side container. If you did everything correctly, you will see test events on the Facebook Test Event page.
Facebook Test Event page

Event Name mapping

By default, tag tries to map GA standard events to Facebook standard events. If it can’t match it, the raw event name that came from GA client will be used. For example, if you send an event name like this gtag(‘event’, ‘UserLikedProduct’); in Facebook event manager you will get event “UserLikedProduct”.

Below you will find the scheme of mapping GA events to Facebook events that we use in this tag by default:

page_viewPageView
add_payment_infoAddPaymentInfo
add_to_cartAddToCart
add_to_wishlistAddToWishlist
sign_upCompleteRegistration
begin_checkoutInitiateCheckout
generate_leadLead
purchasePurchase
searchSearch
view_itemViewContent
contactContact
customize_productCustomizeProduct
donateDonate
find_locationFindLocation
scheduleSchedule
start_trialStartTrial
submit_applicationSubmitApplication
subscribeSubscribe

Google Analytics EE to Facebook events

To make migration of your web Facebook GTM tag to server-side container simpler, we tried to map all data that GA client receives to the Facebook event without any setup from your side. It also supports GA Enhanced Eccomerce events mapping, and of course, the tag has the ability to override all event parameters that will be sent to Facebook.

For example, if the tag detects that the event type is “Purchase”, we will determine the product list, their currency, and value.

In the sections below, we describe how exactly data mapped in every parameter group.

Event Name mapping

Default mapping and overriding Server Event Data

There are only a few parameters that are in the category of Server Event Data. See this documentation for more details on what data parameters exist.  Parameters that are setting by default:
event_nameEvent Name
event_source_urlPage Location
action_sourceWhere your conversions occurred. (For example: website, email, app)
event_timeCurrent Time

Default mapping and overriding User Data

With the “User Data” tag option you can override or add any user data that will be sent to Facebook. See this Facebook documentation for more details on what user data parameters you can add to the call. If the documentation calls for the parameter to be hashed, you must hash it with SHA256 or the hit will not be sent to Facebook.
fbcCookie value ‘_fbc’ or ‘fbclid’ url parameter if exist
fbpCookie value ‘_fbp’
external_idexternal_id or user_id or userId
subscription_idsubscription_id or subscriptionId
lead_idlead_id or leadId
lnlastName or LastName or nameLast
fnfirstName or FirstName or nameFirst
ememail
phphone
gegender
ctcity
ststate
zpzip
countrycountryCode

Default mapping and overriding Custom Data

You also can override any other parameters or add your own using the “Custom Data” section in the tag setup. Check out this documentation for more details on what data parameters you can add to the call.

If EE parameter ‘items’ exists, tag set content_type to ‘product’. GA product parameters item_name, item_category maps to Facebook content_name, content_category accordingly.

Tag try to determine other product parameters like this:

valuex-ga-mp1-ev or value
currencycurrency
transaction_idorder_id

If the event type is “Purchase” but currency can’t be mapped, ‘USD’ used as the default value, that’s because Facebook doesn’t accept “Purchase” events without currency parameters.