Sub topic 11
Personalising from behaviour and events
This chapter assumes you can insert merge tags with fallbacks and write a basic if block. It also assumes you have read the chapter on consent and data minimisation. Here you will use what a contact did, such as viewing a product or abandoning a cart, to decide what an email says and when it is sent.
Attributes versus events
Earlier chapters used attributes, which are fields stored on a contact record, such as first_name or plan. An attribute holds one current value. When it changes, the old value is gone.
An event is a record that something happened at a point in time. "Viewed product 4471 at 18:05" is an event. A contact has many events, and each one keeps its own timestamp and details.
| Row | Attribute | Event |
|---|---|---|
| Shape | One value per field | Many records over time |
| Example | `plan = "pro"` | `order_placed` on 2 October |
| Answers | "What is true now?" | "What happened, and when?" |
| Typical use in email | Merge tags, segments | Triggers, recency rules, item details |
Most platforms let you turn events into attributes. For example, "category of the last product viewed" can be saved as a contact field. This is called a derived attribute. Derived attributes are easier to use in templates than raw event lists.
How behaviour reaches the platform
Behavioural data is not typed in by a person. It arrives from other systems.
- Tracking script or SDK. A snippet on your website records page views and product views for known contacts.
- Server-side API. Your shop or app sends events directly, for example
order_placed. - Webhooks or integrations. A store connector such as Shopify or WooCommerce forwards cart and order events.
- The email platform itself. Opens and clicks are events too.
The step that beginners most often skip is the exit check. It is covered below.
What an event payload looks like
An event usually has a name, a timestamp, and properties, which are the details of that event. The exact shape depends on the platform, but most look like this:
{
"event": "cart_updated",
"contact_email": "[email protected]",
"timestamp": "2026-11-26T12:10:00Z",
"properties": {
"product_name": "Trail Runner Jacket",
"product_url": "https://shop.example.com/p/4471",
"price": 89.0,
"currency": "GBP"
}
}In a triggered email, the platform makes the triggering event available to the template. The variable name varies (event, trigger, data), so check your platform's documentation.
Using event data in a template
Event properties work like any other merge tag, so they need the same fallbacks. A property can be absent because a tracking script failed, an older event used a different schema, or the product was deleted.
Hi {{ customer.first_name | default: "there" }},
You left {{ event.product_name | default: "something" }} in your basket.
<a href="{{ event.product_url | default: 'https://shop.example.com/cart' }}">Return to your basket</a>The link fallback matters most. A missing name produces awkward text. A missing URL produces a broken button.
Event data also drives conditions. Here the email changes depending on how recently the contact bought:
{% if customer.days_since_last_order == blank %}
Welcome. Here is a guide to getting started.
{% elsif customer.days_since_last_order < 30 %}
Thanks for your recent order. Here is how to get the most from it.
{% else %}
It has been a while. Here is what is new.
{% endif %}The first branch handles a contact with no orders at all. Without it, an empty value could be compared as a number and fall into the wrong branch, depending on the platform. Always put the "no data" case first.
Building derived attributes
Raw events are often too detailed for a template. A common approach is to compute a few summary fields and store them on the contact. Your platform may do this with built-in rules. If you compute it yourself, the logic looks like this:
from datetime import datetime, timezone
events = [
{"type": "product_viewed", "category": "running-shoes", "at": "2026-11-20T09:15:00+00:00"},
{"type": "order_placed", "at": "2026-10-02T17:40:00+00:00"},
{"type": "product_viewed", "category": "jackets", "at": "2026-11-24T18:05:00+00:00"},
]
def derive_attributes(events, now):
views = sorted((e for e in events if e["type"] == "product_viewed"), key=lambda e: e["at"])
orders = sorted((e for e in events if e["type"] == "order_placed"), key=lambda e: e["at"])
attrs = {}
if views:
attrs["last_viewed_category"] = views[-1]["category"]
if orders:
last_order = datetime.fromisoformat(orders[-1]["at"])
attrs["days_since_last_order"] = (now - last_order).days
return attrs
now = datetime(2026, 11, 26, tzinfo=timezone.utc)
print(derive_attributes(events, now))This prints {'last_viewed_category': 'jackets', 'days_since_last_order': 54}. Note that a contact with no orders gets no days_since_last_order key at all. The template then sees a missing value, which is why the blank branch above matters.
Timing and freshness
Behaviour loses value quickly. An email about a product viewed yesterday feels helpful. One about a product viewed four months ago feels odd.
- Set a recency window. Only use event data newer than a limit you choose, such as 30 days for browsing and 90 days for purchases. Outside the window, fall back to generic content.
- Expect delay. Events can arrive minutes or hours late if they are batched or synced. Do not build a flow that assumes instant arrival.
- Check for newer events before sending. A wait step creates a gap between trigger and send. Anything may happen in that gap.
The exit check
Suppose a cart-reminder flow waits two hours. If the shopper completes the order in that time, the reminder is now wrong and a little embarrassing. Most platforms let you define an exit condition (also called a goal or a filter). It cancels the send if a specific event, such as order_placed, has occurred since the trigger.
Add the exit condition whenever a flow includes a wait. Test it by performing the exit action yourself during a trial run.
Respecting consent and avoiding the creepy line
Behavioural data is more sensitive than a first name. Two checks apply before you use it.
- Do you have permission? Tracking what someone browses may need explicit consent in some regions, separate from consent to receive email. Use only event data you were allowed to collect. Honour withdrawals.
- Would the recipient be surprised? Even lawful personalisation can feel intrusive. "Still thinking about the Trail Runner Jacket?" is usually fine. "We noticed you looked at this four times at 2 a.m." is not. Reference the item or category, not the observation itself.
Measurement caveats
Email engagement events are weaker signals than they look. Some mail clients, including Apple Mail with Mail Privacy Protection, preload images and register an open whether or not the person read the message. Security scanners can also click every link. Do not build a segment such as "opened in the last 7 days" and treat it as proof of interest. Prefer clicks to a specific page, site events, and purchases, and filter out clicks that happen within a few seconds of delivery.
Common errors
- No fallback for event properties. The email ships with an empty product name or a dead link.
- No exit condition. Contacts who already bought receive a cart reminder.
- Stale behaviour. Content refers to something the contact viewed months ago.
- Counting machine opens. Segments based on opens include people who never saw the email.
- Missing the "no events" case. New contacts hit the wrong branch or see blank text.
- Over-personalising. The email reveals more surveillance than the contact expected.
The next chapter covers loops, which let you render several items from an event, such as every product in a cart.
Exercise
Your cart-reminder flow triggers on cart_updated, waits 2 hours, then sends an email that uses {{ event.product_name }}. Name two things that can go wrong and the fix for each.
Show answer
First, the shopper may complete the purchase during the wait, so the reminder arrives after the order. Fix this with an exit condition that cancels the send when order_placed has occurred since the trigger. Second, product_name may be missing from the event, which leaves a blank gap in the sentence. Fix this with a fallback such as {{ event.product_name | default: "something" }}. A fallback for the product link is also needed, so the button does not break.
Exercise
A contact has never placed an order, so days_since_last_order does not exist on their record. In the conditional from this chapter, why is the blank check placed first?
Show answer
A missing value might be treated as zero or compared in an unexpected way by the platform's < operator. That could send the contact into the "recent order" branch and thank them for an order they never made. Checking for blank first sends them to the correct welcome branch before any numeric comparison runs.