# Klaviyo Export Integration Klaviyo is a digital marketing platform focused on email marketing automation for e-commerce businesses. Klaviyo offers an SMS and email marketing automation platform for e-commerce. Using this integration and TD - You can add profiles to a Klaviyo member list. - You can remove profiles from Klaviyo member list ## Prerequisites - Basic knowledge of Treasure Data, including the [TD Toolbelt](https://api-docs.treasuredata.com/en/tools/cli/quickstart/). - A Klaviyo account with a Private API access token. ## API Information Treasure Data uses the [Campaigns API](https://developers.klaviyo.com/en/reference/campaigns_api_overview) to direct Klaviyo to send email messages. The documentation on the Request Body of this API can be very helpful in setting up your email templates on Klaviyo. ## Requirements and Limitations - To perform a "Remove Profiles" operation," your query must specify a profile_id column (case sensitive) whose data type is a string. If your query does not specify this column, then an error is returned, and the job fails. - To perform an "Add Profiles" operation, your query must specify a column named email (case sensitive) whose data type is string. If your query does not specify this column, then an error is returned, and the job fails. - If your query specifies a column named phone_number (case sensitive), then data from this field must follow the [Accepted phone number formats for SMS in Klaviyo](https://help.klaviyo.com/hc/en-us/articles/360046055671-Accepted-Phone-Number-Formats-for-SMS-in-Klaviyo). Otherwise, an error is returned, and the job fails. ## Obtain the Private API access token from Klaviyo Console 1. Log in to the [Klaviyo Dashboard](https://www.klaviyo.com/login). 2. On the Settings tab select **Create Private API Key**. 3. Copy the Private API key and store it in a secure location. ![](/assets/image2021-9-2_19-8-32.3c940e83c69573c651a7da50d75f3d29c1cea78773d0919014671f5cce866889.9d563c94.png) ## Obtain the List Name or List ID from the Klaviyo Console 1. Log in to the [Klaviyo Dashboard](https://www.klaviyo.com/login). 2. On the left-hand side of the Dashboard, select **Lists & Segments**. 3. From the Lists & Segments page, select the link for your list name. 4. Copy the list_id from the URL of the list display page and store it in a secure location. ## Use the TD Console to Create Your Connection ### Create a New Connection In Treasure Data, you must create and configure the data connection 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 Klaviyo and select **Klaviyo Output Beta**. 4. Select **Create Authentication**. ![](/assets/klaviyo_out.0f1f332e2fc85584bcb180c9fa57797ba9cf1d0a8c104ef10e5135ca3b4a15f9.9d563c94.png) 5. Enter your Klaviyo Private API key. 6. Select **Continue**. ![](/assets/image2021-9-2_19-14-14.07bfc65e3d181f1ab49f3dbe1216044dced2a2b71f29e03d716a3f93f0f71541.9d563c94.png) 7. Enter a name for your connection. 8. Select **Done**. ### Define your Query To perform an "Add Profiles" operation, your query must specify a column named email (case sensitive) whose data type is string. If your query does not specify this column, then an error is returned, and the job fails. To perform a "Remove Profiles" operation," your query must specify a profile_id column (case sensitive) whose data type is a string. If your query does not specify this column, then an error is returned, and the job fails. 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 **Use Existing Integration**. ![](/assets/image2021-9-2_19-23-5.c6b3eb8e8b76d7851851789359e57bca513b4d9131d3ba63d22498b710a4242e.9d563c94.png) 7. Define any additional Export Results details. In your export integration content review the integration parameters. ![](/assets/screen-shot-2024-04-10-at-11.12.26.b5eae7fa5e71597f36ea8a08fe276329846e8678c14ec021c4260e197779ad5e.9d563c94.png) 8. Select **Done**. 9. Run your query. 10. Validate that your data moved to the destination you specified. **Integration Parameters for Klaviyo** | Parameter | Values | Description | | --- | --- | --- | | Data operation | - Add profiles - Remove profiles | Add or remove profiles from a Klaviyo list. Klaviyo's profile_id is required for the remove profiles operation. After removal, the profile will no longer receive marketing emails from this particular list. | | Add to | - List ID - List Name | Used to identify the Klaviyo list. | | List ID or List Name value | - List ID - List Name | The List ID or List Name that profiles will be added to.  If the "Add to" parameter has been set to List Name, and the name specified here does not exist in Klaviyo then a new list will be created with these values. | | Wait Until The Operation Is Finished? | True/False | If checked, the TD Console will wait until the operation is finished on Klaviyo side before continuing. If unchecked, the TD Console submits the task to Klaviyo and moves on. | | The waiting time for operation completion | Minimum: 300000 ms (5 minutes) Maximum: 86400000 ms (1 day) | The time in milliseconds that TD Console will wait for the operation to complete. | ## Example Query This query must include an email column. ``` SELECT email, col_a, col_b, col_c FROM your_table; ``` This query requires a profile_id column. ``` SELECT profile_id FROM your_table; ``` ### (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 about [using workflows to export data with the TD Toolbelt](https://api-docs.treasuredata.com/en/tools/cli/api/#workflow-commands). ### Example Workflow for Adding Profiles by List ID ``` _export: td: database: klaviyo_db +klaviyo_export_task: td>: export_data.sql database: ${td.database} result_connection: new_created_klaviyo_auth result_settings: type: klaviyo api_token: api_token operation: add list_identify_type: id list_value: id_value wait_for_completion: true waiting_time_for_completion_millis:3600000 ``` ### Example Workflow for Adding profiles by List Name ```yaml _export: td: database: klaviyo_db +klaviyo_export_task: td>: export_data.sql database: ${td.database} result_connection: new_created_klaviyo_auth result_settings: type: klaviyo api_token: api_token operation: add list_identify_type: name list_value: name_value wait_for_completion: true waiting_time_for_completion_millis:3600000 ``` ### Example Workflow for Removing Profiles by List ID ```yaml _export: td: database: klaviyo_db +klaviyo_export_task: td>: export_data.sql database: ${td.database} result_connection: new_created_klaviyo_auth result_settings: type: klaviyo api_token: api_token operation: remove_profiles list_identify_type: id list_value: id_value ``` ### Example Workflow for Removing Profiles by List Name ```yaml _export: td: database: klaviyo_db +klaviyo_export_task: td>: export_data.sql database: ${td.database} result_connection: new_created_klaviyo_auth result_settings: type: klaviyo api_token: api_token operation: remove_profiles list_identify_type: name list_value: name_value ```