Sub topic 13
Modelling topics, frequency, and channel preferences
This chapter assumes you have the subscriber lifecycle and consent records from earlier chapters: a subscribers table with pending, confirmed, and unsubscribed states, and an append-only consent_events table. It also assumes basic SQL and one general-purpose language. Here you design the data that sits behind a preference centre.
Why preferences need their own model
Unsubscribing is all-or-nothing. A preference model gives subscribers a middle path. Someone who finds weekly emails too frequent can switch to monthly, or drop one topic, and stay on your list. That keeps the relationship and the consent. It also lowers the chance that they press "report spam" instead.
Preferences are not the same as status. Status answers "may we email this person at all?" Preferences answer "which messages, how often, and where?" Keep the two separate, and always check status first.
The three dimensions
| Dimension | Question it answers | Example values |
|---|---|---|
| Topics | What content do they want? | `product_updates`, `weekly_digest`, `events` |
| Frequency | How often at most? | `daily`, `weekly`, `monthly` |
| Channels | Where do we reach them? | `email`, `sms`, `push` |
A pause date is a fourth, optional control. It suspends sending until a given day without losing the other choices.
A record you can store
This record holds one subscriber's choices:
{
"email": "[email protected]",
"topics": {
"product_updates": true,
"weekly_digest": true,
"events": false
},
"frequency": "weekly",
"paused_until": "2027-01-15"
}Topics are a map of name to boolean. A map lets you add a topic later without changing the shape. frequency is a ceiling: "weekly" means at most one message a week, not exactly one. paused_until is a date, and null means not paused.
Storing it in SQL
You can keep the JSON in a column, but a relational layout makes queries and constraints easier. This sketch builds on the earlier schema:
CREATE TABLE subscriber_preferences (
subscriber_id BIGINT PRIMARY KEY REFERENCES subscribers(id),
frequency TEXT NOT NULL DEFAULT 'weekly'
CHECK (frequency IN ('daily', 'weekly', 'monthly')),
paused_until DATE,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE subscriber_topics (
subscriber_id BIGINT NOT NULL REFERENCES subscribers(id),
topic TEXT NOT NULL,
enabled BOOLEAN NOT NULL,
PRIMARY KEY (subscriber_id, topic)
);The composite primary key on subscriber_topics prevents duplicate rows for the same topic. Every change should also write a preferences_updated row to consent_events, so you can show later what the person chose and when.
Decisions that cause trouble
- Defaults. Decide what a new subscriber gets. Enabling only the topics they signed up for is safer than enabling everything. Consent applies to what you told them at signup.
- New topics. When you launch a topic, do not switch it on for existing subscribers. Offer it and let them opt in.
- Absent versus false. Treat a missing topic row as "not opted in", never as "yes". Store
falseonly when the person actively turned it off. - Channels. Consent is per channel. An email opt-in does not cover SMS, so store each channel's consent separately with its own event.
- Frequency enforcement. A preference only matters if the sender checks it. Record the last send time per subscriber and skip anyone who would exceed their ceiling.
Applying preferences at send time
Sending code checks status, then pause, then topic. The suppression list from the previous chapter still comes first.
from datetime import date
def may_send(sub, topic, today=None):
today = today or date.today()
if sub["status"] != "confirmed":
return False
prefs = sub["preferences"]
paused = prefs.get("paused_until")
if paused and date.fromisoformat(paused) > today:
return False
return prefs["topics"].get(topic, False)The .get(topic, False) call implements "absent means no".
Exercise
A subscriber has "events": false in the topics map, and you launch a new topic called webinars. Should may_send allow a webinar email to this subscriber, and why?
Show answer
No. The webinars key is absent, so topics.get("webinars", False) returns False. The subscriber never opted in to that topic. You should offer it in the preference centre or a campaign and let them enable it.