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.

If you are using conditional personalization in emails, check out the Personalization widget. It makes building conditional personalization much easier!

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 profile.country '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 profile.country '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.

The #ifCond evaluates:

  • == - equals
  • < - less than
  • > - greater than
  • <> or != - not equal
  • <= - less than or equal to
  • >= - greater than or equal to
  • contains - only supported with strings and string arrays. Allows comparison of a semi-colon separated list of strings.
  • notcontains - only supported with strings and string arrays. Allows comparison of a semi-colon separated list of strings.
Important the evaluators <= and >= are only valid when evaluating numbers and dates. And, double == is used for testing equality.

Example using contains

The contains operator is special and only is supported when comparing a single string value. For example:

{{#ifCond profile.membership_type 'contains' 'Gold,Silver,Bronze'}}Your membership is valid!{{/ifCond}}

This comparison evaluates if the membership_type is either Gold, Silver or Bronze.

Performing date comparisons

In addition to providing a specific date for evaluation, you can also use:

  • today - the current date
  • tomorrow - day after today
  • yesterday - day before today
  • past-N-days/weeks/months/years - for example, past-1-years would be a year ago today.
  • next-N-days/weeks/months/years - for example, next-3-days would be 3 days from today.

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:

{{#switch user.country}}
{{#case 'Canada'}}Hello Canada!{{/case}}
{{#case 'United States'}}Hello United States!{{/case}}
{{/switch}}
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

Contact