Skip to content
Last updated

Facebook Conversions API Export Integration

You can use the Facebook Conversions API to send job results (in the form of event data) from Treasure Data directly to Facebook to measure how much your Facebook ads lead to real-world outcomes, such as purchases, registrations, applications, or some other web conversions.

You can upload 1st party data as conversion data in addition to the conversion data that is ingested by Facebook Pixel as 3rd party data.

You can also upload CV logs extracted from your web server logs to the Facebook conversion API.

It is recommended to upload offline events (conversion was made in person at your physical store) using this integration.

Prerequisites

  • Basic Knowledge of Treasure Data.

  • Basic knowledge of Facebook Conversions and Facebook Event

  • To upload event data, you need access to one of the following on Facebook:

    • Business Manager admin
    • Admin system user who created the event set
  • Admin on the ad_account connected to the event set

  • Facebook Pixel ID

  • Facebook Access Token

Obtain Facebook Pixel ID and Access Token

  1. Open the Business Manager dashboard and select Event Manager.

  1. Select an Event Set. Select Settings, and the Pixel ID will be displayed.

  1. Select Create Access Token.

  1. Select your Pixel.
  2. Select Next.

  1. Select Generate Access Token.

  1. Copy and save the access token. For security, we have obscured the token in our sample.
  2. Select Next.

Validate an Upload

To try an upload test, check the test code on the Test Events tab.

Use TD Console to Create Your Connection

Create a New Connection

In Treasure Data, you must create and configure the data connection to be used during export prior to running your query. As part of the data connection, you provide authentication to access the integration.

  1. Open TD Console.
  2. Navigate to Integrations Hub > Catalog.
  3. Search for and select Facebook Conversion API.

  1. Select Create Authentication.

Complete these fields:

  • Pixel ID

  • Access Token

  1. Select Continue.  Type a name for your connection and select Done.

Define your Query

You need to define the column mapping in the query. The columns in the query represent Event data to be uploaded into Facebook.

Any website events shared using Conversions API require the following data parameters:

  • client_user_agent
  • action_source
  • event_source_url

However, non-web events require only the parameter action_source.

These parameters help improve the quality of events used for ad delivery and may improve campaign performance. Additionally, the data is hashed and normalized for some columns before being sent to Facebook.

You need at least one customer information column to configure export results. Learn more about hashing and normalization requirements and Facebook Conversions API.

Integration Parameters for Facebook Conversions API

ParameterDescription
Test Event Code (optional)The code to test your server events in the Test Events feature in the Events Manager
Pre-hashed Columns(Optional) Comma-separated columns are already hashed using SHA256 and meet API acceptance requirements.
Skip Invalid Data (optional)It is used to terminate a job (without reverting) when invalid records are encountered. For example, a record is missing the required columns, e.g., event_name, event_time...

Example Query

For offline events, action_source = physical_store.

If you want to export data like currency that belongs to custom_data, you must set data in JSON format.

For example, the query should look similar to:

WITH sample_data AS(
  SELECT 
     *
  FROM
    (
    VALUES(
      1,
      'USD',
      104.2
    ),
    (
      2,
      'JPY',
      10000.2
    ),
    (
      3,
      'USD',
      103.4
    )
    ) AS t(
      id,
      currency,
      VALUE
    )
) SELECT 
  CAST(
    MAP(
      ARRAY['currency',
      'value'],
      ARRAY[currency,
      CAST(
        VALUE AS VARCHAR
      ) ]
    ) AS JSON
  ) AS custom_data,
  id
FROM
  sample_data

To query multiple values with the same name, you specify the name multiple times in the query.

Example of a Query to Populate Events Data

From Treasure Data, run the following query with export results into a connection for Facebook Conversions:

  • Regular SELECT query from a table
SELECT
  an_event_name_column AS event_name,
  an_event_time_column AS event_time,
  an_email_column AS em,
  a_country_column AS country
FROM your_table;
  • Query custom_data column
SELECT
  'PageView' as event_name,
  1598531676 as event_time,
  'elizabetho@fb.com' as em,
  '{"a":12, "b":"c"}' as custom_data

Example App Data Event query

SELECT 
  event_name,
  event_time,
  em,
  action_source,
  advertiser_tracking_enabled,
  application_tracking_enabled,
  campaign_ids,
  install_referrer,
  installer_package,
  url_schemes,
  vendor_id,
  windows_attribution_id,
  extinfo_version,
  app_package_name,
  short_version,
  long_version,
  os_version,
  device_model_name,
  locale,
  timezone_abbr,
  carrier,
  screen_width,
  screen_height,
  screen_density,
  cpu_core,
  external_storage_size,
  free_space_in_external_storage_size,
  device_time_zone,
  anon_id,
  madid,
  client_ip_address,
  client_user_agent
FROM
  conversion_table

Integration Parameters for Facebook Conversions API

Treasure Data handles data normalization and hashing based on the Facebook side data requirement in our integration before sending.

