Sub topic 2
The data segments are built from
This chapter assumes you have used an email service provider (ESP) or CRM, and that you know terms like campaign, open rate, and click rate. You should also be comfortable with AND, OR, and NOT. The SQL examples are PostgreSQL and optional, but they show exactly what a segment tool does behind its filter screen.
Every segment is a question asked of stored data. If the data to answer the question is missing, stale, or unreliable, the segment will be wrong. That is true however clever the rule is. This chapter covers the three kinds of data a segment can use, the shape each takes, and the problems that most often break segments later.
The three kinds of subscriber data
Almost every ESP stores subscriber information in three forms.
| Kind | What it holds | Changes how | Typical use |
|---|---|---|---|
| Profile fields | Facts about one person: email, status, signup date, country, last purchase date | Overwritten in place | Lifecycle stage, location, eligibility |
| Events | A record of something that happened, with a time | Appended, never edited | Behaviour and interest |
| Tags | Short labels attached to a person | Added and removed | Manual grouping, imports, flags |
The difference between "overwritten" and "appended" matters most. A profile field holds only the current value. An event table keeps history, so you can ask what someone did in the last 30 days. A tag sits in between: it records that a label is present now, and many systems do not record when it was added or why.
Profile fields
A profile field is one value stored on the subscriber record. Fields fall into three groups.
- Identity and status. Email address and subscription status (for example
subscribed,unsubscribed,bounced,complained). Status is the most important field in the whole system. Nearly every segment should filter on it, because a rule that ignores status can select people you are not allowed to email. - Descriptive attributes. Country, language, time zone, job title, or a preferred product category. These come from signup forms, imports, or a connected store.
- Dates. Signup date (
created_at) and last purchase date (last_purchase_at). Lifecycle segments depend on these, which is why they are worth getting right.
Fields have three common weaknesses:
- They overwrite. If
last_purchase_atis replaced by each new order, you lose the earlier purchases. You can still ask "when did they last buy?" but not "how many times did they buy this year?". If you need a count, store a count or keep the orders in a separate table. - Free text is hard to filter. A "country" field containing
UK,U.K.,United Kingdom, andenglandneeds four conditions to match one place. Use a controlled list (a dropdown or a standard code) at the point of capture. - Empty is ambiguous. A null
last_purchase_atcould mean "never bought" or "the store sync did not run". Decide what null means, write it down, and check that the sync really populates the field.
Events
An event is a record that something happened to a subscriber at a particular time. Email opens, clicks, purchases, page views, and form submissions can all be events. Because events are appended and never edited, they are what let you describe behaviour over a time window.
A useful event has at least four parts:
- Who: a subscriber identifier.
- What: an event type such as
email_clickorpurchase. - When: a timestamp.
- Context: optional extra detail, such as the topic of the clicked link or the product category purchased.
The examples in this tutorial use this table. The first block creates the schema, so you can run everything that follows in a scratch PostgreSQL database.
-- PostgreSQL: a minimal schema for the examples in this tutorial
CREATE TABLE subscribers (
id bigint PRIMARY KEY,
email text NOT NULL UNIQUE,
status text NOT NULL
CHECK (status IN ('subscribed', 'unsubscribed', 'bounced', 'complained')),
created_at timestamptz NOT NULL,
last_purchase_at timestamptz,
country text
);
CREATE TABLE events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
subscriber_id bigint NOT NULL REFERENCES subscribers (id),
event_type text NOT NULL,
topic text,
occurred_at timestamptz NOT NULL
);
CREATE TABLE subscriber_tags (
subscriber_id bigint NOT NULL REFERENCES subscribers (id),
tag text NOT NULL,
added_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (subscriber_id, tag)
);
CREATE INDEX events_lookup
ON events (subscriber_id, event_type, occurred_at);
INSERT INTO subscribers (id, email, status, created_at, last_purchase_at, country) VALUES
(1, '[email protected]', 'subscribed', now() - interval '200 days', now() - interval '20 days', 'GB'),
(2, '[email protected]', 'subscribed', now() - interval '5 days', NULL, 'GB'),
(3, '[email protected]', 'unsubscribed', now() - interval '300 days', now() - interval '250 days', 'DE');
INSERT INTO events (subscriber_id, event_type, topic, occurred_at) VALUES
(1, 'email_click', 'running', now() - interval '3 days'),
(1, 'email_click', 'running', now() - interval '10 days'),
(2, 'email_click', 'cycling', now() - interval '1 day');
INSERT INTO subscriber_tags (subscriber_id, tag) VALUES
(1, 'webinar-2026'),
(2, 'imported-trade-show');The events_lookup index matches how segment queries read the table: first by person, then by type, then by time range. Without it, a large event table makes every segment slow to calculate.
The topic column
The topic column is what turns a click into an interest signal. A click on a link tagged running says something different from a click on an unlabelled link. This label does not appear by itself. Someone must add it to links or products when content is built. If your emails do not label their links, you cannot build interest segments from clicks later, and you cannot recover the missing labels afterwards.
Event types need a naming standard
Pick one name per action and keep it. If one integration sends purchase, another sends order_completed, and a third sends Purchase, a rule written for one will silently miss the others. Use lowercase, snake_case names, and keep a short list of the allowed types.
Tags
A tag is a short label attached to a subscriber, such as webinar-2026 or vip. Tags are fast to create and many tools let you apply them in bulk, so they spread quickly. That is both their strength and their risk.
Tags work well for:
- Marking where a person came from (an import, a trade show, a partner).
- Flags a human decided on, such as
do-not-promote. - Short-lived campaign groupings.
Tags work poorly as a stand-in for behaviour. A tag named engaged records that someone applied the label at some point. It does not tell you what the person did, when, or whether it is still true. If a rule such as "clicked in the last 30 days" can be calculated from events, calculate it from events. Do not rely on a tag that someone has to keep updated.
The tag table above stores added_at. Not every ESP does, so check yours. Without a date, you cannot tell a tag applied last week from one applied three years ago.
Derived values
Sometimes a rule would be easier to write if a value were stored directly, for example "days since last click" or "purchases in the last year". These are derived values: calculated from events and fields, then saved as a field or a view.
A view is the safest form. It recalculates every time you query it, so it never goes stale.
-- PostgreSQL: a derived value that is always current
CREATE VIEW subscriber_activity AS
SELECT s.id AS subscriber_id,
s.status,
max(e.occurred_at) FILTER (WHERE e.event_type = 'email_click') AS last_click_at,
count(e.id) FILTER (WHERE e.event_type = 'email_click'
AND e.occurred_at >= now() - interval '90 days') AS clicks_90d
FROM subscribers s
LEFT JOIN events e ON e.subscriber_id = s.id
GROUP BY s.id, s.status;
SELECT * FROM subscriber_activity ORDER BY subscriber_id;The LEFT JOIN keeps subscribers with no events, which show up with a null last_click_at and a zero count. An inner join would drop them, and a "has not clicked" segment would then miss the people it most needs to find. Many ESPs offer similar calculated fields. If yours stores them and refreshes them on a schedule, find out how often. A "clicks in the last 90 days" field that refreshes weekly will be up to a week out of date.
Checking the data before you segment
Run a few checks before you build rules on top of the data. They take minutes and catch the problems that otherwise appear as mysteriously small or oddly large segments.
-- PostgreSQL: quick data-quality checks
-- 1. What event types exist, and how many of each?
SELECT event_type, count(*) AS n
FROM events
GROUP BY event_type
ORDER BY n DESC;
-- 2. How many clicks carry no topic? (They cannot feed interest segments.)
SELECT count(*) AS clicks_without_topic
FROM events
WHERE event_type = 'email_click' AND topic IS NULL;
-- 3. Events dated in the future, which usually mean a time zone or clock error
SELECT count(*) AS future_events
FROM events
WHERE occurred_at > now();
-- 4. Subscribers missing a signup date
SELECT count(*) AS missing_created_at
FROM subscribers
WHERE created_at IS NULL;Check 1 reveals inconsistent naming. Check 2 tells you whether interest segments are possible at all. Check 3 matters because a time window such as "last 30 days" depends on correct timestamps. Check 4 can only return rows if the column allows nulls. The example schema forbids them with NOT NULL, but many ESP exports do not.
Common errors
- Ignoring status. Filtering on behaviour without
status = 'subscribed'includes people who have unsubscribed. They still have click history from before they left. - Mixing time zones. Store timestamps in UTC (
timestamptzin PostgreSQL does this) and convert only for display. A window boundary that shifts by hours is rarely noticed but changes who is in a segment. - Counting machine activity as human. Some mail clients and security scanners open messages or follow every link automatically. Treat opens, and clicks that occur seconds after delivery, with caution. The next chapters return to this when engagement signals are discussed.
- Assuming history exists. An event table only holds what was recorded after tracking began. A behavioural segment cannot look back further than your first stored event.
- Duplicate people. The same person signed up with two addresses has two records and two separate histories. Segments built on either record will be incomplete.
Summary of the model
Profile fields describe who someone is now. Events describe what they did and when. Tags are human-applied labels that should not replace either. Derived values combine them, and are best kept as views or refreshed fields whose update schedule you know. Behavioural and interest segments draw mainly on events. Lifecycle segments draw on profile dates. Every segment draws on status.
Exercise
Your team keeps a tag called "engaged" that a marketer applies by hand after reviewing reports each quarter. You want to email people who clicked in the last 30 days. Should you segment on the tag or on events, and why?
Show answer
Use events. The tag records only that someone applied the label at some point. It is not tied to a time window, so it can be up to a quarter out of date and says nothing about the last 30 days. A rule on email_click events with occurred_at >= now() - interval '30 days', combined with status = 'subscribed', is always current and can be inspected.
Exercise
A "has not clicked in 90 days" segment, built from a query that joins subscribers to events with an inner join, returns fewer people than you expected. What is the likely cause, and how do you fix it?
Show answer
An inner join keeps only subscribers that have at least one matching event, so people with no events at all are dropped. These are exactly the people who have not clicked. Use a LEFT JOIN (or a NOT EXISTS condition) so subscribers without events remain, then test for a null or old last_click_at.