Visit our new documentation site! This documentation page is no longer updated.

Writing Job Results into Facebook Custom Audience

You can write job results directly to your Facebook Custom Audience.

Table of Contents

Prerequisites

  • 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

Go to Treasure Data Connections. Locate and select Facebook Custom Audience.



The following dialog will open.



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



Create a new OAuth connection

Log into your Facebook account:



And grant access to Treasure Data app.



You will be redirected back to Treasure Data Connections. 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 Output results at top of your query editor and select your Facebook Custom Audience connection as follows:

There are several parameters to define:

  • Ad Account ID (required): This is your Ad Account ID without act_ prefix.
  • API Version (optional, default v2.11): Facebook API Version, it’s best to keep default value
  • 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 Custom Audience Name will be used. We recommend that you give your Custom Audience a unique name.
  • Custom Audience Description (optional): Description of Custom Audience.
  • No need to normalize and hash records (default false): Indicates whether the data has already been normalized and hashed. If not, TD automatically normalizes and hashes the records.
  • Initial intervals in milliseconds between retries (optional, default 60000): Interval to retry if a recoverable error occurs (in milisecond).
  • Retry limit (optional, default 5): Number of retries before attempts end.

Here is a sample configuration:

Step 3: Write the Query to Populate a user list

Here is an example Audiences list before output of a query result:

From 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 for demo purpose only. Also, the query requires no source table (for the ease of testing out this feature), but you still must choose a 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 are found from query result, you receive an error.

You can use an 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, for example, you can use either of email or EMAIL.

Appendix B: Data Normalization

Treasure Data result output normalizes your values automatically to follow Facebook’s normalizing rules, see here.

All values, uploaded to Facebook for matching, must 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 the data manually, apply your own normalization before attempting to output results.

The following conversion behaviour is applied according to data type during 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 (because 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, use 2-character ANSI abbreviation code, the Treasure Data platform does not cut off input string (into 2 characters), because of the need 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, use exactly 5 digits, our the Treasure Data platform does not cut off input string (into 5 characters), because of the need to support UK zip code format.

Last modified: May 24 2018 18:30:50 UTC

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