|
| 1 | +--- |
| 2 | +title: Subscriptions with SQL Traits |
| 3 | +plan: engage-premier |
| 4 | +beta: true |
| 5 | +--- |
| 6 | + |
| 7 | +Use Subscriptions with SQL Traits to connect to your data warehouse and query user subscription data to Engage on a scheduled basis. Use your data warehouse as a single source of truth for subscription statuses and query from warehouses such as BigQuery, Redshift, or Snowflake. |
| 8 | + |
| 9 | +On this page, you'll learn how to use SQL to sync subscription data from your warehouse to Engage. |
| 10 | + |
| 11 | +## Getting started |
| 12 | + |
| 13 | +To use Subscriptions with SQL Traits, you need the following: |
| 14 | +- A warehouse connected to Segment |
| 15 | +- A Segment workspace |
| 16 | +- A user account with access to Engage in that workspace |
| 17 | + |
| 18 | +> info "" |
| 19 | +> Segment supports Redshift, Postgres, Snowflake, Azure SQL, and BigQuery as data warehouse sources for SQL Traits. Visit [Segment's warehouse docs](/docs/connections/storage/warehouses/) for more on getting started with data warehouses. |
| 20 | +
|
| 21 | +> success "" |
| 22 | +> Before you begin with Subscriptions with SQL Traits, you may also want to visit the [subscription docs](/docs/engage/user-subscriptions/) to learn more about user subscriptions in Engage. |
| 23 | +
|
| 24 | +## Sync subscription data with SQL |
| 25 | + |
| 26 | +You can sync with SQL from two locations in the Segment app. Navigate to **Profiles > Profile explorer** or **Engage > Audiences > Profile explorer**, then: |
| 27 | + |
| 28 | +1. Click **Manage subscription statuses**, and select **Update subscription statuses**. |
| 29 | +2. Select **Sync with SQL**, and click **Configure**. |
| 30 | + |
| 31 | +### Configure your SQL query |
| 32 | + |
| 33 | +To configure Subscriptions with SQL Traits, you can write your own query or click **Use Template** to use one of the templates Engage provides. For any new users that your query returns, Engage adds a new profile. |
| 34 | + |
| 35 | +> success "" |
| 36 | +> Click **Reset Template** to reset your SQL query. |
| 37 | +
|
| 38 | +Queries must return at least one pair of the columns below with a value of `subscribed`, `unsubscribed`, or `did_not_subscribe`: |
| 39 | +- `email` and `_segment_internal_email_subscription_` |
| 40 | +- `phone` and `_segment_internal_sms_subscription_` |
| 41 | + |
| 42 | +For more subscription SQL best practices, view the [query requirements](#query-requirements) below. |
| 43 | + |
| 44 | +### Select a warehouse and preview your query |
| 45 | + |
| 46 | +Once you write your SQL query, click **Select warehouse** from the Configure screen to select the data warehouse you'd like to query. |
| 47 | + |
| 48 | +Before you schedule your sync intervals, click **Preview** to preview a subset of data and validate your results. To see subscription statuses for a particular profile, select a user row, then select the Identities tab. |
| 49 | + |
| 50 | +### Schedule sync intervals |
| 51 | + |
| 52 | +You can schedule sync intervals to import subscription data from your warehouse to Engage: |
| 53 | + |
| 54 | +1. From the Configure screen, click **Schedule**. |
| 55 | +2. Add a SQL job name and description. |
| 56 | +3. Set the sync schedule. |
| 57 | + - Choose a time to start the SQL job and how often to run syncs. |
| 58 | +4. Click **Create** to create and save the SQL job. |
| 59 | + |
| 60 | +> info "" |
| 61 | +> SQL queries are executed directly to your data warehouse, which could generate additional costs for pay-per-query warehouses. |
| 62 | +
|
| 63 | +### View SQL job history |
| 64 | + |
| 65 | +Use the Update History page to view all SQL jobs. |
| 66 | + |
| 67 | +1. Navigate to **Profiles > Profile explorer** or **Engage > Audiences > Profile explorer.** |
| 68 | +2. Click **Manage subscription statuses**. |
| 69 | +3. Select **View update history**, then select the **SQL Jobs** tab. |
| 70 | + |
| 71 | +From the Update History page, you can view details for each SQL job including the creation date and time, compute status, and the number of users updated across all runs for a job. Click the job link to visit a particular SQL job Overview page. |
| 72 | + |
| 73 | +## Query requirements |
| 74 | + |
| 75 | +When you build your SQL query, keep the following requirements in mind for the data your query returns. |
| 76 | + |
| 77 | +**Your query must:** |
| 78 | + |
| 79 | +- Return at least one column with `user_id`, `anonymous_id`, `email`, `phone` (or `group_id` for account traits if Profiles for B2B is enabled). |
| 80 | +- Return records less than 16KB in size. |
| 81 | + |
| 82 | +**Your query must not:** |
| 83 | + |
| 84 | +- Include values for both `user_id` and `anonymous_id` for a given record. |
| 85 | +- Return any `user_id`s, `anonymous_id`s, or `group_id`s with a `null` value. |
| 86 | +- Return any records with duplicate `user_id`s. |
| 87 | +- Return duplicate `email` or `phone` records that have different subscription statuses. |
| 88 | +- Return more than 25 million rows. |
0 commit comments