Features
Merch Jar Formulas allow you to define intermediate values and calculations using variables via the let keyword. This not only improves the readability and reusability of your logic within the Formula Composer but also automatically creates corresponding Custom Property columns in your resulting Segment table.
let
?Defining variables with let offers several advantages:
let $target_cpc = cpc(14d) * 1.1;
is easier to understand than embedding the calculation directly multiple times.target_cpc
) and reference the variable name multiple times later in your final filter expression or even in other let
statements.let
automatically generates a new column in your Segment results table, displaying the calculated value for each item that matches your Formula expression. This allows you to see custom metrics or calculated thresholds directly alongside standard data.let $variable_name = expression;
Define a variable using let
, followed by the variable name starting with $
, =
, the expression, and a required semicolon ;
.
let
: The keyword.
$variable_name
: Your chosen name.
$
sigil._
, e.g., $
high_spend_threshold
).=
: Assignment operator.
expression
: The calculation or value (Number, String, Time Period, case
result, reference to another variable, Timestamp result from now()
,etc.).
;
: Mandatory end for each let
statement.
When your formula runs, each let statement produces a column in the Segment results:
$
high_spend_threshold
) is used internally and when selecting the variable in Actions.$
high_spend_threshold
becomes "High Spend Threshold").Examples
Here's how let
works in practice to create variables and Custom Properties:
// --- Example 1: Identify Budget Pacing ---
// Calculate yesterday's spend as a percentage of the daily budget
let $budget_pacing_pct = case(
budget > 0 => spend(1d) / budget, // Calculate pacing if budget > 0
else 0 // Assign 0 if budget is 0
);
// Filter for campaigns spending over 90% of their budget yesterday
$budget_pacing_pct > 90% and state = "enabled"
Resulting Segment: Includes enabled campaigns that spent over 90% of their budget yesterday. The Segment table will have a Custom Property column named "Budget Pacing Pct" showing the calculated percentage (e.g., 0.95 for 95%).
// --- Example 2: Define Thresholds & Performance Tag ---
let $low_acos_target = 25%;
let $high_acos_target = 50%;
let $performance_tag = case(
acos(30d) < $low_acos_target => "Excellent",
acos(30d) < $high_acos_target => "Good",
else "Review" );
// Filter for items needing review
$performance_tag = "Review" and state = "enabled"
Resulting Segment: Includes items needing review AND columns named "Low Acos Target", "High Acos Target", and "Performance Tag".
All let
definitions must appear before the final Formula Expression (the main filtering condition).
Using let
is fundamental for creating sophisticated logic, custom data views, and dynamic automations in Merch Jar.
© Merch Jar LLC