Using n8n to Send Events to the Meta/Facebook Conversions API

As marketers want to ensure the most accurate tracking and to maximize the amount of signals they are collecting (to gain better visibility of campaign performance, increase ROI etc.), sending Conversion API events from different data sources is becoming a common use case. In this article, it will demonstrate how you can send events to the Meta/Facebook Conversions API with n8n.

The use case we will cover is that we have a CSV file on an FTP server with all our conversion events and we want to pass this in to the Conversions API with n8n.

The following example will:

  1. Download a CSV file off an FTP
  2. Convert the input file to JSON
  3. Convert that JSON to a valid Conversion API payload
  4. Send payload to the Conversions API via an HTTP request*

The example outlined is extremely contrived and incomplete when it comes to a full-blown Conversion API integration, but the example is to simply show how we can bring the whole flow together within n8n with the native components available to us.

These concepts would generally apply to any other Conversion API (e.g. TikTok) so it may be useful for other integrations especially if there are no pre-defined nodes available in n8n and you want to get up and running immediately.


n8n Workflow

Below is what my complete n8n workflow looks like:

 

1. Download a CSV file off an FTP

This is a standard FTP node where we have a CSV file on a FTP that contains:

  • event_name: the event name we want to send into the Conversions API
  • event_source_url: the URL of the conversion
  • value: value of Purchase
  • currency: currency of Purchase
  • ip: user’s IP
  • user_agent: user’s User Agent

File used in this example

The setup of the FTP node is fairly standard. It assumes also that the file on the FTP has a static file name and will be uploaded to the FTP with the latest conversion events.

2. Convert the input file to JSON

We use n8n to grab take the file that we just grabbed and use a spreadsheet node to “Read from file” and convert the items into JSON. Make sure to specify that the “Header Row” option is turned on.

3. Convert that JSON to a valid Conversion API payload

This is the part that is especially contrived when compared to a real-world scenario. However, the general idea is to grab (and transform, if needed) the values from the CSV into a valid Conversion API payload. I won’t dwell to much on the format and details as this is deeply documented at Meta’s Conversion API documentation.

The following function will take the JSON data (originating from the file off the FTP) then place it in a structured object that we add to a property within each item called payload which is the data that is correctly formatted for the Conversion API call.


for (item of items) {
  item.json.payload = [{
         "event_name": item.json.event_name,
         "event_time": Math.round((new Date()).getTime() / 1000),
         "user_data": {
           "client_ip_address": item.json.ip,
           "client_user_agent": item.json.user_agent
         },
         "custom_data": {
           "currency": item.json.currency,
           "value": item.json.value
         },
         "event_source_url": item.json.event_source_url,
         "action_source": "website"
       }];
}

return items;


4. Send payload to the Conversions API via an HTTP request*

Using the HTTP Request node we can then set up the proper parameters to forward the payload property to the Conversions API.

Adding the following is necessary:

      • Request Method to: POST
      • URL to: https://graph.facebook.com/GRAPH_API_VERSION/YOUR_PIXEL_ID/events
        • GRAPH_API_VERSION: needs to be replaced with the version of the Graph API you want to use. The latest version when this article was written was: v13.0
        • YOUR_PIXEL_ID: needs to be replaced with your Facebook Pixel ID
      • Add query parameters:
      • Add body parameters:
        • data: this is where add our newly formatted payload. It takes in an array of events. For the sake of our example all we need to do is reference the payload value by creating an expression to reference the value we previously created in our function.
        • test_event_code (optional): optional parameter that allows us to see the events we are sending to the Conversion API in the Event Testing Tool within Events Manager.

Now you’re all done and can execute your workflow!

End Result – Conversion API Events Received

When you execute your workflow you can see that events are received as expected via the output in n8n:

In addition, you can see your events Facebook’s Event Testing Tool (if this was set up with the test_event_code in the previous step).


*As the Graph API (the Conversions API is a subset of the Graph API) is a REST-based API, we don’t need a special n8n node to send data to the Conversion API endpoint. The inputs are however a bit more detailed and less user-friendly compared to if a specialized n8n node existed to guide a user through the setup process. In the future, I would like to create a n8n node that streamlines this whole process.