Course contentsShow
← System Design
Lesson 1530 of 1,91937. Real-World System: URL ShortenerFree lesson

Analytics and Click Tracking

Track click metrics, geolocation, referrers, and user agents for each shortened URL without impacting redirect latency.

Analytics and Click Tracking

You'll learn how to capture detailed click metrics for shortened URLs without slowing down the critical redirect path.

The Core Challenge

When a user clicks a shortened URL, your primary job is to redirect them instantly to the destination. But you also want to collect analytics—click counts, geolocation, device type, referrer, timestamp—for each link. The trick is doing both without making the user wait.

Asynchronous Write-Behind Pattern

The redirect must remain synchronous and lightning-fast (typically <50ms). Handle analytics asynchronously:

  1. Redirect first: Look up the destination URL in cache or database and return the HTTP 301/302 immediately
  2. Log in background: Write click metadata to a message queue (Kafka, RabbitMQ) or fast write buffer
  3. Process later: Consumer services pull from the queue and persist to an analytics database

Think of it like a restaurant: the waiter serves your food immediately (redirect), then fills out paperwork in the back office later (analytics).

What to Track

  • Basic metrics: timestamp, short URL clicked
  • User context: IP address → geolocation (city/country), user agent → device/browser/OS
  • Referrer: which website/app drove the click
  • Session data: unique visitor tracking (anonymized cookies)

Storage Strategy

Store analytics separately from your operational URL database. Use:

  • Time-series database (InfluxDB, TimescaleDB) for efficient time-based queries
  • Columnar storage (ClickHouse, Druid) for fast aggregations across millions of clicks
  • Data warehouse (BigQuery, Redshift) for complex reporting

Pre-aggregate common queries (daily totals, top countries) using batch jobs overnight to keep dashboards fast.

Key Takeaway:
Decouple analytics from redirects using asynchronous processing—redirect users instantly while capturing rich click data in the background through message queues and specialized analytics storage.