Using Conditional Logic with Personalization

Rob Howard Updated by Rob Howard

The DailyStory Personalization engine automatically replaces the personalization token with the content associated with it.

For example, for a contact whose first name is 'Anna' the personalization token {{user.firstname}} is replaced with Anna.

But what happens when the firstname is is not set, e.g. blank? Such as when you write an email and start with a salutation, such as:

Hi {{user.firstname}},

If the contact's first name is not set, this becomes:

Hi ,

Instead, this should be replaced with a conditional statement: if the contact has a first name, use it. If they don't have a first name, replace it.

Setting a default replacement value

Below you will learn more about if / else statements. While powerful, these can be complex. For most uses, replacing a value, such as first name, can use the simplified replacement default:

Hi {{@ user.firstname default="friend"}},

When using the default replacement you must use the @ symbol and you must specify a default value.

Now, when the first name is blank, this is what the Personalization Engine outputs:

Hi friend,

This is the same as the following conditional logic using if / else:

Hi {{#if user.firstname}}{{user.firstname}}{{else}}friend{{/if}},

However, as you can see, using and #if makes this simple example more complex.

If / else

The most basic conditional statement is #if. This simply checks if a profile field exists.

This can be as simple as:

{{#if account.url_facebook}}Visit our page on Facebook{{/if}}

Or, using the {{else}} statement, show an alternative if the field was empty:

Hi {{#if user.firstname}}{{user.firstname}}{{else}}friend{{/if}},
Note you can use personalization tokens within the output of conditional statements, such as the example above where the user's first name is shown if it isn't blank.

An #if condition is used to check if a value exists, but what if you want to check if the value, such as Country, matches a specific country? That is solved with #ifEqual.

If equal / else

The basic #if condition checks if a value is empty. The if equal check evaluates whether the value of the token matches a specific value. For example, checking if a contact's country is 'Canada':

{{#ifEqual 'Canada'}}Hey there Canada!{{/ifEqual}}
String evaluations are not case sensitive. In the above example, 'canada', 'Canada' or 'CANADA' would all evaluate as equal.

An else condition is also supported for {{#ifEqual}}:

{{#ifEqual 'Canada'}}Hey there Canada!{{else}}You aren't Canadian - neither am I!{{/ifEqual}}

The #ifEqual condition is designed for exact matching. It's a shortcut for a more complex evaluation such as those possible with #ifCond.

If condition / else

The basic #if checks if a value is empty. While useful, sometimes you need more complex evaluations. For example, checking if a contact's membership is about to expire:

{{#ifCond profile.membership_expiration_date '>' '2019-06-01'}}Your membership has expired.{{/ifCond}}

The #ifCond syntax requires that a evaluation symbol is provided in single quotes and that the value to evaluate is also provided in single quotes. This includes dates and numbers.

Important to evaluate a date, the date must be entered as year-month-day.

The #ifCond evaluates a personalization token against a value using:

  • == - equals
  • < - less than
  • > - greater than
  • <> or != - not equal
  • <= - less than or equal to
  • >= - greater than or equal to
Important the evaluators <= and >= are only valid when evaluating numbers and dates. And, double == is used for testing equality.

An else condition is also supported for {{#ifCond}}:

{{#ifCond profile.membership_expiration_date '>' '2019-06-01'}}Your membership has expired.{{else}}Your membership is in good standing.{{/ifCond}}

The #ifCond gives you the flexibility to evaluate if tokens are greater than, less than, and so on. However, #ifCond is limited to a true / false outcome. Use #switch to support multiple evaluations.

Multiple conditions

In the examples referenced above, all of the conditions evaluates to either true or false. This limits your personalization options because it requires to you match a condition. But what about when you can match for multiple conditions?

While slightly more complex, the #switch statement supports multiple matching options:

{{#case 'Canada'}}Hello Canada!{{/case}}
{{#case 'United States'}}Hello United States!{{/case}}
The #switch personalization condition is currently only available for string evaluations.

The syntax for #switch requires one or more #case statements. Each #case statement performs a case-insensitive string comparison to the value specified in the #switch.

The #switch does not support a default.

How did we do?

Custom Personalization Tokens

Personalization Frequently Asked Questions