Sub topic 7
Segmenting by behaviour
Behavioural segments group subscribers by what they have done. The actions that matter most are opens, clicks, purchases, site visits, and replies. This chapter assumes you have read the earlier chapters on subscriber data, segment rules, and engagement signals. You should be comfortable with boolean logic and time windows, and able to read simple SQL. The SQL examples use PostgreSQL. If your email service provider (ESP) has a visual segment builder, the same logic applies. Each clause in the SQL maps to one condition in the builder.
What makes a segment behavioural
A behavioural segment is defined by a condition on recent events. A profile field such as country describes who a person is. An event such as email_click records something they did, at a particular time. Because behaviour changes, a behavioural rule almost always contains three parts:
- An event type, such as a click or a purchase.
- A time window, such as the last 30 days.
- Often a count threshold, such as at least twice.
It also needs a fourth check that people forget: the subscriber must still be subscribed. Consent comes first, and a person who unsubscribed last week and clicked a link the week before still matches an event rule.
The working model is a filter over two tables. One table holds subscribers and their status. The other holds events, one row per action, with a subscriber id, an event type, and a timestamp.
A first behavioural segment: recent clickers
The simplest useful behavioural segment is "clicked an email in the last 30 days". It is a good audience for a message that assumes the reader is paying attention.
-- PostgreSQL: subscribers who clicked an email in the last 30 days
SELECT s.id
FROM subscribers s
WHERE s.status = 'subscribed'
AND EXISTS (
SELECT 1
FROM events e
WHERE e.subscriber_id = s.id
AND e.event_type = 'email_click'
AND e.occurred_at >= now() - interval '30 days'
);The query has two conditions joined by AND. The first is the consent check on status. The second uses EXISTS, which asks whether at least one matching event is present. EXISTS stops at the first match, so it does not count anything. It also cannot return the same subscriber twice, which a plain join to the events table would do for anyone with several clicks.
The time window is a rolling window. now() - interval '30 days' is recalculated each time the query runs, so a subscriber who clicked 29 days ago is in today and out tomorrow. That is the behaviour you want for most campaigns. It also means the segment is dynamic, a point covered in the chapter on static and dynamic segments.
Choosing the event
The event you choose decides what the segment means. Different events carry different strength of evidence.
| Event | What it tells you | Main weakness |
|---|---|---|
| Open | The message was rendered, possibly | Mail privacy features can fire opens automatically, so it can overstate attention |
| Click | A person chose to follow a link | Security scanners can click links before the person sees the email |
| Site visit | Interest beyond email | Needs tracking and consent on the website |
| Purchase | Strongest commercial signal | Rare, so segments are small |
| Reply | Strong signal of real attention | Hard to capture in many ESPs |
Prefer clicks over opens when you have a choice, and prefer purchases over clicks when the goal is revenue. The engagement chapter explains why opens are unreliable. If your data shows a subscriber who "clicked" a dozen links within one second of delivery, treat it as a scanner and not as a person. Many teams exclude clicks that happen within a few seconds of send time, or clicks on every link in a message.
Adding a count threshold
One click can be an accident. Repeated actions are better evidence. A count threshold uses GROUP BY and HAVING instead of EXISTS.
-- PostgreSQL: subscribers who clicked at least twice on running content in the last 60 days
SELECT subscriber_id
FROM events
WHERE event_type = 'email_click'
AND topic = 'running'
AND occurred_at >= now() - interval '60 days'
GROUP BY subscriber_id
HAVING COUNT(*) >= 2;This query is the shape of an interest segment, which later chapters develop. It appears here because the mechanism is the same for behaviour. WHERE filters individual events first. GROUP BY then collapses the remaining events into one row per subscriber. HAVING filters those groups by their count.
A common mistake is to put the count in WHERE, as in WHERE COUNT(*) >= 2. PostgreSQL rejects that, because WHERE runs before any grouping exists. Use HAVING for conditions on aggregates.
Note that this query does not check subscription status. In a real segment, join the result to subscribers and filter on status = 'subscribed', as the first example does. Without that check, unsubscribed people can appear in the output.
Segments built on absence
Some of the most useful behavioural segments describe people who did not do something. Examples are subscribers who have not clicked in 90 days, or customers who viewed a product and did not buy. In boolean terms these use NOT. In SQL they use NOT EXISTS.
-- PostgreSQL: subscribed contacts with no click in the last 90 days
SELECT s.id
FROM subscribers s
WHERE s.status = 'subscribed'
AND NOT EXISTS (
SELECT 1
FROM events e
WHERE e.subscriber_id = s.id
AND e.event_type = 'email_click'
AND e.occurred_at >= now() - interval '90 days'
);Absence segments have two traps.
- New subscribers match automatically. Someone who joined yesterday has no clicks, so they qualify as "inactive". Add a condition such as
s.created_at < now() - interval '90 days'so that the person has had a fair chance to act. - Missing data looks like inactivity. If click tracking broke for two weeks, everyone appears inactive. Check that events are arriving before you trust a "did not" segment.
The first trap is the reason lifecycle stages exist as a separate dimension. A later chapter covers them.
Time windows: picking the length
The window length is a trade-off between freshness and size. A short window gives a sharper picture of current attention but a smaller group. A long window gives a larger group that includes people whose attention has drifted.
A practical way to choose is to match the window to how often people act in your business:
- If you send daily and customers buy weekly, 14 or 30 days of clicks is a reasonable "engaged" window.
- If customers buy twice a year, a 30-day purchase window will be nearly empty. Use 180 or 365 days.
- For win-back or sunset audiences, use windows of 90 days or more, because you are looking for a lack of activity and need to be sure it is real.
Check the result before you commit. Run a count at 14, 30, and 60 days and look at how the size changes. If the count barely moves between 30 and 60 days, the longer window adds little. If it doubles, you have a large group of occasional clickers who may deserve their own treatment.
Time zones and timestamps
Store event timestamps as timestamptz in PostgreSQL, which keeps them in UTC internally. A rolling window such as now() - interval '30 days' is then unaffected by the viewer's time zone. Problems appear when you cut windows at calendar boundaries, such as "clicked this month". A subscriber's local month and your server's month differ near midnight on the first day. For segments, rolling windows avoid this, so prefer them unless you have a business reason to align to a calendar.
Common errors
These mistakes appear often in behavioural segmentation.
- Forgetting the consent check. An event rule alone does not know who unsubscribed. Always combine it with the subscription status.
- Counting automated activity. Bot clicks and prefetched opens inflate the segment. Filter them out where your ESP allows it.
- Using AND where you mean OR. "Clicked or purchased in 30 days" is an OR across event types. Writing it as AND returns only people who did both, a much smaller group.
- Mixing up the direction of the window.
occurred_at >= now() - interval '30 days'means within the last 30 days. Reversing the comparison gives events older than 30 days. - Treating one segment as permanent. A person who is engaged today will not be in six months. Dynamic rules recheck; snapshots do not.
- Making the rule too narrow. A segment of 40 people cannot support a meaningful test or a reliable read of results. The chapter on segment hygiene covers size.
A combined rule with OR
Sometimes "engaged" should mean any of several actions. Express that with OR inside a group, and keep the consent check outside it.
-- PostgreSQL: subscribed contacts who clicked or purchased in the last 30 days
SELECT s.id
FROM subscribers s
WHERE s.status = 'subscribed'
AND EXISTS (
SELECT 1
FROM events e
WHERE e.subscriber_id = s.id
AND e.event_type IN ('email_click', 'purchase')
AND e.occurred_at >= now() - interval '30 days'
);Here IN is shorthand for OR across event types, so one EXISTS covers both. If you wrote two separate EXISTS clauses, you would join them with OR, and the parentheses would matter: status = 'subscribed' AND (A OR B). Without the parentheses, AND binds tighter than OR, and the status check would apply only to the first branch. That bug lets unsubscribed people through, and it is easy to miss in a visual builder as well as in SQL.
Using behavioural segments well
A behavioural segment is useful when the message responds to the behaviour. Three pairings show the idea:
- Recent clickers receive a message that builds on what they just read.
- Cart or product viewers who did not purchase receive a reminder.
- People with no clicks in 90 days receive a re-engagement message, and then, if they stay silent, are suppressed. The hygiene chapter returns to this.
If the message would be identical for every group, the segment adds work without value. Start with the message, then define the behaviour that makes it relevant.
Exercise
A teammate writes a rule for a re-engagement campaign: "status = subscribed AND no click in the last 60 days". On the first run, the segment includes 2,000 people who joined last week. What is wrong, and how do you fix the rule?
Show answer
New subscribers have no click history, so the "no click" condition is true for them by default. The rule confuses "has not acted" with "has had no chance to act". Add a condition that the subscriber joined long enough ago, for example created_at < now() - interval '60 days', so only people who were subscribed for the whole window are included. Also confirm that click tracking was working during the window, so missing data is not mistaken for inactivity.
Exercise
Write the segment rule, in plain boolean terms, for subscribers who clicked at least three times in the last 30 days, and explain which SQL clause holds the count.
Show answer
Rule: status = subscribed AND the number of emailclick events with occurredat within the last 30 days is at least 3. In SQL, WHERE filters to click events in the last 30 days, GROUP BY subscriber_id makes one row per subscriber, and HAVING COUNT(*) >= 3 holds the count. WHERE cannot hold the count because it runs before grouping. You still need to join to subscribers to check status.