{"Status":true,"Message":"","Response":{"post":{"postuid":"v7ihhxexur","tenantuid":"d8b744fc-2e70-4089-bb80-dd1d08f6c7b2","projectuid":"239698c5-f7eb-4574-8cc8-c6568f08b3a0","title":"Using Conditional Logic with Personalization","slug":"article/v7ihhxexur-using-conditional-logic-with-personalization","html":"\u003Cp\u003EThe DailyStory Personalization engine automatically replaces the personalization token with the content associated with it.\u003C/p\u003E\u003Ccite class=\u0022recommended\u0022\u003E\u003Cspan class=\u0022title\u0022\u003E\u003Ci class=\u0022fa-duotone fa-circle-exclamation\u0022 aria-hidden=\u0022true\u0022\u003E\u003C/i\u003ERecommended\u003C/span\u003E\u003Cp\u003EIf you are using conditional personalization in emails, check out the \u003Ca href=\u0022https://docs.dailystory.com/article/5foqen0lx6-personalization-widget\u0022\u003EPersonalization widget\u003C/a\u003E. It makes building conditional personalization much easier!\u003C/p\u003E\u003C/cite\u003E\u003Cp\u003EFor example, for a contact whose first name is \u0026#39;Anna\u0026#39; the personalization token\u0026#160;\u003Ccode\u003E{{user.firstname}}\u003C/code\u003E\u0026#160;is replaced with\u0026#160;\u003Ccode\u003EAnna\u003C/code\u003E.\u003C/p\u003E\u003Cp\u003EBut 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:\u003C/p\u003EHi {{user.firstname}},\u003Cp\u003EIf the contact\u0026#39;s first name is not set, this becomes:\u003C/p\u003E\u003Cp\u003E\u003Ccode\u003EHi ,\u003C/code\u003E\u003C/p\u003E\u003Cp\u003EInstead, this should be replaced with a conditional statement: if the contact has a first name, use it. If they don\u0026#39;t have a first name, replace it.\u003C/p\u003E\u003Ch3\u003ESetting a default replacement value\u003C/h3\u003E\u003Cp\u003EBelow 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:\u003C/p\u003E\u003Cp\u003E\u003Ccode\u003EHi {{@ user.firstname default=\u0026#34;friend\u0026#34;}},\u003C/code\u003E\u003C/p\u003E\u003Cp\u003EWhen using the default replacement you must use the \u003Ccode\u003E@\u003C/code\u003E symbol and you must specify a default value.\u003C/p\u003E\u003Cp\u003ENow, when the first name is blank, this is what the Personalization Engine outputs:\u003C/p\u003E\u003Cp\u003E\u003Ccode\u003EHi friend,\u003C/code\u003E\u003C/p\u003E\u003Cp\u003EThis is the same as the following conditional logic using if / else:\u003C/p\u003EHi {{#if user.firstname}}{{user.firstname}}{{else}}friend{{/if}},\u003Cp\u003EHowever, as you can see, using and \u003Ccode\u003E#if\u003C/code\u003E makes this simple example more complex.\u003C/p\u003E\u003Ch3\u003EIf / else\u003C/h3\u003E\u003Cp\u003EThe most basic conditional statement is \u003Ccode\u003E#if\u003C/code\u003E. This simply checks if a profile field exists.\u003C/p\u003E\u003Cp\u003EThis can be as simple as:\u003C/p\u003E{{#if account.url_facebook}}Visit our page on Facebook{{/if}}\u003Cp\u003EOr, using the\u0026#160;\u003Ccode\u003E{{else}}\u003C/code\u003E\u0026#160;statement, show an alternative if the field was empty:\u003C/p\u003EHi {{#if user.firstname}}{{user.firstname}}{{else}}friend{{/if}},\u003Ccite class=\u0022recommended\u0022\u003E\u003Cspan class=\u0022title\u0022\u003E\u003Ci class=\u0022fa-duotone fa-circle-exclamation\u0022 aria-hidden=\u0022true\u0022\u003E\u003C/i\u003ERecommended\u003C/span\u003E\u003Cp\u003E\u003Cstrong\u003ENote\u003C/strong\u003E you can use personalization tokens within the output of conditional statements, such as the example above where the user\u0026#39;s first name is shown if it isn\u0026#39;t blank.\u003C/p\u003E\u003C/cite\u003E\u003Cp\u003EAn \u003Ccode\u003E#if\u003C/code\u003E 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 \u003Ccode\u003E#ifEqual\u003C/code\u003E.\u003C/p\u003E\u003Ch3\u003EIf equal / else\u003C/h3\u003E\u003Cp\u003EThe basic \u003Ccode\u003E#if\u003C/code\u003E 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\u0026#39;s country is \u0026#39;Canada\u0026#39;:\u003C/p\u003E{{#ifEqual profile.country \u0027Canada\u0027}}Hey there Canada!{{/ifEqual}}\u003Ccite class=\u0022recommended\u0022\u003E\u003Cspan class=\u0022title\u0022\u003E\u003Ci class=\u0022fa-duotone fa-circle-exclamation\u0022 aria-hidden=\u0022true\u0022\u003E\u003C/i\u003ERecommended\u003C/span\u003E\u003Cp\u003EString evaluations are not case sensitive. In the above example, \u0026#39;canada\u0026#39;, \u0026#39;Canada\u0026#39; or \u0026#39;CANADA\u0026#39; would all evaluate as equal.\u003C/p\u003E\u003C/cite\u003E\u003Cp\u003EAn else condition is also supported for\u0026#160;\u003Ccode\u003E{{#ifEqual}}\u003C/code\u003E:\u003C/p\u003E{{#ifEqual profile.country \u0027Canada\u0027}}Hey there Canada!{{else}}You aren\u0027t Canadian - neither am I!{{/ifEqual}}\u003Cp\u003EThe \u003Ccode\u003E#ifEqual\u003C/code\u003E condition is designed for exact matching. It\u0026#39;s a shortcut for a more complex evaluation such as those possible with \u003Ccode\u003E#ifCond\u003C/code\u003E.\u003C/p\u003E\u003Ch3\u003EIf condition / else\u003C/h3\u003E\u003Cp\u003EThe basic \u003Ccode\u003E#if\u003C/code\u003E checks if a value is empty. While useful, sometimes you need more complex evaluations. For example, checking if a contact\u0026#39;s membership is about to expire:\u003C/p\u003E{{#ifCond profile.membership_expiration_date \u0027\u003E\u0027 \u00272019-06-01\u0027}}Your membership has expired.{{/ifCond}}\u003Cp\u003EThe \u003Ccode\u003E#ifCond\u003C/code\u003E syntax requires that a evaluation symbol is provided in single quotes and that the value to evaluate is also provided in single quotes.\u003C/p\u003E\u003Cp\u003EThe\u0026#160;\u003Ccode\u003E#ifCond\u003C/code\u003E\u0026#160;evaluates:\u003C/p\u003E\u003Cul\u003E\u003Cli\u003E\u003Ccode\u003E==\u003C/code\u003E\u0026#160;- equals\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003E\u0026lt;\u003C/code\u003E\u0026#160;- less than\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003E\u0026gt;\u003C/code\u003E\u0026#160;- greater than\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003E\u0026lt;\u0026gt;\u003C/code\u003E\u0026#160;or\u0026#160;\u003Ccode\u003E!=\u003C/code\u003E\u0026#160;- not equal\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003E\u0026lt;=\u003C/code\u003E\u0026#160;- less than or equal to\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003E\u0026gt;=\u003C/code\u003E\u0026#160;- greater than or equal to\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003Econtains\u003C/code\u003E - only supported with strings and string arrays. Allows comparison of a semi-colon separated list of strings.\u003C/li\u003E\u003Cli\u003E\u003Ccode\u003Enotcontains\u003C/code\u003E - only supported with strings and string arrays. Allows comparison of a semi-colon separated list of strings.\u003C/li\u003E\u003C/ul\u003E\u003Ccite class=\u0022recommended\u0022\u003E\u003Cspan class=\u0022title\u0022\u003E\u003Ci class=\u0022fa-duotone fa-circle-exclamation\u0022 aria-hidden=\u0022true\u0022\u003E\u003C/i\u003ERecommended\u003C/span\u003E\u003Cp\u003E\u003Cstrong\u003EImportant\u003C/strong\u003E the evaluators \u003Ccode\u003E\u0026lt;=\u003C/code\u003E and \u003Ccode\u003E\u0026gt;=\u003C/code\u003E are only valid when evaluating numbers and dates. And, double \u003Ccode\u003E==\u003C/code\u003E is used for testing equality.\u003C/p\u003E\u003C/cite\u003E\u003Ch4\u003EExample using contains\u003C/h4\u003E\u003Cp\u003EThe contains operator is special and only is supported when comparing a single string value. For example:\u003C/p\u003E{{#ifCond profile.membership_type \u0027contains\u0027 \u0027Gold,Silver,Bronze\u0027}}Your membership is valid!{{/ifCond}}\u003Cp\u003EThis comparison evaluates if the \u003Ccode\u003Emembership_type\u003C/code\u003E is either Gold, Silver or Bronze.\u003C/p\u003E\u003Ch4\u003EPerforming date comparisons\u003C/h4\u003E\u003Cp\u003EIn addition to providing a specific date for evaluation, you can also use:\u003C/p\u003E\u003Cul\u003E\u003Cli\u003E\u003Cb\u003Etoday\u003C/b\u003E - the current date\u003C/li\u003E\u003Cli\u003E\u003Cb\u003Etomorrow\u003C/b\u003E - day after today\u003C/li\u003E\u003Cli\u003E\u003Cb\u003Eyesterday\u003C/b\u003E - day before today\u003C/li\u003E\u003Cli\u003E\u003Cb\u003Epast-N-days/weeks/months/years\u003C/b\u003E - for example, past-1-years would be a year ago today.\u003C/li\u003E\u003Cli\u003E\u003Cb\u003Enext-N-days/weeks/months/years\u003C/b\u003E - for example, \u003Ccode\u003Enext-3-days\u003C/code\u003E would be 3 days from today.\u003C/li\u003E\u003C/ul\u003E\u003Cp\u003EAn else condition is also supported for\u0026#160;\u003Ccode\u003E{{#ifCond}}\u003C/code\u003E:\u003C/p\u003E{{#ifCond profile.membership_expiration_date \u0027\u003E\u0027 \u00272019-06-01\u0027}}Your membership has expired.{{else}}Your membership is in good standing.{{/ifCond}}\u003Cp\u003EThe \u003Ccode\u003E#ifCond\u003C/code\u003E gives you the flexibility to evaluate if tokens are greater than, less than, and so on. However, \u003Ccode\u003E#ifCond\u003C/code\u003E is limited to a true / false outcome. Use \u003Ccode\u003E#switch\u003C/code\u003E to support multiple evaluations.\u003C/p\u003E\u003Ch3\u003EMultiple conditions\u003C/h3\u003E\u003Cp\u003EIn 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?\u003C/p\u003E\u003Cp\u003EWhile slightly more complex, the\u0026#160;\u003Ccode\u003E#switch\u003C/code\u003E\u0026#160;statement supports multiple matching options:\u003C/p\u003E{{#switch user.country}}\u003Cbr\u003E{{#case \u0027Canada\u0027}}Hello Canada!{{/case}}\u003Cbr\u003E{{#case \u0027United States\u0027}}Hello United States!{{/case}}\u003Cbr\u003E{{/switch}}\u003Ccite class=\u0022warning\u0022\u003E\u003Cspan class=\u0022title\u0022\u003E\u003Ci class=\u0022fa-duotone fa-circle-exclamation\u0022 aria-hidden=\u0022true\u0022\u003E\u003C/i\u003EWarning\u003C/span\u003E\u003Cp\u003EThe\u0026#160;\u003Ccode\u003E#switch\u003C/code\u003E\u0026#160;personalization condition is currently only available for string evaluations.\u003C/p\u003E\u003C/cite\u003E\u003Cp\u003EThe syntax for \u003Ccode\u003E#switch\u003C/code\u003E requires one or more \u003Ccode\u003E#case\u003C/code\u003E statements. Each \u003Ccode\u003E#case\u003C/code\u003E statement performs a case-insensitive string comparison to the value specified in the \u003Ccode\u003E#switch\u003C/code\u003E.\u003C/p\u003E\u003Cp\u003EThe \u003Ccode\u003E#switch\u003C/code\u003E does not support a default.\u003C/p\u003E","publish_status":0,"post_type":"Article","author":{},"featured_image_updating":false,"meta_description":"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 \u0026#39;Anna\u0026#39; the personalization token\u0026#160; {{user.firstname}} \u0026#160;is replaced with\u0026#160; Anna . But what happens when the firstname is is not set, e.g. blank? Such a ...","display_toc":true,"has_workingcopy":false,"allow_indexing":true,"sort_order":4,"total_views":1136,"date_published":"2024-10-02T16:19:34","date_updated":"2025-05-16T19:19:11.697","date_created":"2025-04-24T15:02:56.097"}}}