Sub topic 2
Events and User Identity: The Raw Material
Product metrics are only as reliable as the events and identities behind them. This chapter assumes you understand basic product usage, percentages, ratios, simple charts, and SQL SELECT, JOIN, and GROUP BY.
What an event record contains
An event is a record that says something happened in a product. Examples include project_created, report_shared, and subscription_started. A useful event record usually contains:
| Field | Meaning | Example |
|---|---|---|
| `event_name` | The action that happened | `project_created` |
| `user_id` | The known person or account that acted | `u_1842` |
| `anonymous_id` | A temporary identifier before sign-in | `anon_91af` |
| `event_time` | When the action happened | `2026-01-14 09:32 UTC` |
| `event_id` | A unique identifier for this event occurrence | `evt_7c20` |
| `properties` | Extra details about the action | plan, project type |
An event should describe a meaningful product action, not an implementation detail. A page-load event may help diagnose performance, but it is usually a poor definition of engagement because it does not prove that a user received value.
A user identity is the identifier used to connect events to one person or account. Use a stable internal identifier where possible. Do not use an email address as the primary key because email addresses can change and may expose personal information.
Anonymous users and identity stitching
Before sign-in, a browser or device often receives an anonymous_id. After sign-in, the product learns the user's stable user_id. Identity stitching is the process of connecting earlier anonymous events to the later known identity.
flowchart LR
A[Anonymous browser] -->|anonymous_id: anon_91af| B[Project viewed]
A --> C[User signs in]
C -->|user_id: u_1842| D[Identity link]
B --> D
D --> E[Known user history]caption: Connecting anonymous events to a known user after sign-in alt: An anonymous browser produces an event, then signs in; an identity link joins the earlier event to the user's known history.
A safe identity model keeps both identifiers and records when the link was made. Avoid silently merging two known users. If several people share a device, or one person uses several devices, an incorrect merge can distort activation and retention.
Build a trustworthy event stream
Define an event contract before instrumenting it. An event contract is a written agreement about the event name, required fields, property types, and meaning. Keep names stable and specific. For example, use document_published, not both publish_document and documentPublished.
Common errors include:
- Sending the same event from both the client and server.
- Using the device clock instead of a server timestamp.
- Recording several retries as separate business actions.
- Renaming an event without versioning or documenting the change.
- Counting background refreshes as meaningful activity.
- Dropping events when a user moves from anonymous to known status.
A unique event_id supports deduplication. The query below keeps the first copy of each event and assumes PostgreSQL syntax.
-- Assumes events(event_id, user_id, event_name, event_time, received_at)
WITH ranked_events AS (
SELECT
event_id,
user_id,
event_name,
event_time,
received_at,
ROW_NUMBER() OVER (
PARTITION BY event_id
ORDER BY received_at
) AS copy_number
FROM events
)
SELECT
event_id,
user_id,
event_name,
event_time
FROM ranked_events
WHERE copy_number = 1;Use event_time to represent when the action occurred and received_at to represent when your system received it. Late-arriving events can then be handled without changing the user's actual behavior date.
Exercise
A user creates a project while anonymous, then signs in. Which fields should the product retain so the project-created event can be attributed without guessing?
Show answer
Retain the anonymousid on the original event, the later userid, and a recorded identity link connecting the two. Also retain the eventid and eventtime so the event can be deduplicated and placed in the correct time window.