Sub topic 2
The Data Segments Are Built From
This chapter assumes you know basic email marketing terms (campaign, click rate) and have used an email service provider or CRM. The SQL is optional, but it shows the shape of the data. Every segment rule you write later can only be as good as the data underneath it, so this chapter covers what to collect and how to store it.
Three kinds of data
Segment rules read from three kinds of data. Each answers a different question.
| Data type | What it holds | Changes how | Best for answering |
|---|---|---|---|
| Profile fields | One current value per subscriber, such as status or signup date | Overwritten | "What is true of this person now?" |
| Events | One row per action, with a timestamp | Appended, never edited | "What did they do, and when?" |
| Tags | A label attached to a subscriber | Added or removed | "Has someone or something marked them?" |
Profile fields
A profile field is a single attribute stored on the subscriber record. Examples are status, created_at, last_purchase_at, and country. Fields suit lifecycle rules because a stage depends on a few dates and flags.
Two problems appear often:
- Overwriting loses history. If
last_purchase_atis replaced on each order, you can no longer see the earlier purchases. Store purchases as events if you need the history. - Empty values. A null
last_purchase_atmeans "never purchased" only if your system guarantees it. Decide what null means and write it down.
Events
An event is a record that something happened. It needs at least a subscriber, a type, and a timestamp. Interest rules also need a topic on the event. The timestamp is what makes time windows such as "in the last 30 days" possible. Without it you can count actions but not recency.
Typical event types are email_open, email_click, purchase, and page_view. Keep the names consistent, because email_click and EmailClick will be counted as two different things.
Tags
A tag is a label such as webinar-attendee or vip. Tags are quick to apply and easy for non-technical colleagues to use. They usually carry no timestamp, so they go stale. A tag applied two years ago looks the same as one applied yesterday. Use tags for facts that rarely change or for manual overrides. Use events when the time of the action matters.
A minimal schema
This PostgreSQL schema supports every example 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')),
created_at timestamptz NOT NULL DEFAULT now(),
last_purchase_at timestamptz
);
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 DEFAULT now()
);
CREATE TABLE subscriber_tags (
subscriber_id bigint NOT NULL REFERENCES subscribers (id),
tag text NOT NULL,
PRIMARY KEY (subscriber_id, tag)
);
CREATE INDEX events_lookup_idx
ON events (subscriber_id, event_type, occurred_at);The index matters in practice. Segment rules filter events by subscriber, type, and time, and without an index each evaluation scans the whole table.
Checking the data before you use it
Run a quick audit before building rules on any field. This query shows how many events of each type arrived in the last 30 days and how many lack a topic.
SELECT event_type,
COUNT(*) AS events,
COUNT(*) FILTER (WHERE topic IS NULL) AS missing_topic
FROM events
WHERE occurred_at >= now() - interval '30 days'
GROUP BY event_type
ORDER BY events DESC;If email_click has a high missing_topic count, an interest rule will silently ignore many clicks. Fix the tracking first.
Common errors
- Collecting everything. Each field needs a rule that uses it. Unused data is storage and privacy exposure with no benefit. The next chapter covers consent boundaries.
- Treating
statusas optional. Every segment should also check that the person is still subscribed, as the behavioural example in this tutorial does. - Mixing time zones. Store timestamps with a time zone, as
timestamptzdoes, so windows are computed consistently. - Trusting opens. Some mail clients preload images, which records an open the person never made. Treat opens as weak evidence. Clicks are stronger.
Exercise
A subscriber bought running shoes in January and a jacket in October. Your system stores only a last_purchase_at field, overwritten on each order. Which lifecycle or interest question can you no longer answer, and what change fixes it?
Show answer
You can no longer tell what the January purchase was, or how often they buy. Record each purchase as an event with a type, a topic or product category, and a timestamp. Keep last_purchase_at as a convenience field if you want, derived from those events.