Sub topic 9
Keeping an audit trail of consent
This chapter assumes you have the subscriber lifecycle from the earlier chapters (pending, confirmed, unsubscribed) and basic SQL. It covers how to record consent so you can later show when, where, and how someone agreed.
Why a status column is not enough
A status column tells you what is true now. It does not tell you how it became true. If a subscriber or a regulator asks "when did this person agree, and to what?", an overwritten column cannot answer.
Under GDPR you must be able to demonstrate consent. That means keeping evidence of each change, not just the latest state.
Two tables with different jobs
The schema keeps current state and history apart:
CREATE TABLE subscribers (
id BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'confirmed', 'unsubscribed')),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
confirmed_at TIMESTAMPTZ
);
CREATE TABLE consent_events (
id BIGSERIAL PRIMARY KEY,
subscriber_id BIGINT NOT NULL REFERENCES subscribers(id),
event_type TEXT NOT NULL
CHECK (event_type IN ('signup', 'confirm', 'unsubscribe', 'preferences_updated')),
source TEXT NOT NULL,
consent_text TEXT,
ip_address INET,
user_agent TEXT,
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now()
);subscribers is fast to query when you send. consent_events is append-only: you insert rows and never update or delete them.
What each field proves
| Field | Evidence it provides |
|---|---|
| `event_type` | Which step happened: signup, confirm, unsubscribe, or a preference change |
| `source` | Where it happened, such as `footer_form`, `checkout`, or `one_click_header` |
| `consent_text` | The exact wording the person saw when they agreed |
| `ip_address`, `user_agent` | The device that made the request |
| `occurred_at` | When it happened, in UTC |
The consent_text field matters most. If your form wording changes next year, the old wording is the only proof of what people agreed to. Store the text itself, or a version identifier that points to an archived copy.
Recording events with state changes
Write the event and the state change in one transaction. Otherwise a crash can leave a confirmed subscriber with no evidence.
def record_confirmation(db, subscriber_id, ip, user_agent):
with db.transaction():
db.execute(
"UPDATE subscribers SET status = 'confirmed', confirmed_at = now() "
"WHERE id = %s AND status = 'pending'",
(subscriber_id,),
)
db.execute(
"INSERT INTO consent_events "
"(subscriber_id, event_type, source, ip_address, user_agent) "
"VALUES (%s, 'confirm', 'confirmation_page', %s, %s)",
(subscriber_id, ip, user_agent),
)Record the confirm event from the POST request, not the GET. The GET only displays a button, so it is not consent.
Flow of evidence
Common errors
- Updating or deleting event rows. Restrict the application's database role to
INSERTandSELECTonconsent_events. - Storing only the latest consent wording, so old events point at text that no longer exists.
- Keeping unsubscribe evidence only in the status column. You also need proof of when someone opted out.
- Keeping events forever without a reason. Set a retention period and document it, since IP addresses are personal data.
Exercise
A subscriber says they never signed up. Which fields in consent_events help you check, and why must you record the confirm event on POST rather than GET?
Show answer
The signup and confirm events give the times, source shows which form was used, consent_text shows the wording, and ip_address and user_agent show the device. Recording on POST matters because a GET can be triggered by a security scanner prefetching the link, so it would store evidence from a machine that did not consent.