Define Metrics
Write SQL queries that tell Levered what a successful outcome looks like -- conversions, revenue, engagement, or any custom reward.
A metric is a SQL query that defines what counts as a reward. Levered runs this query against your warehouse during model training to join reward events with exposures and learn which variants perform best.
You write standard SQL. Levered requires a few specific column aliases in the result set so it can map rows to users and time periods.
Column mapping
Your metric query must return these columns:
| Column | Required | Description |
|---|---|---|
anonymous_id | Yes | The user identifier. Must match the anonymousId you pass in the SDK. |
timestamp | Yes | When the reward event occurred. |
value | Only for numeric rewards | The numeric reward value (e.g., revenue amount). |
The anonymous_id is the join key between exposures and rewards. Use the same identifier in your metric query and in the SDK's anonymousId parameter.
Reward types
Levered supports two reward types:
- Boolean -- Did the user convert? Any row returned counts as a positive reward. No
valuecolumn needed. - Numeric -- How much value did the user generate? Requires a
valuecolumn with a number.
Example: boolean reward (signup conversion)
This query returns one row per user who converted. Levered treats the presence of a row as a positive reward.
SELECT
user_id AS anonymous_id,
converted_at AS timestamp
FROM conversions
WHERE converted_at >= CURRENT_DATE - INTERVAL '30 days'Include a time filter in your WHERE clause to avoid scanning your entire history on every training run.
Example: numeric reward (revenue)
This query returns purchase events with a dollar amount. Levered uses the value column to weight rewards.
SELECT
user_id AS anonymous_id,
purchase_date AS timestamp,
amount AS value
FROM purchases
WHERE purchase_date >= CURRENT_DATE - INTERVAL '30 days'Create a metric
- Navigate to Metrics in the Levered dashboard.
- Click Create Metric.
- Enter a name (e.g., "Signup Conversion").
- Select the reward type: Boolean or Numeric.
- Write your SQL query in the editor.
- Map the columns: select which output column maps to
anonymous_id,timestamp, and optionallyvalue. - Click Preview to run the query and inspect sample results.
- Click Save.
The preview step runs your query with a short lookback window and shows you the first rows. Use it to confirm the column types and data look correct before saving.
Tips
- Keep queries simple. The metric query runs on every training cycle. Avoid expensive joins or full table scans.
- Filter by time. Include a
WHEREclause with a time range (e.g.,CURRENT_DATE - INTERVAL '30 days') to avoid full table scans on every training run. - Match your identifiers. The
anonymous_idcolumn must contain the same values you pass asanonymousIdin the SDK. If your warehouse usesuser_idbut your frontend uses a different ID, add a mapping in your query.
Next step
With a metric defined, you are ready to create your first optimization.