Recently I got a Trello ticket to add Facebook Pixel tracking code, and they gave me a token to use. As a frontend engineer on that project, it’s confusing to me, because the token is for Facebook Conversions API. The biggest difference between Facebook Conversions API and Facebook Pixel is Facebook conversions API is invoked on the server, and Facebook Pixel is on the client. Besides, Facebook Conversions API is to solve IOS 14 updates. Now, let me write the usage of Facebook Conversions API down.
Get Facebook Conversions API token.#
You may be familiar with this page if you’re using Facebook Pixel. Let’s go to Settings
and click Get Started
in Conversions API
, and select Events
we are interested.
In the final step, clicking Open Implementation Guide
then redirects to Using the Conversions API
page to Generate Access Token
.
Make a POST request#
To send new events, make a
POST
request to this API’s/events
edge from this path:https://graph.facebook.com/{API_VERSION}/{PIXEL_ID}/events?access_token={TOKEN}
.
As we saw here, it’s just a POST request. We can try it with Graph API Explorer
.
1 | { |
You have to add client_ip_address
to JSON, or the POST request won’t be tracked. Now back to the Test Events tab to see the result.
Also, the View Details of TestEvents
in Overview shows the POST request log.
Let’s Code.#
I run serverless lambda by AWS CDK, and set the token to AWS SecretManager. The following code is my Stack sample.
1 | import * as cdk from '@aws-cdk/core'; |
And here is my lambda and class.
lambda
1 | import { |
FacebookServerSideApi
1 | const fetch = require("node-fetch"); |
I removed hashing logic used on Line 20
, and it should hash each key described here. Finally, after Deploying to AWS and sending the request by Postman or the frontend library, the Overview should show the event log.
That’s all. Now we can send such as PageView
or other events defined by Facebook Pixel and Facebook Conversions API on the same page.