Sub topic 2
Events and user identity: the raw material
Every metric in this tutorial, whether activation rate, stickiness or a retention curve, is computed from one table of events. If that table is wrong, the metrics are wrong in ways that are hard to see. This chapter covers what an event is, how events are tied to people, and the errors that corrupt the data before any metric is calculated.
It assumes you know how a product is used, you are comfortable with ratios, and you can read basic SQL (SELECT, JOIN, GROUP BY). Window functions are not needed.
What an event is
An event is a record that something happened. It has four parts:
- Who: an identifier for the actor, such as
user_idoranonymous_id. - What: an event name, such as
project_created. - When: a timestamp,
event_time. - Context: properties such as platform, plan, app version, or the id of the object involved.
Most analytics tools store this as one wide table with one row per event. The later chapters assume a simplified shape: events(user_id, event_name, event_time).
Two design choices decide how useful the data will be.
Naming. Use a consistent pattern, such as object_action in past tense: project_created, invite_sent, report_exported. Do not put variable data in the name (clicked_button_blue). Put it in a property. Names that vary by platform (ProjectCreated on iOS, project_created on web) force every query to list aliases, and someone will eventually miss one.
Granularity. Track events that mark a decision or a result, not every mouse movement. Page views and scroll events are cheap to collect but poor evidence of value. A small set of meaningful events is easier to define metrics on than a flood of low-level ones. Later chapters use one core_action event, the action that best shows the product doing its job. Choosing it is a product decision, not a tracking decision.
User identity
Metrics count people, so every event must resolve to one person. Real data makes that hard, because the same person appears under several identifiers over time:
- Before signup, a visitor has only an
anonymous_id, usually a cookie or device id. - After signup or login, the visitor has a
user_id. - The same person may use a phone and a laptop, giving two anonymous ids and one user id.
- Clearing cookies creates a new anonymous id.
Identity stitching links these identifiers. When a visitor signs up or logs in, the tracker sends an identify call that records the pair (anonymous_id, user_id). Events with only an anonymous id are later attributed to the user through that link. This matters for activation and funnels, because the steps before signup (landing page, pricing page) are recorded under the anonymous id.
Resolving identity in SQL
The query below deduplicates raw events and resolves each one to a person. It is written for PostgreSQL (DISTINCT ON is PostgreSQL-specific). It assumes events_raw(event_id, anonymous_id, user_id, event_name, event_time, received_at) and identity_map(anonymous_id, user_id).
-- Resolve raw events to one person each, dropping duplicate deliveries
WITH deduped AS (
SELECT DISTINCT ON (event_id) *
FROM events_raw
ORDER BY event_id, received_at
)
SELECT
d.event_id,
COALESCE(d.user_id, m.user_id) AS resolved_user_id,
d.event_name,
d.event_time
FROM deduped d
LEFT JOIN identity_map m
ON m.anonymous_id = d.anonymous_id;COALESCE prefers the user_id on the event itself and falls back to the link. Events from visitors who never signed up keep a null resolved_user_id. That is correct, since they are not yet users. Keep them out of user-level metrics, but they remain useful for the top of a funnel.
The join is only safe if identity_map has exactly one user per anonymous id. If a shared device maps one anonymous id to two users, the join duplicates every event from that device. Check for it before using the map:
-- Anonymous ids linked to more than one user
SELECT anonymous_id, COUNT(DISTINCT user_id) AS users_linked
FROM identity_map
GROUP BY anonymous_id
HAVING COUNT(DISTINCT user_id) > 1;Decide a policy for the rows this returns. Common choices are to keep the earliest link, to keep the latest, or to exclude the ambiguous id. Write the policy down, because it changes your counts.
Time: three timestamps, one clock
An event can carry several times, and they differ:
| Timestamp | Set by | Use |
|---|---|---|
| `event_time` | The client, when the action happened | Defining windows, such as "within 7 days of signup" |
| `received_at` | The server, when the event arrived | Deduplication and pipeline monitoring |
| `signup_at` | The user table | Anchoring cohorts and windows |
Client clocks are unreliable. A phone with the wrong date can send events from the future or the past. Many trackers record both a client time and a send time, and some systems correct the event time by the difference between the client's clock and the server's. Whichever you use, store everything in UTC and convert to a local time zone only for display. A "daily" count made in local time and one made in UTC place late-evening events on different days, so DAU will differ between the two.
Mobile apps also queue events while offline and send them later. An event can therefore arrive days after it happened. A metric computed yesterday for last week can change today. Decide how long you wait before treating a period as final, and rerun recent periods during that wait.
Common errors
| Error | Symptom | Fix |
|---|---|---|
| Duplicate delivery from retries | Counts of events slightly high, some users show two signups | Give each event a unique `event_id` and deduplicate on it |
| Unlinked anonymous activity | Funnel shows a large drop between landing and signup | Stitch identities and keep the identify call reliable |
| Shared or reassigned ids | One person has implausible event volume | Detect many-to-one links and apply a written policy |
| Internal and test traffic | Inflated usage, odd spikes | Flag staff and test accounts and exclude them |
| Bot traffic | Many anonymous ids with one event each | Filter known bot user agents and rate-limit |
| Event renamed or dropped in a release | A metric falls on one date with no product change | Keep a tracking plan and a changelog, and test events before release |
The last row is the hardest to spot. A metric that drops sharply on the day of an app release is more likely a broken event than a real change in behavior. Check the tracking before investigating the product.
Why this matters for what follows
The next chapter fixes a metric by three choices: the population, the event, and the window. Each one rests on the data described here. The population is the set of resolved users. The event comes from your event names. The window uses event_time and signup_at. If identity or time is unreliable, a precise definition still yields an unreliable number.
Exercise
A team finds that 12% of events in the warehouse have a null user_id, and the 7-day activation rate looks lower than expected. Name two checks you would run before concluding the product has an activation problem.
Show answer
First, check whether the null-user events belong to anonymous visitors who later signed up. If the identity link is missing or late, their pre-signup events are not attributed, and joining through the identity map should resolve many of them. Second, check for tracking changes, duplicate deliveries, or excluded test and bot traffic that shift the counts, for example by looking for a step change in event volume on a release date. Only after identity resolution and tracking are confirmed should you treat the lower rate as real user behavior.