Utilising Facebook Conversion API For Optimising Targeted Ads at Simplilearn

Mayank Bansal
Simplilearn Engineering
6 min readMay 26, 2022
Photo by Will Francis on Unsplash

What Is the Facebook Conversions API?

The Conversions API creates a connection between an advertiser’s marketing data and the Facebook systems that optimise ad targeting, decrease cost per click and measure results. In the case of direct integrations, this entails establishing a connection between an advertiser’s server and Facebook.

How Does the Facebook Conversions API Work?

Most businesses have the Facebook pixel installed directly on their website or indirectly through Google Tag Manager. When a user loads your website, the pixel fires at the browser level. With each page the user visits and action the user completes (add to cart, purchase, lead, etc.), the browser sends an update back to Facebook. Facebook then tells you in Ads Manager how many purchases, leads, add to carts, and so on your ad generated.

With the Conversions API, when a user clicks on a Facebook ad and lands on your website, Facebook will set a unique ID for that user. The system will track the user via this ID as they visit different pages on your website and note if they complete a purchase, opt for a lead magnet, add a product to their shopping cart, and other actions.

Image Credits: Daniel Rostron

Why Do we need Facebook Marketing API

Conversions API is a new name but not a new way of tracking, even for Facebook. The previous name was Server-Side Events, which better explains what’s going on. With the Conversions API, the website’s server (as opposed to the browser) tracks the user as they visit your website. The server notes what actions the user takes (add to cart, purchase, lead) and then the server sends the information back to Facebook.

Recently Facebook has been sending up alarms about the upcoming iOS 14 changes and how they’ll impact your ads. Here’s the gist of what’s going on.

With iOS 14, Apple is making it much harder for apps and websites on Apple devices to track user behaviour. Apple is doing a few things to make this happen, including giving iOS 14 users the ability to turn off tracking at the device level and putting limitations on event tracking, regardless of a user’s settings.

Increased user controls from privacy laws like the EU’s GDPR and California’s Privacy Rights Act, as well as rising usage of ad-blocking software, have already been limiting Facebook’s ability to track events.

How to set up a Facebook Conversion API

There are two ways to set up the Facebook conversions API. Which you choose will depend on the platforms you use and the level of technical help you can access within your business.

Remember that you should already have a functional Facebook pixel before you dive in.In Events Manager, choose the pixel from the Data Sources tab and click Settings in the top menu.Under Settings, scroll down to Conversions API and click Generate Access Token.

Fig -1: Facebook Developer Consle
Fig-2: Facebook Developer Console

Copy the access token.

Fig-3: Facebook Developer Console

This all we need for setting up our System to start sending Events to Facebook.

Setting up the Server Architecture

There can be multiple ways to setup a system which takes events from different sources and send them to Facebook System.We Will be using AWS Lambda and Facebook Business SDK.

Facebook Business SDK for NodeJS

The Facebook Business SDK is a one-stop shop to help our partners better serve their businesses. Partners are using multiple Facebook API’s to serve the needs of their clients. Adopting all these API’s and keeping them up to date across the various platforms can be time consuming and ultimately prohibitive. For this reason Facebook has developed the Business SDK bundling many of its APIs into one SDK to ease implementation and upkeep. The Business SDK is an upgraded version of the Marketing API SDK that includes the Marketing API as well as many Facebook APIs from different platforms such as Pages, Business Manager, Instagram, etc.

Architecture Overview

Fig-4 : Architecture Overview

Events Publisher — Any Event sent from your website when user click on course or submit a lead event.

Custom Lambda Function — Lambda Function which is deployed to AWS and Hooked with api Gateway to provide and end point to our Events Publisher and other system

Processed Event — Dynamo DB Table for storing the successfully sent event to Facebook.

Invalid Events — Dynamo DB Table For storing Events that were failed while sending to Facebook due to any failure like wrong email address or Facebook pixel id.

Current Events — Dynamo DB Table For storing The events before sending to Facebook, each events is saved with status and retry count.

Retry Cron Job — Another Lambda function which runs periodically and looks for failed events in Current Events Table and send them for retry to Custom Lambda function.

Creating Custom Lambda Function

We can use AWS SAM to spin up our Server less architecture for creating the Lambda and Dynamo Table

you can refer the AWS docs to learn how to create a hello world Lambda function and deploy to AWS Link

AWS Sam template:

Fig-5: Sam Template

Lambda Handler Sample Code:

Fig-6: Lambda Handler Sample Code

Let’s understand what are we doing in our lambda handler.

First we take the lead data from request body and parse the data, once we have the lead we check if that contains a unique_event_id to check if this a lead being retried or a new event.

Next, if the event is new we generate a new unique_event_id and attach it the lead data and save it dynamo table of Current Events.

Post we have save the data to dynamo we now send it Facebook using the facebook-nodejs-business-sdk, we will see the code implementation below for the same.

In case if the lead event is sent to Facebook successfully, we remove the lead from Current event table and push it Processed Events table and send the response back to Event Publisher.

Example Code For Facebook API Model

Fig-7: Facebook Send Event Sample Code

As for the Customer Information Parameters, this is the data Facebook uses to match the events to the ads. I’d recommend sending:

1- IP address: this shouldn’t be hashed

2- User Agent: the browser agent of the corresponding event

3- Email: this should be hashed

4- Phone Number: hashing needed

5- Click ID — fbc: this is Facebook click ID value stored in the _fbc browser cookie under your domain

6- Browser ID — fbq: this is Facebook browser ID value stored in the _fbp browser cookie under your domain

Facebook Business SDK accept a list of parameters, below is a list of all the parameters supported by the SDK, you can refer the same in Facebook conversion API docs

Fig-8: Facebook Event Parameters

Conclusion

Implementing this setup enabled much better control on the data being sent to Facebook, keeping track of events and creating a better campaign for marketing our products.

We would be delighted to share further insights into the Facebook Conversion API Implementation. Please do let us know your thoughts, feedback or questions in the comments section.

--

--