Variables in endpoints
Contents
Variables let you customize endpoint results at execution time without creating multiple endpoints.
Variables in SQL-based endpoints
For SQL-based endpoints, use variables to inject values into your query at runtime.
If your query defines a variable, you must pass a value for it when executing the endpoint. Requests without required variables will fail. This is a safety feature to prevent accidentally returning unfiltered data to your customers.
Define variables in your query using the {variables.variable_name} syntax:
Then pass values when executing:
Materialization
SQL-based endpoints with variables can be materialized when each variable is used in a WHERE clause with a supported comparison operator.
Supported operators include: =, >=, >, <, <=
You can use multiple variables in the same query, including range queries on the same column.
For example, the following queries can be materialized:
However, queries with unsupported operators or variable usage outside WHERE clauses cannot be materialized:
Variables in insight-based endpoints
For insight-based endpoints, variables work differently. Instead of defining them in your query, certain magic variables are automatically available based on your insight configuration.
The breakdown property variable
If your insight has a single breakdown configured, the breakdown property name automatically becomes a variable. For example, if your TrendsQuery breaks down by $browser, you can filter results by passing that property as a variable:
Note: The breakdown variable is only available for insights with a single breakdown. Insights with multiple breakdowns can still be used as endpoints, but you will not be able to filter by the variables.
This filters the results to only return data where $browser equals "Chrome".
Breakdown variables work with:
- TrendsQuery
- FunnelsQuery
- RetentionQuery
Date variables
For non-materialized insight endpoints, you can also use date_from and date_to variables to filter by date:
You can combine date variables with the breakdown variable:
Materialization
Materialized insight-based endpoints only support the breakdown property variable. Date variables (date_from, date_to) are not available for materialized endpoints because the data is pre-computed.
If your insight has a breakdown, you can still filter by that property: