# 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. ![](/assets/image2021-6-10_15-33-31.3b1cc60814728dca0583915eb5f143639455b698840ae34d0320bb3ed76c452f.08eb519a.png) ## 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](https://www.facebook.com/business/help/952192354843755?id=1205376682832142) - Facebook Access Token ## Obtain Facebook Pixel ID and Access Token 1. Open the Business Manager dashboard and select Event Manager. ![](/assets/image2021-1-12_14-29-52.37077fd18222428750314607eaa28fd5af44e5c9def7c655ce7ba40361cf0a67.08eb519a.png) 1. Select an Event Set. Select Settings, and the Pixel ID will be displayed. ![](/assets/image2021-6-10_16-56-50.8c84e75d84d6a790a41ed7cc9a5a38fe1df541099fb5e6fb2fd6af71086d36b2.08eb519a.png) 1. Select Create Access Token. ![](/assets/image2021-6-10_16-58-8.43de3a484f74cdf93a8cd5820215946e61dcbc1da9cb7e2fac9c6ea8906fecdc.08eb519a.png) 1. Select your Pixel. 2. Select Next. ![](/assets/image2021-6-10_17-1-37.8e91dc607e729934d2a2c9308b2a36baec30502f0eb1db2ff91a52c8a7f04a22.08eb519a.png) 1. Select Generate Access Token. ![](/assets/image2021-6-10_17-3-37.e93e67772d3204fe1a804e4824c1e4a92d575fb4ba3e1baac32308bd08353e35.08eb519a.png) 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. ![](/assets/image2021-6-10_17-17-27.4701574ba551161d46afaf9f044da26f11714605fcd2c272932d3583dc4bc781.08eb519a.png) ## 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**. ![](/assets/image2021-6-10_17-18-39.689ea9857c4f778f9a161f66875e6887624f1794bcfc66b9d40b422fe0afbc92.08eb519a.png) 1. Select **Create Authentication**. Complete these fields: - Pixel ID - Access Token ![](/assets/image2021-1-12_16-17-28.1d0def713122ca79a6a6cc26f4bf49722a12dd56e3c57ed75c1155e05cc5b20b.08eb519a.png) 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](https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences#hash) and Facebook Conversions API. ![](/assets/image2021-6-10_17-31-40.b1ff2839dd8f7db5e06e9167e79a8f64f280c1ef8d07c36aad1ddeab99ac32da.08eb519a.png) ### Integration Parameters for Facebook Conversions API ![](/assets/image2021-6-10_17-39-26.3092acb62aca28f2072e77ea492081fffdc2960e2df0b4df40b1bb4b3cf2201c.08eb519a.png) | **Parameter** | **Description** | | --- | --- | | 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 ![](/assets/image2021-6-10_17-32-45.1dc34ffd053ae116249f4b51b7e4faac4ef5c2db7ccb73a555483c23a3ae9bad.08eb519a.png) 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: ```sql 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. ![](/assets/image2021-6-10_17-44-20.c0b9ac826e9ce804925cacc3d5406758ce088d5fe7769a62e4ad5c50a4f0c90b.08eb519a.png) ### 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 ```sql 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 ```sql 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 ```SQL 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 Name** | **Data Type** | **Required** | **Hash required?** | **Description** | | --- | --- | --- | --- | --- | | ***Server Events*** | | | | | | `event_name` | String | ✅ | | | | `event_time` | Long | ✅ | | | | `custom_data` | String/JSON | | | An array of custom properties. See more [here](https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/custom-data) | | `event_source_url` | String | | | | | `opt_out` | Boolean | | | | | `event_id` | String | | | | | `action_source` | String | | | For app event, the action_source value must be app | | `data_processing_options` | String | | | for LDU | | `data_processing_options_country` | Long | | | for LDU | | `data_processing_options_state` | Long | | | for LDU | | `Customer Information (at least one key provided)` | | | | | | `em` | String | | ✅ | Email | | `ph` | String | | ✅ | Phone | | `ge` | String | | ✅ | Gender | | `db` | String | | ✅ | Date of birth | | `ln` | String | | ✅ | Last name | | `fn` | String | | ✅ | First Name | | `ct` | String | | ✅ | City | | `st` | String | | ✅ | State | | `zp` | String | | ✅ | Zip | | `country` | String | | ✅ | Country | | `external_id` | String | | ✅ | External ID | | `client_ip_address` | String | required both if using | | Client IP Address | | `client_user_agent` | String | | Client user agent | | | `fbc` | String | | | Facebook Click ID | | `fbp` | String | | | Facebook Browser ID | | `subscription_id` | String | | | Subscription | | `lead_id` | Long | | | Lead ID | | `fb_login_id` | Long | | | FB Login ID | | anon_id | String | | | Use for app event only. Your install ID. This field represents unique application installation instances. | | madid | String | | | Use 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_enabled | String | ✅ | | - 0: disabled - 1: enabled | | application_tracking_enabled | String | ✅ | | - 0: disabled - 1: enabled | | extinfo_version | String | ✅ | | - a2: Android - i2: iOS | | app_package_name | String | | | App package name | | short_version | String | | | Short version | | long_version | String | | | Long version | | os_version | String | ✅ | | OS version | | device_model_name | String | | | Device model name | | locale | String | | | Locale | | timezone_abbr | String | | | timezone abbreviation | | carrier | String | | | Carrier | | screen_width | String | | | Screen width | | screen_height | String | | | Screen height | | screen_density | String | | | Screen density | | cpu_core | String | | | CPU cores | | external_storage_size | String | | | External storage size in GB | | free_space_in_external_storage_size | String | | | Free space on external storage in GB | | device_time_zone | String | | | Device timezone | | campaign_ids | String | | | An 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_referrer | String | | | Third party install referrer, currently available for Android only | | installer_package | String | | | Used internally by the Android SDKs | | url_schemes | String | | | Used internally by the iOS and Android SDKs | | vendor_id | String | | | Vendor ID | | windows_attribution_id | String | | | Attribution token used for Windows 10 | See more on [Facebook's content pages](https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/custom-data). ### (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. ![](/assets/image2021-1-15_17-28-51.f1b242f6ecc7666a0097fdf37edd1682786ec11ef80eff68c66f091bc405c371.0f87d8d4.png) 4. In the drop-down, select one of the following schedule options: ![](/assets/image2021-1-15_17-29-47.45289a1c99256f125f4d887e501e204ed61f02223fde0927af5f425a89ace0c0.0f87d8d4.png) | Drop-down Value | Description | | --- | --- | | Custom cron... | Review [Custom cron... details](#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. | | None | No schedule. | #### Custom cron... Details ![](/assets/image2021-1-15_17-30-23.0f94a8aa5f75ea03e3fec0c25b0640cd59ee48d1804a83701e5f2372deae466c.0f87d8d4.png) | **Cron Value** | **Description** | | --- | --- | | `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 Value | Example | Example 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](https://docs.treasuredata.com/smart/project-product-documentation/exporting-data-with-parameters). ```yaml 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 ```