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 token is not set? 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:

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

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

Hi,

The above could also be written as {{@ user.firstname default="friend"}} - this uses "friend" when the personalized value is missing.

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:

{{#if user.firstname}}Hi {{user.firstname}},{{else}}Hi,{{/if}}
Note, you can use personalization tokens within the output of conditional statements.

If equals / else

The basic if / else condition checks if a value is empty. The if equals check evaluates whether 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 Candadian!{{/ifEqual}}

If condition / else

The basic if / else condition 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}} 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.

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}}

Multiple conditions

In the examples referenced above, all of the conditions are if/else. This limits your personalization because it requires to you match a condition. But what about when you can match for muliple 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}} personaliation condition is currently only available for string evaluations.

How did we do?

Custom Personalization Tokens

Personalization Frequently Asked Questions

Contact