# Braze Export Integration The Braze platform provides insight into new growth opportunities and brand interactions that can help you build transformative campaigns and take strategic actions. Treasure Data supports the Braze platform by allowing you to write job results from Treasure Data directly to Braze. # What can you do with this Integration? - **Map external ids:** For example, you can map ids from your CRM system to the Braze user account. - **Opt-out:** When an end-user updates the consent choosing not to participate. - **Upload your own tracking of events, purchases, or custom profile attributes**. This information can help you build precise customer segments that enhance the user experience for your campaigns. - **Update user's subscription group status**: Update user subscription state (subscribed/unsubscribed) via external_id, email or phone​ # Prerequisites - Basic Knowledge of Treasure Data. - Basic knowledge of [Braze](https://www.braze.com/). # Limitations Braze Export Integration does not support sending large amounts of data. This is due to [limitations](https://www.braze.com/docs/api/api_limits/#rate-limits-by-request-type) on Braze REST API. # Obtain Rest API Key in Braze 1. Go to the Braze dashboard and enter the development console. ![](/assets/screen-shot-2021-03-02-at-20.19.43.bd8e8ddaab558bc8d1a01321d0558a017618c426affa7aa153ede2d7c973251f.bcdb7aa7.png) 2. Go to the API setting tab and create a new Rest API key. ![](/assets/image2021-3-3_8-5-23.de3d49d870cd160a5ae262e2eb597be64932315827ae511473991a089693904f.bcdb7aa7.png) # Use the TD Console to Create Your Connection ## Create a New Connection You must create and configure the data connection in Treasure Data before running your query. As part of the data connection, you provide authentication to access the integration. 1. Open the **TD Console**. 2. Navigate to **Integrations Hub > Catalog**. 3. Select the search icon on the far right of the Catalog screen, and type **Braze**. 4. Hover over the Braze connector and select **Create Authentication**.![](/assets/braze.033d4385f429c845548e4a05b9b554b9216eb7cb9c0a27e018b4b2caad408fef.bcdb7aa7.png) 5. Enter the required credentials. ![](/assets/screen-shot-2021-03-02-at-20.27.29.ab4716d80f1cd7a008641dd40d15cb7fa4a8d53694abd3bfa76863c008fbdfac.bcdb7aa7.png) 6. Type a name for your connection. 7. Select **Done.** # Define your Query 1. Complete the instructions in [Creating a Destination Integration](https://docs.treasuredata.com/smart/project-product-documentation/creating-a-destination-integration). 2. Navigate to **Data Workbench > Queries**. 3. Select a query for which you would like to export data. 4. Run the query to validate the result set. 5. Select **Export Results**. 6. Select an existing integration authentication. ![](/assets/image2020-12-18_13-44-6.09e8af43184e33e337bef7c546600eaaa5be9f010b690af1d591c7c2b4bb2df3.c27c97ee.png) 7. Define any additional Export Results details. In your export integration content review the integration parameters. For example, your Export Results screen might be different, or you might not have additional details to fill out: ![](/assets/image2023-5-17_14-42-52.d2483b20e117c4abf1aaa32c5071595644e4c6b9d987ef4ea4ce4a2038171b85.c27c97ee.png) 8. Select **Done**. 9. Run your query. 10. Validate that your data moved to the destination you specified. ## Integration Parameters for Braze The export results parameters are described in the table following the image. ![](/assets/image2021-3-9_9-20-50.355652941f452a0c60e907505564cd40f6565c0cf89ec2b5ab45a8eeb606a0da.bcdb7aa7.png) | Title | Parameter | Values | Required | Description | | --- | --- | --- | --- | --- | | Mode | mode | user_new_alias, user_identifying, user_track, user_delete, user_subscription_group | Yes | Connector mode | | Track Record Type | track_record_type | custom_events, purchases, user_profile_attributes | No | Record type for user_track mode | | Pre-formatted Fields | pre_formatted_fields | String | No | Specify the list of columns that has an Array or JSON value to send as is to Braze. When the field is specified in this list, the value of it will be sent as is. (Otherwise, the value would be cast to a String value.) This field is important for use cases such as sending complex attributes to Braze, or sending extra operations that the Braze API can support. A example use case for this parameter would be specifying a section like "A helpful script for adding multiple Audiences to a specific user in Braze." | | Subscription Status | subscription_status | subscribed / unsubscribed | No | | | Skip on invalid records? (User Track Mode) | skip_on_invalid_records | Boolean | No | Visible for user_track mode. If enabled, continue and ignore the fail records for the JSON column. Otherwise, the job stops. | | Skip on invalid records? (User Subscription Mode) | skip_invalid_records_user_subscription | Boolean | No | Visible for user_subscription_group mode. If enabled, continue and ignore the fail records for the JSON column. Otherwise, the job stops. | | Send null values (User Track Mode and User Profile Attributes Track Record Type) | send_null_values | Boolean | No | Enable to set null values to remove user profile attributes. Some fields, include external_id, user_alias, braze_id , phone and email cannot be removed after they’re added to a user profile. | | Mode | Track Record Type | Columns | | --- | --- | --- | | user_new_alias | | Required columns: - external_id - alias_name - alias_label Other columns are ignored. | | user_identifying | | Required columns: - external_id - alias_name - alias_label Other columns are ignored | | user_track | custom_events | Required columns: - external_id or (alias_name, and alias_label) or braze_id - name, time Other aliases are sent as is | | user_track | purchases | Required columns: - external_id or (alias_name, and alias_label) or braze_id - product_id, currency, price, time Other aliases are sent as is | | user_track | user_profile_attributes | Required columns: - external_id or (alias_name, and alias_label) or braze_id Other aliases are sent as is | | user_delete | | Required columns: - external_id or (alias_name, and alias_label) or braze_id | | user_subscription_group | | Required columns: subscription_group_id and - Single column for external_id or email or phone - Two columns with - external_id & email - external_id & phone Not allow column phone & email together in schema. Phone should be String in [E.164](https://en.wikipedia.org/wiki/E.164) format. Email should have String in valid email format. SMS Subscription Group/Phone is a Braze premium feature. Contact the Braze Support team for more information. | ## Example Query ```SQL SELECT external_id, alias_name, alias_label FROM table create_user_alias ``` ## Example Query for user_subscription_group With single column for external_id or email or phone ```sql SELECT {external_id | email | phone}, subscription_group_id FROM table tbl ``` With 2 column external_id and  {email or phone} ```sql SELECT external_id , {email | phone}, subscription_group_id FROM table tbl ``` ### (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 data connector to export data. Learn more at [Using Workflows to Export Data with the TD Toolbelt](https://docs.treasuredata.com/smart/project-product-documentation/exporting-data-with-parameters). ## Example Workflow for Mode user_new_alias, user_identifying, user_delete ```yaml _export: td: database: td.database +braze_export_task: td>: export_braze.sql database: ${td.database} result_connection: your_authentication_name result_settings: mode: {user_new_alias|user_identifying|user_delete} ``` ## Example Workflow for Mode user_track ```yaml _export: td: database: td.database +braze_export_task: td>: export_braze.sql database: ${td.database} result_connection: your_authentication_name result_settings: mode: user_track track_record_type: custom_events pre_formatted_fields: array,json skip_on_invalid_records: false ``` ## Example Workflow for Mode user_subscription_group ```yaml _export: td: database: td.database +braze_export_task: td>: export_braze.sql database: ${td.database} result_connection: your_authentication_name result_settings: mode: user_subscription_group subscription_status: subscribed skip_invalid_records_user_subscription: false ``` # A Helpful Script for Adding Multiple Audiences to a Specific User in Braze Braze doesn’t support the API to directly upload users to a specific Audience / Audiences list. Braze can filter the users (to include into a specific Segment) by picking only users with attributes (including custom ones) that meet certain conditions. The conditions also support Array Attribute containing certain values. Using this capability, one can send an array of segment_ids instead of just 1 id and then can map one user with multiple audiences. What if you want to opt in / opt out after the user has already been there? Intuitively, you will want to append or remove element(s) from that array attribute. Braze API supports these operations: [API User attributes object](https://www.braze.com/docs/api/objects_filters/user_attributes_object). ![](/assets/image-20230613-080557.53878fbfa626a18afbd5e33135c83af87351710c125aae4de51b65f14ea38fc5.bcdb7aa7.png) What you need to do is to include this JSON object into the Attribute of choice. For example, the following is a curl script that do the work. ```bash curl --location --request POST 'https://rest.iad-03.braze.com/users/track?Authorization=Bearer *****************************' \ --header 'Authorization: Bearer *****************************' \ --header 'Content-Type: application/json' \ --data-raw '{ "attributes": [ { "email": "some_one@mail.com", "_td_segment_id": { "add": {"segment_id_1", "segment_id_2"}, "remove": {"segment_id_old"} } } ] }' ``` In TD query or activation, this can be sent as ```SQL SELECT 'some_one@mail.com' AS "email", '{"add":{"segment_id_1", "segment_id_2"}, "remove": {"segment_id_old"}}' AS "_td_segment_id" FROM user_table; ``` Remember to mark "_td_segment_id as a `Pre-formatted Field`