Writing Job Results into Facebook Custom Audience (BETA)

This article explains how to write job results directly to your Facebook Custom Audience. This feature is currently in beta, any feedback would be appreciated.

Table of Contents


  • Basic knowledge of Treasure Data, including the toolbelt.
  • A Facebook Ad Account.
  • Authorized Treasure Data Facebook app access to own Facebook Ad Account

Step 1: Create a new connection

Please visit Treasure Data Connections, search and select Facebook Custom Audience.

The dialog below will open.

Please select an existing OAuth connection for Facebook, or click the link under OAuth connection to create a new one.

Create a new OAuth connection

Please login to your Facebook account in popup window:

And grant access to Treasure Data app.

You will be redirected back to Treasure Data Connections. Please repeat the first step (Create a new connection) and choose your new OAuth connection.

Step 2: Configure to output results to Facebook Custom Audience connection

Check to Output results at top of your query editor and select your Facebook Custom Audience connection as follows:

There are several parameters to fill out:

  • Ad Account ID (required): This is your Ad Account ID without act_ prefix.
  • API Version (optional, default v2.8): Facebook API Version, it’s best to keep default value v2.8 (v2.7 will be deprecated soon).
  • Custom Audience Name (required): Name of Custom Audience to create/update, if none exists, one will be created.
    • Important note: If you have many Custom Audiences with same name as this input, the latest one will be used. We recomment to give your Custom Audience a unique name.
  • Custom Audience Description (optional): Description of Custom Audience.
  • Initial intervals in milliseconds between retries (optional, default 60000): Interval to retry if a recoverable error happens (in milisecond).
  • Retry limit (optional, default 5): Number of retries before it gives up.

Here is a sample configuration:

Step 3: Write the Query to Populate a user list

Here is an example Audiences list before outputting a query result:

Then, back on Treasure Data, run the following query with Output results into a connection of Facebook Custom Audience (see Appendix A for more details of column naming):

SELECT email, fn, ln FROM (
  VALUES ('demo1@example.com', 'John', 'Doe'),
         ('demo2@example.com', 'Isaac', 'Miceli'),
         ('demo3@example.com', 'Christopher', 'Agar')
)  tbl (email, fn, ln)

The above query will not match any real users, it’s solely for demo purpose only. Also, it requires no source table (for the ease of testing out this feature), but you still need to choose your database, so pick “sample_datasets” or any other arbitrary table.

The query should complete in a few seconds. After that, check your Audience List:

Appendix A: Column naming

Output result needs to follow Facebook User schema here. Supported column names are:

  • EXTERN_ID: External ID
  • EMAIL: Email addresses
  • PHONE: Phone numbers
  • GEN: Gender
  • DOBY: Birth Year
  • DOBM: Birth Month
  • DOBD: Birthday
  • LN: Last Name
  • FN: First Name
  • FI: First Initial
  • CT: City
  • ST: States
  • ZIP: Zip code
  • MADID: Mobile advertiser id
  • COUNTRY: Country code

If no supported column was found from query result, an error will be thrown.

You can use alias in your query to rename columns of your query result, for example:

SELECT an_email_column AS EMAIL,
another_phone_column AS PHONE
FROM your_table;
  • Note: column name is case-insensitive, ie. you can use either of email or EMAIL.

Appendix B: Data Normalization

Keep it in mind that our result output normalizes your values automatically to follow Facebook’s normalizing rules, see here.

All values, uploaded to Facebook for matching, need to be normalized with the normalizing rules of Facebook. The values will just miss chances to match if they are not normalized. If you need to normalize by yourselves, apply your own normalization beforehand.

The conversion below is actually applied per type in our result output for normalization.

  • EXTERN_ID (External ID): no action needed.
  • EMAIL (Email addresses): trimming leading and trailing whitespace and convert all characters to lowercase.
  • PHONE (Phone numbers): removing any non-digit characters and leading zeros.
  • GEN (Gender): trimming leading and trailing whitespace and convert all characters to lowercase. If the result does not match with “m” or “f”, an empty string will be used (since it’s an invalid value, there is no use to match it)
  • DOBY (Birth Year): removing any non-digit characters and get first 4 digits.
  • DOBM (Birth Month) and DOBD (Birthday): removing any non-digit characters and get first 2 digits.
  • FN (First Name) and LN (Last Name): trimming leading and trailing whitespace and convert all characters to lowercase. Remove all punctuations. Supports special characters in UTF-8 encoding.
  • FI (First Initial): apply the same as First Name and get first character.
  • CT (City): convert all characters to lowercase and remove any non alphabetic (a-z) characters.
  • ST (States): convert all characters to lowercase and remove any non alphanumeric (a-z and 0-9) characters.
    • Note: If your value is US State, please use 2-character ANSI abbreviation code, our platform will not cut off input string (into 2 characters), as it needs to support states outside US.
  • ZIP (Zip code): trimming leading and trailing whitespace and convert all characters to lowercase, remove any non alphanumeric or whitespace from result.
    • Note: If your value is US zip code, please use exact 5 digits, our platform will not cut off input string (into 5 characters), as it needs to support UK zip code format.

Last modified: Apr 04 2017 17:53:25 UTC

If this article is incorrect or outdated, or omits critical information, please let us know. For all other issues, please see our support channels.