SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 20
Design a website analytics pipeline and dashboard
Site owners paste a small script into their pages and then open a dashboard to see how many people visited, which pages they read, where they came from and how long they stayed. A "right now" panel shows how many visitors are on the site at this moment.
Requirements
- Collect page views and custom events from a JavaScript snippet without slowing the page.
- Group a visitor's events into sessions that end after 30 minutes of inactivity.
- Show page views, unique visitors, sessions, bounce rate and average session length over any date range, broken down by page, referrer, country and device, with filters.
- Show visitors active in the last 5 minutes, updated every few seconds.
Constraints and assumptions
- 200,000 sites; 50,000 events per second on average, 300,000 at peak. The largest site sends 30% of all events.
- A visitor is identified by a random first-party ID stored in the browser; some browsers clear it daily.
- Dashboard reports for the last 30 days should load in under 2 seconds at p95.
- Realtime panel within 1 minute of the event; daily reports may be corrected for up to 24 hours.
- Unique visitor counts within 2% are acceptable. Raw events are kept for 13 months, aggregates for 5 years.
Out of scope for the first sketch
Ad attribution and conversion modeling · Per-user journey and funnel analysis · Billing and account management
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a website analytics pipeline and dashboard
Finish your first sketch before answering these. Cross out freely.
- How does the snippet send events so the page stays fast and events are not lost when the visitor closes the tab?
- A site owner picks a 30-day range. Why can you not add up 30 daily unique-visitor counts, and what do you store instead?
- Where do you build sessions, and what happens when a session crosses midnight or its events arrive out of order?
- The largest site sends 90,000 events per second at peak. Where does that hurt in ingestion and in queries?
- An owner filters by country = Germany, device = mobile and page = /pricing together. Can pre-built rollups answer that, and if not, what does?
Compare afterwards: worked approaches and reading at sketchbook /questions/web-analytics/review