How to set up Facebook event deduplication in Google Tag Manager

Using Facebook server-side tracking (Facebook Conversions API) is getting more popular due to its ability to prevent tracking pixel blocking by ITPs and AdBlockers. Another massive benefit of Facebook conversion API is that it increases FB cookie lifetime. If you haven’t yet implemented FB Conversions API on your site, follow this guide. 

In this article, I will describe how to set up Facebook events deduplication if you are using both browser and server tracking methods for your site.

Why do you need to set up Facebook event deduplication?

The Facebook pixel and Conversions API lets you share standard and custom events with FB so that you can measure and optimize ad performance. The pixel enables you to share web events from a web browser, while the Conversions API lets you share web events directly from the cloud server. 

Both Facebook pixel and Facebook conversion API let you send events. And by events, I mean customer actions. The difference is that pixel sends events from the browser, and CAPI sends events from the server. If you tell Facebook that the browser and server method send identical events, they will accept one and discard another.

There are several ways of setting up Facebook tracking, including the following:

    1. Using solely browser (pixel) method. It is not recommended by FB because of user tracking restrictions. 
    2. Using only server tracking. The main benefit of having tracking only on the server is that FB pixel will not load your browser. Your site will load faster, and it will lead to a better user experience. 
    3. Combine browser and server tracking. Here you have several options: 
      • Send some events from the browser and another form server. For example, Shopify native FB Conversion API integration will send all FB events through the browser and purchase events from the Server. In this case, you do not need the deduplication. 
      • Send all events from the browser and server. 

If you decide to use both browser and server events tracking for all your events, in this case, you will need to set up event deduplication. With Facebook event deduplication, FB will receive all your events, understand that events are identical, clean some of them, and keep others. If you combine the browser and server method but do not set up event deduplication, your data will not be correct because all the events will be tracked twice.

Facebook requires sending two event parameters for event deduplication: its event name and event ID. You already sent the event name with the existing setup. This article will cover how to configure Facebook event ID inside the Google Tag Manager container.

How does Facebook event deduplication work?

Facebook has two way of identifying duplicated events that were sent from browser and server:

1.Event ID and event name. 

Facebook recommends this method, and we will cover it in our how-to. For this method, you must use event ID for all events you send via pixel and CAPI. FB will deduplicate Facebook Pixels and Conversion API events with identical event names and event IDs. Facebook gives more credit to the browser event, and if they see similar events and come within 5 minutes difference, they will keep browser events and clean server events. 

2.FBP or External ID

This method exists but is not recommended by Facebook. In this case, you will need to send the event name, fbp, and/or external_id for all your browser and server events. Facebook compares all these parameters and identifies if they already have a browser event with these parameters, and if they do, they discard server events. FB does not recommend using these events since they can identify deduplication only if the browser event was sent first. It means that if you send a server event first, events will be duplicated. 

Facebook also can merge events. For example, if you send some enriched data from the server, you can add the missing data to the browser event. That is not a default feature. To enable it, you should contact a Facebook representative.

Want us to implement Facebook CAPI on your site?
Find out how much it'll cost!

How to set up Facebook event deduplication via Google Tag Manager?

We created a custom Google Tag Manager variable that will add a unique event ID to your web and server Facebook events. It will allow you to track users both from browser and server and deduplicate events. This method will work if your Facebook pixel is set up inside the Google Tag Manager Web Container and if Facebook conversion API is set up via Google Tag Manager Server Container.

    1. You need to have Facebook conversion API setup via Google Tag Manager server container. Please follow this how-to if it’s not yet set up on your site. 
    2. Click on this link to download a custom variable that sends a unique event ID or add it using Template Gallery.
    3. Go to your Google Tag Manager Web Container and import the variable that you’ve just downloaded. Click Templates -> New (inside the Variable Templates) -> click on the three dots on the top right corner -> import -> select variable -> When done, click Save. Go to the Variables tab inside the Web container -> Under User-Defined Variables click New -> create a Unique event ID variable using the template you’ve imported.  
facebook event deduplication variable
facebook event deduplication add variable

4. Add this variable to all Facebook events that you have inside the Web Container. 

There are two ways of adding this variable: using a custom template for FB tags and adding FB tags as custom HTML. 

If you are using FB template: 

Open Facebook Tag -> Click More Settings -> Add variable to the fields Event ID -> Click Save. 

If you’ve added FB tags via custom HTML:

