Sub topic 2
How a referral program works
A referral program lets existing subscribers invite others in exchange for recognition or rewards. This chapter walks through the working model, the pieces you need to build, and the errors that most often break a first version.
Prerequisites
This chapter assumes you know how an email service provider (ESP) stores subscribers, what a confirmed (double opt-in) subscription is, and how to read short Python and SQL snippets. Designing the rewards themselves comes in a later chapter. Here the focus is the mechanics.
The working model
A referral program is a loop with five steps:
- A subscriber receives a personal link.
- The subscriber shares it with a friend or audience.
- The friend opens the link and signs up.
- The system records which subscriber the signup came from.
- Once the signup is confirmed, the referrer's count goes up and any milestone reward is unlocked.
The new subscriber then receives their own link, so the loop repeats.
The pieces you need
Every referral program, whether you build it or buy it from a tool, needs the same four components.
| Component | Job | Example |
|---|---|---|
| Referral code | Identifies the referrer in a link | `?ref=Ab3dE9xQ2k` |
| Referrals table | Maps each referred subscriber to a referrer | `referrals(referrer_id, referred_id)` |
| Qualification rule | Decides which signups count | Confirmed and still subscribed |
| Reward logic | Triggers a reward at a threshold | Five referrals unlock a bonus issue |
Generating a referral code
A code must be unique per subscriber and hard to guess. If codes are sequential, anyone can invent a valid one. An HMAC (a keyed hash) of the subscriber id gives a short code that outsiders cannot predict without your secret.
import base64
import hashlib
import hmac
def referral_code(subscriber_id: int, secret: bytes) -> str:
digest = hmac.new(secret, str(subscriber_id).encode(), hashlib.sha256).digest()
return base64.urlsafe_b64encode(digest)[:10].decode()
code = referral_code(42, b"keep-this-secret-server-side")
link = f"https://example.com/subscribe?ref={code}"
print(link)Store the code and the subscriber id together in a table. An HMAC cannot be reversed, so the table is how you look up the referrer when a signup arrives with ?ref=... in the URL.
Counting only qualified referrals
The most important design choice is what counts as a referral. Counting raw clicks or unconfirmed signups rewards fake addresses and typos. Count a referral only when the new subscriber has confirmed their address and has not unsubscribed. This query lists referrers who have reached a five-referral milestone:
SELECT r.referrer_id,
COUNT(*) AS confirmed_referrals
FROM referrals AS r
JOIN subscribers AS s ON s.id = r.referred_id
WHERE s.confirmed_at IS NOT NULL
AND s.unsubscribed_at IS NULL
GROUP BY r.referrer_id
HAVING COUNT(*) >= 5
ORDER BY confirmed_referrals DESC;Run it on a schedule, or whenever a subscriber confirms, and send the reward to each referrer who newly appears in the results. Record rewards already sent so nobody receives the same reward twice.
Common errors
- Counting at click or signup time. A click is not a subscriber. Wait for confirmation, or your numbers will include bots and mistyped addresses.
- Losing the referral code between pages. If the visitor lands on one page and signs up on another, the
refvalue must be carried along, usually in a cookie or a hidden form field. Otherwise the signup is credited to nobody. - Letting people refer themselves. Reject a referral when the referred email matches the referrer, or when it is already on your list.
- Rewarding without a delay. Paying instantly makes abuse profitable. A short wait, such as a week, lets early unsubscribes drop out before you pay.
- Guessable codes. Sequential ids or short numbers let people claim credit they did not earn.
- Ignoring existing subscribers. Someone already on your list who clicks a referral link should not count as a new referral.
Putting it together
A minimal version needs one table for subscribers, one for referrals, a code per subscriber, a signup form that captures ref, and a scheduled job that runs the qualification query. Start with that. You can add dashboards and tiered rewards once the counts match what you expect.
Exercise
A visitor clicks a referral link, enters an email address, but never clicks the confirmation email. Should the referrer's count increase, and why? Name the column in the SQL query that enforces your answer.
Show answer
No. The signup is unconfirmed, so it may be a typo or a fake address and it is not yet a real subscriber. The s.confirmed_at IS NOT NULL condition excludes it. The referral can be counted later if the visitor confirms.
Exercise
Your referral link works when a visitor signs up from the landing page, but signups that happen after the visitor reads two other pages are never credited. What is the likely cause and a fix?
Show answer
The ref value is lost when the visitor moves between pages. Store it in a cookie or session when the first page loads, and read it from there when the signup form is submitted (or add it as a hidden field on the form).