Sub topic 11
Rolling out and automating the sunset flow
This chapter assumes you have read the earlier chapters. You should know how engagement is defined, how to choose an inactivity window, and how a re-engagement campaign works. You should also be comfortable with basic SQL and Python. Here you turn the written sunset policy into a staged rollout and a job that runs on a schedule.
Why you should not switch it on all at once
The first run of a sunset policy usually finds a large backlog. A list that has never been cleaned may have tens of percent of its addresses past the window. Sending a re-engagement message to all of them on one day creates a volume spike to the least engaged part of your list. That can hurt the reputation you are trying to protect.
Roll out in stages instead:
- Dry run. Run the query and the stage function, but send nothing. Count how many subscribers land in each stage.
- Review the counts. Check that the numbers match what you expect from your cadence. A result of 70% suppressed usually means a data problem, such as missing click tracking, and not a truly dead list.
- Pilot batch. Send the re-engagement message to a small slice, for example 5% of the backlog. Watch bounces and complaints.
- Work through the backlog in batches. Cap each day's batch so that total volume stays steady.
- Switch to steady state. After the backlog is cleared, the job only handles subscribers who newly cross the threshold each day.
The flow as a state machine
Each subscriber is in one of four stages: active, send a re-engagement message, waiting, or suppressed. The job moves people between them.
Building the daily job
The job needs three parts. A query finds candidates, the stage function from earlier decides what to do, and a set of actions applies the decision. Reuse the query from the earlier chapter to select candidates. The function below is the decision step.
from datetime import datetime, timedelta
def sunset_stage(
subscribed_at: datetime,
last_engaged_at: datetime | None,
reengagement_sent_at: datetime | None,
now: datetime,
window_days: int = 180,
grace_days: int = 30,
) -> str:
reference = last_engaged_at or subscribed_at
if now - reference < timedelta(days=window_days):
return "active"
if reengagement_sent_at is None:
return "send_reengagement"
if now - reengagement_sent_at < timedelta(days=grace_days):
return "waiting"
return "suppress"Notice that engagement after the re-engagement message pushes reference forward, so the subscriber returns to active with no special case. The function is pure: it reads no database and sends nothing. That makes it easy to test and to run in a dry run.
A wrapper around it can apply a daily cap and a dry-run switch:
def run_sunset(subscribers, now, daily_cap=2000, dry_run=True):
to_send = []
to_suppress = []
for sub in subscribers:
stage = sunset_stage(
sub["subscribed_at"],
sub["last_engaged_at"],
sub["reengagement_sent_at"],
now,
)
if stage == "send_reengagement" and len(to_send) < daily_cap:
to_send.append(sub["id"])
elif stage == "suppress":
to_suppress.append(sub["id"])
if not dry_run:
# Replace with your ESP's send and suppression calls.
print(f"sending {len(to_send)}, suppressing {len(to_suppress)}")
return {"send": len(to_send), "suppress": len(to_suppress)}Subscribers over the cap are not lost. They stay in send_reengagement and are picked up the next day. Leave dry_run=True until the counts look right.
Guardrails to build in
- Make the job idempotent. Record
reengagement_sent_atwhen you send. If the job runs twice, nobody gets two messages. - Apply exceptions first. Filter out new subscribers, transactional-only contacts, and seasonal senders' protected segments before the stage function runs.
- Add a circuit breaker. If a run would suppress more than a set share of the list, for example 10% in a day, stop and alert a person.
- Log every decision. Store the subscriber, the stage, the date, and the policy version. You need this for the records chapter's requirements.
- Handle time zones. Compare timestamps in UTC to avoid off-by-one-day errors.
Common errors
| Symptom | Likely cause |
|---|---|
| Almost everyone is flagged on day one | Click events are missing, or tracking was added recently |
| Subscribers get the re-engagement message twice | The send time was not saved before the next run |
| Engaged people get suppressed | The engagement query ignores a event type, such as purchases made off-site |
| Volume to inactive users spikes | No daily cap on the backlog |
Exercise
Your first dry run shows 65% of the list in the "suppress" stage, though most subscribers were added within the last year. Name two things to check before sending anything.
Show answer
First, check that engagement events are being recorded completely, since missing click or purchase data makes everyone look inactive. Second, check that reengagement_sent_at is not already populated incorrectly, and that new subscribers are excluded by the exceptions filter, since a bad timestamp or missing exception can push people straight to "suppress".