LeveredDocs
Manual Setup

3. Define metrics

Write the SQL queries that return the reward events of an optimization, for example conversions, revenue or engagement.

A metric is a SQL query that returns reward events. During model training, Levered runs the query against the warehouse and joins the reward events with the exposures. On the Managed Warehouse, the query runs against the events sent to the ingestion API. See Managed Warehouse.

Write the query in standard SQL. Tell Levered which result columns contain the user id, the reward time and, for numeric rewards, the value. The columns can have any names.

Column mapping

Map these columns of the query result:

ColumnRequiredDescription
User idYesThe user identifier. It must match the anonymousId that the app passes to the SDK.
TimestampYesThe time at which the reward event occurred.
ValueOnly for numeric rewardsThe numeric reward value (e.g., revenue amount).

The user id is the join key between exposures and rewards. Use the same identifier in the metric query and in the anonymousId parameter of the SDK.

Managed Warehouse

On the Managed Warehouse, rewards are stored in managed_dw.rewards. Write the metric query as follows:

  • Filter by the reward name that the app sends.
  • Keep SELECT *, so that Levered can scope the query to your organization.
  • Exclude test rewards.
levered metrics create \
  --name "Sign-ups" \
  --sql "SELECT * FROM managed_dw.rewards WHERE name = 'signup_completed' AND JSON_VALUE(properties, '\$.test') IS NULL" \
  --anonymous-id-col anonymous_id \
  --timestamp-col timestamp_utc \
  --reward-type bool

Do not use @start_date in a Managed Warehouse metric.

Reward types

Levered supports two reward types:

Reward typeMeasuresQuery result
BooleanWhether the user convertedEach returned row counts as a positive reward. A value column is not needed.
NumericThe value that the user generatedA value column with a number is required.

Example: boolean reward (signup conversion)

This query returns one row per user who converted. Levered counts each 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 the WHERE clause. Without it, the query scans the full 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

  1. Click Metrics in the Levered dashboard sidebar, then Add Metric.
  2. Enter a Name (e.g., "Signup Conversion").
  3. Pick the Source table (your rewards table by default).
  4. Under How is this reward measured?, choose Each matching row is one reward or Use a numeric column as the value.
  5. Under Column Mapping, choose the Anonymous ID column, the Timestamp column and, for numeric rewards, the Value column.
  6. Enter a Filter, or click Use custom SQL to write the query yourself.
  7. Click Create Metric.
  8. Click Preview next to the saved metric to inspect sample results.

Preview runs the saved query with a row limit (10 rows by default) and shows the rows. Use it to check the column types and the data.

Tips

  • Query cost. The metric query runs on every training cycle. Avoid expensive joins and full table scans.
  • Time filter. Include a WHERE clause with a time range (e.g., CURRENT_DATE - INTERVAL '30 days'). Without it, the query scans the full table on every training run.
  • Identifiers. The anonymous_id column must contain the same values that the app passes as anonymousId to the SDK. If the warehouse uses user_id and the frontend uses a different id, map the ids in the query.

Next step

Next, create an optimization.