Conditional Messaging Logic (Tags)
Tags allow you to include programming logic in your messaging campaigns.
A tag must be wrapped in {% %}
.
Tags can be used for executing conditional statements as well as for advanced use cases, like assigning variables or iterating through a block of code.
To make your life a bit easier, Braze has included color-formatting that will activate in green and purple if you correctly format your Liquid syntax.
If you’re having a hard time using conditional messaging, try writing out the conditional syntax before you insert your custom attributes and other Liquid elements.
For example, add the following into the message field first:
1
2
3
{% if X >0 %}
{% else %}
{% endif %}
Be sure it highlights in green, then replace the X
with your chosen Liquid or Connected Content using the blue +
in the message field corner, and the 0
with your desired value.
Then, add your message variations as you need them between the else
conditionals:
1
2
3
4
5
{% if {{custom_attribute.${total_spend}}} >0 %}
Thanks for purchasing! Here's another 10% off!
{% else %}
Buy now! Would 5% off convince you?
{% endif %}
Conditional Logic
You can include many types of intelligent logic within messages – one example is a conditional statement. See the following example which uses conditionals to internationalize a campaign:
1
2
3
4
5
6
7
8
9
{% if ${language} == 'en' %}
This is a message in English from Braze!
{% elsif ${language} == 'es' %}
Este es un mensaje en español de Braze !
{% elsif ${language} == 'zh' %}
这是一条来自Braze的中文消息。
{% else %}
This is a message from Braze! This is going to go to anyone who did not match the other specified languages!
{% endif %}
Conditional Logic: Step By Step
In this example, we use tags with “if”, “elsif” and “else” statements to deliver internationalized content.
1
2
{% if ${language} == 'en' %}
This is a message in English from Braze!
If the customer’s language is English, the first condition is met and the customer will receive a message in English.
1
2
3
4
{% elsif ${language} == 'es' %}
Este es un mensaje en español de Braze !
{% elsif ${language} == 'zh' %}
这是一条来自Braze的中文消息。
You can specify as many conditional statements as you’d like- subsequent conditions will be checked if the previous conditions are not met. In this example, if a customer’s device is not set to English this code will check to see if the customer’s device is set to Spanish or Chinese. If the customer’s device meets one of these conditions, the customer will receive a message in the relevant language.
1
2
{% else %}
This is a message from Braze! This is going to go to anyone who did not match the other specified languages!
You have the option to include an {% else %}
statement in your conditional logic. If none of the conditions that you set are met, the {% else %}
statement specifies the message that should send. In this case, we default to English if a customer’s language is not English, Spanish or Chinese.
1
{% endif %}
The {% endif %}
tag signals that you’ve finished your conditional logic. You must include the
{% endif %}
tag in any message with conditional logic. If you do not include an {% endif %}
tag in your conditional logic, you’ll get an error as Braze will be unable to parse your message.
Accounting For Null Attribute Values
Conditional logic is a useful way to account for null attribute values. A null value occurs when the value of a custom attribute has not been set. For example, a user who has not yet set their first name will not have a first name in Braze’s database.
In some circumstances, you may wish to send a completely different message to users who have a first name set and users who do not have a first name set.
The following tag allows you to specify a message for users with a null “first name” attribute:
1
2
3
4
{% if ${first_name} == blank %}
....
{% endif %}