2. Set up the data pipeline
Create the warehouse tables that Levered reads during training. Exposures record what users saw, and rewards record what they did.
This step applies to data warehouse native setups. On the Managed Warehouse, send events to the ingestion API and skip this page.
With data warehouse native, the events stay in your warehouse. Levered does not store raw event data. It reads exposure and reward events with SQL at training time and trains the model on them.
This setup has the following properties:
- Data ownership. The raw event data stays in your infrastructure.
- Existing pipelines. Log events through Segment, Rudderstack, direct inserts or any pipeline that is already in use.
- Rewards without SDK calls. Levered reads conversions from the tables that the analytics pipeline already writes to.
Levered needs two tables in the warehouse:
- Exposures table. It records which variant each user saw.
- Rewards table. It records successful outcomes (conversions, purchases, etc.). This table often exists already.
Exposures table
Log an exposure event to the warehouse each time a user sees a variant. This applies to variants from the JavaScript SDK, the REST API and server-side calls.
Levered reads from the exposures table which users saw which variants.
Required columns
| Column | Type | Description |
|---|---|---|
anonymous_id | STRING | The id that the app passes to the SDK as anonymousId. The reward metric must return the same id. |
timestamp_utc | TIMESTAMP | The time at which the user was exposed to the variant. |
optimization_id | STRING | The optimization that the exposure belongs to. |
variant | JSON / STRING | The design factor values as a JSON object, e.g., {"headline": "Ship faster", "cta_text": "Start free"}. |
The id column must contain the id that the app passes to the SDK. If the app passes a signed-in user id to the SDK, the id column must contain that user id. A device id or session id from the same table does not work. In that case rewards do not match exposures and the holdout comparison is wrong, and no error is raised. levered verify <optimization-id> checks this (Test before launch).
Optional columns:
| Column | Type | Description |
|---|---|---|
is_holdout | BOOLEAN | true if the SDK assigned the user to the holdout (isHoldout). Recommended. The column simplifies your own analysis, and levered verify uses it to check the id column. |
<context factor name> | per factor | One column per context factor, named after the factor (e.g. device_type). Add these columns if the optimization has context factors. |
Create the table
CREATE TABLE IF NOT EXISTS `your_project.your_dataset.exposures` (
anonymous_id STRING NOT NULL,
timestamp_utc TIMESTAMP NOT NULL,
optimization_id STRING NOT NULL,
variant JSON
);CREATE TABLE IF NOT EXISTS exposures (
anonymous_id VARCHAR NOT NULL,
timestamp_utc TIMESTAMP_NTZ NOT NULL,
optimization_id VARCHAR NOT NULL,
variant VARIANT
);CREATE TABLE IF NOT EXISTS exposures (
anonymous_id TEXT NOT NULL,
timestamp_utc TIMESTAMPTZ NOT NULL,
optimization_id TEXT NOT NULL,
variant JSONB
);Use your own column names
The exposures table can use other column names than the ones above. Set once per warehouse connection which columns contain the SDK id, the exposure time and the variant. Optimizations created afterwards use this setting:
levered warehouse exposure-columns --anonymous-id USER_ID --timestamp TIMESTAMP --variant VARIANTRun the command without options to show the current setting. The defaults are anonymous_id, timestamp_utc and variant. The same setting is exposure_columns in the warehouse API.
The table also needs an optimization_id column. Levered reads the rows of each optimization with WHERE optimization_id = '<id>'.
Logging exposures
The app is responsible for logging the exposure to the warehouse when the user sees the variant. This applies to the JavaScript SDK, the REST API and server-side calls.
From the SDK. The onExposure callback fires when a variant is served:
onExposure={(exposure) => {
analytics.track('levered_exposure', {
anonymous_id: exposure.anonymousId,
timestamp_utc: exposure.timestamp,
optimization_id: exposure.optimizationId,
variant: exposure.variant, // e.g. {"headline": "Ship faster", "cta_text": "Start free"}
context: exposure.context, // e.g. {"device_type": "mobile"}
is_holdout: exposure.isHoldout, // true for holdout users
});
}}From the REST API. Log the exposure in the app after calling the /serve endpoint:
const res = await fetch(`https://api.levered.dev/api/v2/optimizations/${optimizationId}/serve`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ anonymous_id: userId, context: { device_type: 'mobile' } }),
});
const data = await res.json();
// Log the exposure to your warehouse
analytics.track('levered_exposure', {
anonymous_id: userId,
timestamp_utc: new Date().toISOString(),
optimization_id: optimizationId,
variant: data.variants[0].props,
is_holdout: data.is_holdout,
});Use the pipeline that is already in place, such as Segment, Rudderstack, a direct warehouse insert, or a custom ETL.
Rewards table
The rewards table records valuable user actions, such as signing up, purchasing, or clicking a CTA. This data is often already in the warehouse through the existing analytics pipeline.
Levered reads rewards with the SQL query that is defined as a metric. No SDK call is needed for rewards.
Required columns
| Column | Type | Description |
|---|---|---|
anonymous_id | STRING | The same user identifier as in the exposures. Levered uses it to join rewards to the variant that the user saw. |
timestamp | TIMESTAMP | The time at which the reward event occurred. |
value | NUMBER | The reward value. For boolean rewards (converted or not), use 1. For numeric rewards (revenue), use the amount. |
Example tables
A signup conversion table (boolean reward):
-- Your existing signups table probably already looks like this
SELECT
user_id AS anonymous_id,
created_at AS timestamp
FROM signupsA purchase table (numeric reward):
SELECT
user_id AS anonymous_id,
purchased_at AS timestamp,
amount AS value
FROM purchasesNew tables for rewards are not required. In the next step (Define Metrics), write a SQL query that reads from the existing tables.
How Levered joins exposures and rewards
During training, Levered does the following:
- It queries the exposures table for the variant that each user was shown.
- It queries the rewards table with the metric SQL for the users who converted.
- It joins both on
anonymous_idwith last-touch attribution within a conversion window. The default window is 24 hours. - It trains the model on the result.
Exposures table Rewards table
┌──────────────┬──────────┐ ┌──────────────┬───────┐
│ anonymous_id │ variant │ │ anonymous_id │ value │
├──────────────┼──────────┤ ├──────────────┼───────┤
│ user_abc │ {"h":"A"}│───>│ user_abc │ 1 │ ✓ Matched
│ user_def │ {"h":"B"}│ │ user_ghi │ 1 │ ✗ No exposure
│ user_ghi │ {"h":"A"}│ └──────────────┴───────┘
└──────────────┴──────────┘
↓
Observations (training data)
┌──────────────┬──────────┬────────┐
│ anonymous_id │ variant │ reward │
├──────────────┼──────────┼────────┤
│ user_abc │ {"h":"A"}│ 1 │
│ user_def │ {"h":"B"}│ 0 │
│ user_ghi │ {"h":"A"}│ 1 │
└──────────────┴──────────┴────────┘Next step
Next, define the metrics that tell Levered which SQL to run against the rewards data.