Column NameData TypeRequiredHash required?Description
Server Events
event_nameString
event_timeLong
custom_dataString/JSONAn array of custom properties. See more here
event_source_urlString
opt_outBoolean
event_idString
action_sourceStringFor app event, the action_source value must be app
data_processing_optionsStringfor LDU
data_processing_options_countryLongfor LDU
data_processing_options_stateLongfor LDU
Customer Information (at least one key provided)
emStringEmail
phStringPhone
geStringGender
dbStringDate of birth
lnStringLast name
fnStringFirst Name
ctStringCity
stStringState
zpStringZip
countryStringCountry
external_idStringExternal ID
client_ip_addressStringrequired both if usingClient IP Address
client_user_agentStringClient user agent
fbcStringFacebook Click ID
fbpStringFacebook Browser ID
subscription_idStringSubscription
lead_idLongLead ID
fb_login_idLongFB Login ID
anon_idStringUse for app event only. Your install ID. This field represents unique application installation instances.
madidStringUse for app event only. Your mobile advertiser ID, the advertising ID from an Android device or the Advertising Identifier (IDFA) from an Apple device.
App Data Event
advertiser_tracking_enabledString- 0: disabled - 1: enabled
application_tracking_enabledString- 0: disabled - 1: enabled
extinfo_versionString- a2: Android - i2: iOS
app_package_nameStringApp package name
short_versionStringShort version
long_versionStringLong version
os_versionStringOS version
device_model_nameStringDevice model name
localeStringLocale
timezone_abbrStringtimezone abbreviation
carrierStringCarrier
screen_widthStringScreen width
screen_heightStringScreen height
screen_densityStringScreen density
cpu_coreStringCPU cores
external_storage_sizeStringExternal storage size in GB
free_space_in_external_storage_sizeStringFree space on external storage in GB
device_time_zoneStringDevice timezone
campaign_idsStringAn encrypted string and non-user metadata appended to the outbound URL (for example, ad_destination_url) or deep link (for App Aggregated Event Manager) when a user clicked on a link from Facebook.
install_referrerStringThird party install referrer, currently available for Android only
installer_packageStringUsed internally by the Android SDKs
url_schemesStringUsed internally by the iOS and Android SDKs
vendor_idStringVendor ID
windows_attribution_idStringAttribution token used for Windows 10

See more on Facebook's content pages.

(Optional) Schedule Query Export Jobs

You can use Scheduled Jobs with Result Export to periodically write the output result to a target destination that you specify.

Treasure Data's scheduler feature supports periodic query execution to achieve high availability.

When two specifications provide conflicting schedule specifications, the specification requesting to execute more often is followed while the other schedule specification is ignored.

For example, if the cron schedule is '0 0 1 * 1', then the 'day of month' specification and 'day of week' are discordant because the former specification requires it to run every first day of each month at midnight (00:00), while the latter specification requires it to run every Monday at midnight (00:00). The latter specification is followed.

Scheduling your Job Using TD Console

  1. Navigate to Data Workbench > Queries

  2. Create a new query or select an existing query.

  3. Next to Schedule, select None.

  4. In the drop-down, select one of the following schedule options:

    Drop-down ValueDescription
    Custom cron...Review Custom cron... details.
    @daily (midnight)Run once a day at midnight (00:00 am) in the specified time zone.
    @hourly (:00)Run every hour at 00 minutes.
    NoneNo schedule.

Custom cron... Details

Cron ValueDescription
0 * * * *Run once an hour.
0 0 * * *Run once a day at midnight.
0 0 1 * *Run once a month at midnight on the morning of the first day of the month.
""Create a job that has no scheduled run time.
 *    *    *    *    *
 -    -    -    -    -
 |    |    |    |    |
 |    |    |    |    +----- day of week (0 - 6) (Sunday=0)
 |    |    |    +---------- month (1 - 12)
 |    |    +--------------- day of month (1 - 31)
 |    +-------------------- hour (0 - 23)
 +------------------------- min (0 - 59)

The following named entries can be used:

  • Day of Week: sun, mon, tue, wed, thu, fri, sat.
  • Month: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.

A single space is required between each field. The values for each field can be composed of:

Field ValueExampleExample Description
A single value, within the limits displayed above for each field.
A wildcard '*' to indicate no restriction based on the field.'0 0 1 * *'Configures the schedule to run at midnight (00:00) on the first day of each month.
A range '2-5', indicating the range of accepted values for the field.'0 0 1-10 * *'Configures the schedule to run at midnight (00:00) on the first 10 days of each month.
A list of comma-separated values '2,3,4,5', indicating the list of accepted values for the field.0 0 1,11,21 * *'Configures the schedule to run at midnight (00:00) every 1st, 11th, and 21st day of each month.
A periodicity indicator '*/5' to express how often based on the field's valid range of values a schedule is allowed to run.'30 */2 1 * *'Configures the schedule to run on the 1st of every month, every 2 hours starting at 00:30. '0 0 */5 * *' configures the schedule to run at midnight (00:00) every 5 days starting on the 5th of each month.
A comma-separated list of any of the above except the '*' wildcard is also supported '2,*/5,8-10'.'0 0 5,*/10,25 * *'Configures the schedule to run at midnight (00:00) every 5th, 10th, 20th, and 25th day of each month.
  1. (Optional) You can delay the start time of a query by enabling the Delay execution.

Execute the Query

Save the query with a name and run, or just run the query. Upon successful completion of the query, the query result is automatically exported to the specified destination.

Scheduled jobs that continuously fail due to configuration errors may be disabled on the system side after several notifications.

(Optional) You can delay the start time of a query by enabling the Delay execution.

Optionally Configure Export Results in Workflow

Within Treasure Workflow, you can specify the use of this integration to export data.

Learn more at Exporting Data with Parameters.

timezone: UTC

_export:
  td:
    database: sample_datasets

+td-result-into-target:
  td>: queries/sample.sql
  result_connection: facebook_conversions
  result_settings:
    test_event_code: 361738844830373
    skip_invalid: false