Sub topic 16
Measuring the funnel and testing the flow
This chapter assumes you have built the pieces from earlier chapters: the subscribers and consent_events tables, signed confirmation tokens, the GET/POST confirmation page, and a suppression list. Here you measure how the flow performs and write tests that stop it from breaking.
The funnel
A double opt-in flow loses people at each step. Count each step separately so you can see where they leave.
Your consent_events table already holds most of the data. Each signup event starts a funnel, and each confirm event ends the confirmation step. This query gives the confirmation rate and the median time to confirm for each sign-up source:
SELECT
s.source,
count(*) AS signups,
count(c.occurred_at) AS confirmed,
round(100.0 * count(c.occurred_at) / count(*), 1) AS confirm_rate_pct,
percentile_cont(0.5) WITHIN GROUP (
ORDER BY c.occurred_at - s.occurred_at
) AS median_time_to_confirm
FROM consent_events s
LEFT JOIN consent_events c
ON c.subscriber_id = s.subscriber_id
AND c.event_type = 'confirm'
WHERE s.event_type = 'signup'
AND s.occurred_at >= now() - interval '30 days'
GROUP BY s.source
ORDER BY signups DESC;The LEFT JOIN keeps sign-ups that never confirmed. An inner join would drop them and make every rate look like 100%.
Metrics worth tracking
| Metric | How to compute it | What a bad value suggests |
|---|---|---|
| Confirmation rate | confirmed / sign-ups | Confirmation email lands in spam, or the page is confusing |
| Time to confirm | `confirm` minus `signup` | Most people confirm within minutes. A long tail means resends matter |
| Bounce rate on confirmation email | hard bounces / sent | Typos or fake addresses on the form |
| Unsubscribe rate after the first send | unsubscribes / first-send recipients | A mismatch between what the form promised and what you sent |
| Complaint rate | spam complaints / delivered | Weak consent or a hard-to-find unsubscribe link |
Read these by source and by cohort, not only as one overall number. A single bad landing page can hide behind a healthy average.
A low confirmation rate does not always mean the flow is broken. Double opt-in filters out typos, bots, and people who were never interested. Compare it with the cost of mailing those addresses, not with an ideal of 100%.
Testing the flow
Test the behaviours that cause real harm when they fail. Using pytest and the functions from the token chapter:
import time
import pytest
from tokens import make_token, verify_token
def test_valid_token_returns_email():
token = make_token("[email protected]", "confirm")
assert verify_token(token, "confirm") == "[email protected]"
def test_wrong_purpose_is_rejected():
token = make_token("[email protected]", "confirm")
assert verify_token(token, "unsubscribe") is None
def test_expired_token_is_rejected():
token = make_token("[email protected]", "confirm", ttl_seconds=-1)
assert verify_token(token, "confirm") is None
def test_tampered_token_is_rejected():
token = make_token("[email protected]", "confirm")
payload, signature = token.split(".")
forged = payload[:-2] + "AA." + signature
assert verify_token(forged, "confirm") is None
@pytest.mark.parametrize("garbage", ["", "abc", "a.b.c", "....", "%%%.%%%"])
def test_garbage_does_not_raise(garbage):
assert verify_token(garbage, "confirm") is NoneAdd tests at the HTTP level for the scanner and repeat-click cases:
- A GET request to the confirmation URL leaves the subscriber
pending. - A POST changes the status to
confirmedand writes oneconfirmevent. - A second POST leaves
confirmed_atunchanged. - A suppressed address never appears in the send query, even if its status is
confirmed.
Exercise
Your confirmation rate is 38% overall, but 80% for the newsletter page and 12% for a contest form. What should you check first, and why not just remove double opt-in?
Show answer
Check the contest form's addresses and traffic: fake or throwaway addresses, bots, and people who entered only for the prize explain low confirmation without a flow bug. Also check the confirmation email's placement and wording for that source. Removing double opt-in would put those unconfirmed addresses on your list, which raises bounces and complaints and hurts deliverability.