Open Facebook Tag -> add event ID parameter to the Facebook event -> Click Save. 

eventID: ‘{{Event ID}}’

Add an Event ID variable to all your Facebook events. 

facebook event deduplication custom html

Follow the next steps if you use Universal Analytics. If you use GA4, please scroll down to step 9

5. We will configure a setup that will pass the same event ID to the Google Tag Manager Server container in this step. Since our Facebook server tag works based on the Google Analytics events, we will add Facebook events ID to Google Analytics tags inside the Web container. In this example, I will send an Event ID inside the Google Analytics Custom Dimension. You can send it via other fields. 

Open Google Analytics tag inside the Web container -> Click More settings -> Custom Dimension -> Add index and select Event ID variable

Add Facebook event ID to all GA custom dimensions (or other parameters you’ve selected) that you want to track inside the server container. 

Once you’ve done it, go to the preview mode and verify that everything works correctly. Then, Publish the updates.

how to test facebook event deduplication
test facebook event deduplicaton

6. Go to your Google Tag Manager Server Container -> Click Variables -> New Variable -> select variable type Query Parameter -> Parameter name cd1 (in my case because the index of the custom dimension that I’ve created in the web container is 1) -> Click Save.

7. Go to the Preview mode and check that the Google Tag Manager Server container sees the variable you’ve created. 

facebook event id in tag manager
facebook event id variable

8. Open Facebook Tag inside the Google Tag Manager Server Container -> Click Server Event Data Override -> in the Property Name select Event ID -> in the Property Value select variable that you’ve created in the server Container.

Add Event ID Variable to all Facebook tags inside the server container. Once the tag goes to the server preview mode, that test changes. 

facebook event ID

The next steps are for setup event id thru GA4. If you use Universal Analytics, please proceed to step 13

9. We will configure a GA4 tag to pass the event ID to the Google Tag Manager Server container. If your Facebook server tag works based on the GA4 events, you need to add Facebook events ID to GA4 tags inside the Web container. In this example, I will send an Event ID inside the GA4 event parameter for the page_view event. But to do that you need to be sure that the page_view event doesn’t send with the GA4 configuration tag. If you send a page view event with a GA4 configuration tag, please edit your tag and uncheck this mark.

GA4 base tag

Create or use your existing GA4 event tag inside the Web container -> Event Parameters -> Add event_id as Parameter name and select Event ID variable as a value. Also, check what trigger you use for the page_view tag. It should be DOM Ready or Window Loaded. You can’t use the same trigger for the GA4 configuration tag and GA4 event tag for proper event deduplication setup. Remember that you always must use the same trigger for Facebook tag and GA4.

GA4 page_view

Add Facebook event ID to all GA4 event tags that you want to track inside the server container.

Once you’ve done it, go to the preview mode and verify that everything works correctly. Then, Publish the updates.

10. Go to your Google Tag Manager Server Container -> Click Variables -> New Variable -> select variable type Event Data -> Key path event_id (in my case because the parameter name that I’ve created in the web container is event_id) -> Click Save.

FB CAPI event id variable

11. Open Facebook Tag inside the Google Tag Manager Server Container -> Click Server Event Data Override -> in the Property Name select Event ID -> in the Property Value select variable that you’ve created in the server Container.

Add Event ID Variable to all Facebook tags inside the server container. Once the tag goes to the server preview mode, that test changes. 

FB CAPI event id tag

12. Go to the preview mode and check that the event_id variable you created on the web container is the same as what you send to Facebook.

FB CAPI demo
FB CAPI event id demo

13. In this step, we will verify that Facebook event deduplication worked. Go to the Test Event tool inside your Facebook Business Manager -> Make sure that you’ve added test ID to the Facebook tag inside the Google Tag Manager Server container -> Check that Facebook shows server and browser events in the testing tool and one of the events was deduplicated. 

test facebook events deduplication

14. Don’t forget to publish your changes. 

Conclusion:

Facebook conversion API is an excellent solution if you want to track your site users more accurately, bypass ad blockers and ITPs, and extend cookie lifetime.  With a combined server and browser method, you will still run Facebook pixels in the browser, making your site heavier. Using this method, you can be 100% sure that you did not miss any users or events on your site. 

If you have some questions or comments about implementing Facebook event deduplication, please let me know in the comments below.

If you want to test what the GTM server is and how it works, please register an account.

If you need help setting up a GTM server account on your website, contact us right away!