Sub topic 9
How Variants Are Assigned and Exposed
Assume you know basic product metrics, percentages, and feature flags. A feature flag is a control that decides which version of a feature a user can see. This chapter focuses on assigning users to variants and recording when they actually see them.
Assignment and exposure are different
Assignment chooses a variant for an eligible unit, such as a user, account, or session. Exposure means the assigned variant was actually delivered and rendered. These events can differ:
- A user may be assigned to treatment but leave before the page loads.
- A feature flag may be evaluated, but a later permission check may hide the feature.
- A user may be assigned on the server while an old client still displays the control experience.
Analyze users after verified exposure when the question is about the effect of seeing the feature. Record assignment separately so engineers can debug missing or inconsistent assignments.
The randomization unit must match the behavior being measured. Use a user ID when users return across devices and the metric is measured per user. Use an account ID when teammates share an account and the feature affects the account. Use a session only when each session should be independent.
Use stable, experiment-specific assignment
A stable assignment gives the same eligible unit the same variant on every visit. A common method hashes the experiment key and unit ID, then maps the result to a bucket. Including the experiment key prevents the same hash pattern from coupling unrelated experiments.
import hashlib
def variant_for(unit_id: str, experiment_key: str, treatment_share: float = 0.5) -> str:
if not 0 <= treatment_share <= 1:
raise ValueError("treatment_share must be between 0 and 1")
value = f"{experiment_key}:{unit_id}".encode("utf-8")
bucket = int(hashlib.sha256(value).hexdigest()[:8], 16) % 10_000
return "treatment" if bucket < treatment_share * 10_000 else "control"
for user_id in ["user-101", "user-102"]:
print(user_id, variant_for(user_id, "checkout_address_autofill"))A production flagging system should also store the assignment decision or make it reproducible. Do not assign randomly on every request. That behavior lets one user switch variants and contaminates the comparison.
Control eligibility and collisions
Evaluate eligibility before assignment. For example, exclude logged-out visitors, unsupported browsers, internal staff, or users who already completed the target action. Keep the eligibility rule stable during the test unless the change is documented.
Experiments can collide when two tests change the same component. Use mutually exclusive groups, priority rules, or a shared allocation layer. If a user can receive both treatments, record both experiment assignments so the data team can account for the overlap.
| Concern | Required behavior |
|---|---|
| Stickiness | Keep a unit in one variant throughout the experiment |
| Allocation | Match the planned control and treatment shares |
| Eligibility | Evaluate the same rule before assignment |
| Collision handling | Prevent or record overlapping experiments |
| Identity | Use a durable ID that matches the metric |
Design the exposure path
A safe path usually follows this order:
Log exposure only after the user can receive the intended experience. Include enough context to reconstruct the decision: experiment key, randomization-unit ID, variant, eligibility result, assignment time, exposure time, and application or flag version. Avoid sending unnecessary personal data.
Client-side assignment can reduce server work, but it may briefly show the wrong variant and can be easier to tamper with. Server-side assignment gives stronger control but requires consistent identity handling across requests. Choose the location that best protects the metric and user experience.
Exercise
A user is assigned to treatment during page load, but the treatment component fails before rendering. Should this user count as exposed treatment?
Show answer
No. Keep the assignment record for debugging, but do not count the user as exposed until the intended component renders successfully.