Defining Variables & Custom Properties

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.

Why Use let?

Defining variables with let offers several advantages:

  1. Readability: Break down complex formulas into smaller, named steps. let target_cpc = cpc(14d) * 1.1; is easier to understand than embedding the calculation directly multiple times.
  2. Reusability: Define a calculation once (like target_cpc) and reference the variable name multiple times later in your final filter expression or even in other let statements.
  3. Create Custom Properties: This is a key outcome! Every variable defined with 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.
  4. Powering Dynamic Actions: The values calculated for these variables (shown as Custom Properties) can be directly selected and used when configuring Workflow Actions (see Using Custom Properties in Workflows).

Syntax

let variable_name = expression;

Define a variable using let, followed by the variable name, =, the expression, and a required semicolon ;.

let: The keyword.

variable_name: Your chosen name.

  • Must be one word (use underscores _, e.g., high_spend_threshold).
  • Cannot conflict with built-in property or function names.

=: 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.

From Variable to Custom Property Column

When your formula runs, each let statement produces a column in the Segment results:

  • The variable name you defined (e.g., high_spend_threshold) is used internally and when selecting the variable in Actions.
  • The corresponding Custom Property column header in the Segment table automatically converts underscores to spaces (e.g., 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".

Placement

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.

PREVIOUS ARTICLE
NEXT ARTICLE