Sub topic 4
Fallback values for missing data
This chapter assumes you can send a campaign in an email platform, know what contact fields are, and have inserted a basic merge tag. Examples use Liquid-style syntax. Tag and filter names vary by platform, so check your platform's documentation.
Why fallbacks exist
A merge tag pulls a value from a contact field. If the field is empty or missing, the tag renders nothing. The subscriber then sees "Hi ," or "Hi {{ first_name }}," in their inbox. This happens more often than most senders expect, because many contacts join through forms that only ask for an email address.
A fallback value (also called a default) is the text the platform uses when the field has no usable value. It keeps the sentence readable.
A basic fallback
The default filter supplies replacement text.
Hi {{ customer.first_name | default: "there" }},
Thanks for being with us.- If
first_nameis "Sam", the email reads "Hi Sam,". - If
first_nameis empty or missing, it reads "Hi there,".
Empty, missing and whitespace values
Three different situations can all produce a blank:
| Situation | Example | Common result |
|---|---|---|
| Field is missing | The contact has no `plan` field at all | Nothing, or an error, depending on the platform |
| Field is empty | `first_name` is an empty string | Some platforms treat this as empty, others as a real value |
| Field holds junk | `first_name` is a single space or "N/A" | The fallback is not used, and the junk is shown |
The third case is the most common failure. A fallback only fires when the platform considers the value empty. Cleaning bad values is covered in the data quality chapter.
Choosing good fallback text
A good fallback reads naturally in the sentence around it.
- Greeting: "there", "friend", or a team name. "Hi there," works. "Hi Valued Customer," sounds mechanical.
- Mid-sentence: write the sentence so the fallback fits. "Your plan, {{ plan | default: "current plan" }}, renews soon" can read oddly. Consider whether the sentence should be conditional instead.
- Numbers and dates: avoid made-up defaults such as "0 points". A wrong fact is worse than a blank.
When a fallback is not enough
Some content only makes sense with real data. In that case, hide the whole sentence. The conditional chapter covers this in detail, but here is the idea:
{% if customer.first_name != blank %}
Hi {{ customer.first_name }}, here is your monthly update.
{% else %}
Here is your monthly update.
{% endif %}How fallbacks fit the sending flow
The last branch is the dangerous one. Without a fallback, some platforms send an empty gap and others send the raw tag text.
Testing a fallback in code
Template engines such as Jinja2 let you check this behaviour before you rely on it. The true argument tells default to also replace empty strings. StrictUndefined raises an error when a field is missing, so a gap cannot slip through unnoticed.
from jinja2 import Environment, StrictUndefined
env = Environment(undefined=StrictUndefined)
template = env.from_string("Hi {{ first_name | default('there', true) }}, your plan is {{ plan }}.")
print(template.render(first_name="", plan="Pro"))
try:
template.render(first_name="Sam")
except Exception as error:
print(type(error).__name__)The first call prints Hi there, your plan is Pro. The second call prints UndefinedError, because plan was not supplied.
Common mistakes
- Adding a fallback to the greeting but not to other tags in the body.
- Assuming an empty string counts as missing. In Jinja2, the second argument
trueis needed for that. - Testing only with complete contacts. Always preview with a contact that has blank fields.
- Using a fallback in the subject line that reads awkwardly out of context.
Exercise
A contact's firstname field contains an empty string, and your platform's default filter only applies to missing fields. What would the email show for "Hi {{ firstname | default: "there" }},", and how could you avoid it?
Show answer
It would show "Hi ," because the field exists but is empty, so the fallback does not fire. Avoid it by using a conditional check such as {% if first_name != blank %}, by using a filter that also treats empty strings as missing, or by cleaning the data so empty values are stored as missing.