Sub topic 3
Inserting merge tags
This chapter assumes you have sent a campaign from an email platform and know what contacts, lists, and fields are. It builds on the previous chapters, which separated merge tags from dynamic content and showed where subscriber data comes from. Here you learn how to write merge tags, how the platform turns them into text, and where they break.
What a merge tag does
A merge tag is a placeholder in your template. When the platform sends the email, it replaces the placeholder with a value from the recipient's contact record. You write one template, and each recipient gets a copy with their own values filled in.
The replacement is a lookup. The platform reads the name inside the tag, finds the field with that name on the contact (or on related data such as an order), and puts the field's value into the text. If the lookup fails, the result depends on the platform. It may print nothing, print the raw tag, or refuse to send.
Anatomy of a tag
Most platforms build a tag from three parts:
- Delimiters mark the start and end of the tag, such as
{{and}}. - A variable path names the data to insert, such as
customer.first_name. The part before the dot is the object (the contact, an order). The part after it is the field. - Optional filters change the value before it is inserted. In Liquid, a filter follows a pipe character, as in
| default: "there".
Here is the Liquid example from this tutorial's plan. It uses all three parts:
Hi {{ customer.first_name | default: "there" }},
Thanks for being with us.For Sam, this renders as "Hi Sam,". For a contact whose first name is empty, it renders as "Hi there,". Fallback values get their own chapter, so for now treat default as a safety net.
Syntax differs by platform
The idea is the same everywhere, but the delimiters and field names are not. The examples in this tutorial use Liquid-style syntax. Check your platform's documentation for the exact form before you copy a tag.
| Platform style | Typical first-name tag | Notes | Column | Column |
|---|---|---|---|---|
| Liquid-style (Shopify Email, Klaviyo and others) | `{{ customer.first_name }}` | Object names and filters vary by platform | ||
| Mailchimp | `* | FNAME | *` | Field keys are uppercase merge tags |
| HubSpot | `{{ contact.firstname }}` | Property internal names have no underscore | ||
| Braze | `{{${first_name}}}` | Attributes use a `${...}` form | ||
| Handlebars-style (such as SendGrid dynamic templates) | `{{first_name}}` | Data is supplied as JSON with each send |
The column to focus on is the field name. The same piece of data may be called first_name, FNAME, or firstname, and the tag must use the name your platform gives that field.
Mapping tags to fields
A tag only works if its name matches a real field. Check these points when you map them:
- Use the field's key, not its label. The editor may show "First name", but the key behind it might be
FNAME. Look in the field settings for the key. - Match case and spelling exactly. Many platforms treat
First_Nameandfirst_nameas different fields. - Know which object holds the data. A contact's plan lives on the contact. An order number lives on the order. Event or order data is available only in emails triggered by that event.
- Custom fields must exist first. Create the field and fill it for your contacts before you reference it. Otherwise the tag has nothing to read.
Inserting tags in the editor
Most visual editors have an "insert merge tag" or "personalise" menu. Prefer it over typing, because the menu writes the correct key for you. If you type tags by hand, follow these rules:
- Type the tag in one go, with no formatting in the middle. Bolding half of a tag can wrap part of it in extra HTML, and the platform then no longer recognises it.
- Paste from a plain-text source. Word processors can swap straight quotes for curly quotes, which breaks
default: "there". - Put the tag in the plain-text version of the email as well. Many platforms send a multipart message, and the plain-text part is a separate template.
- Check where the tag appears. Tags work in body text, subject lines, preheaders, link text, and URLs, but some platforms limit which filters or fields each place allows.
Tags in links
A tag inside a URL needs care. A value with a space, ampersand, or plus sign can break the link. Platforms offer a URL-encoding filter for this. In Liquid it is url_encode:
<a href="https://example.com/offers?email={{ customer.email | url_encode }}">See your offer</a>Also avoid putting sensitive values in URLs. Links get forwarded, logged, and scanned by security software.
Filters that change the value
Filters fix small presentation problems without changing the stored data. These are common in Liquid-style systems:
| Filter | Effect | Example result |
|---|---|---|
| `capitalize` | Uppercases the first letter and lowercases the rest | `sAM` becomes `Sam` |
| `upcase` | Uppercases everything | `sam` becomes `SAM` |
| `strip` | Removes leading and trailing spaces | `" Sam "` becomes `Sam` |
| `default` | Uses a replacement when the value is empty | `""` becomes `there` |
capitalize is a gotcha. Because it lowercases everything after the first letter, "McDonald" becomes "Mcdonald" and "de la Cruz" becomes "De la cruz". For names, it is often safer to clean the stored data than to filter it at send time. The chapter on data quality covers that.
Rendering locally to see the mechanics
You can reproduce the lookup behaviour with Jinja2, a Python template engine whose syntax is close to Liquid's. This script renders a template, then renders it again with a field named wrongly. StrictUndefined makes the missing field raise an error instead of printing a blank.
from jinja2 import Environment, StrictUndefined, UndefinedError
env = Environment(autoescape=True, undefined=StrictUndefined)
template = env.from_string(
"Hi {{ customer.first_name }}, order {{ order.number }} has shipped."
)
order = {"number": "A-1042"}
print(template.render(customer={"first_name": "Sam"}, order=order))
try:
template.render(customer={"firstname": "Sam"}, order=order)
except UndefinedError as error:
print("Error:", error)The first call prints Hi Sam, order A-1042 has shipped. The second prints an error saying the dictionary has no attribute first_name. The data holds "Sam", but under a different key, so the lookup fails. This is the most common real-world failure.
The script also sets autoescape=True. That makes the engine convert characters such as < and & in values into safe HTML. Many email platforms escape values in the same way. If a value should contain HTML on purpose, you usually need a specific filter or a different kind of block to allow it.
Common errors
- Raw tag in the sent email. The platform did not recognise the delimiters. Causes include the wrong syntax for your platform, a tag split by formatting, or a template type that does not support merge tags.
- Blank where a value should be. The field name is wrong, the field is empty for that contact, or the data is not available in this kind of send.
- Wrong person's data. A tag inside a reused block pulls from the wrong object, or a test contact was left in a saved preview.
- Broken links. An unencoded value in a URL contains characters that end the parameter early.
- Odd capitalisation. The stored value was typed in all capitals or all lowercase, or a filter changed names that should not be changed.
- Merge text visible in the inbox preview. The tag was placed in the preheader, which some platforms render separately and with different rules.
Checklist before you move on
- Find each field's key in your platform, not just its label.
- Insert tags from the editor menu when possible, and type them in one unformatted run when not.
- Check that each tag has data for realistic contacts, including ones with empty fields.
- Encode any tag used inside a URL.
- Add the same tags to the plain-text version.
Exercise
A template contains Hi {{ customer.FirstName }},. Your contact list stores the field under the key first_name. The preview shows "Hi ,". Name two likely causes and one fix.
Show answer
The tag name does not match the stored key (the case and spelling of FirstName differ from first_name). Many platforms also return an empty string for an unknown field instead of an error, which gives "Hi ,". Fix it by changing the tag to {{ customer.first_name }}, preferably with the editor's insert menu, then preview again with a contact that has a first name.
Exercise
You want a button that links to https://example.com/offers?email= followed by the contact's email address. What problem can occur if you insert the tag as is, and which Liquid filter helps?
Show answer
Email addresses can contain characters such as + that have special meaning in a URL, so the query string can be misread and the link may break. Apply the url_encode filter: {{ customer.email | url_encode }}. You should also consider whether putting an email address in a link is necessary at all.