We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Event Data

Every click, signup, purchase, and page view on a website generates an event: a timestamped record that something happened. "Event-based analytics" means collecting all these user actions and analyzing them to understand how people actually use your product.

At every analytics job I've had, the event log was the single most valuable table in the warehouse. Everything else – dashboards, reports, ML features – was derived from it.

At minimum, "event" records include:

  • A timestamp – when it happened
  • A user ID or anonymous identifier – who did it
  • An event type – what they did
events = pd.DataFrame(
    {
        "timestamp": [
            "2024-01-15 09:23:11",
            "2024-01-15 09:24:05",
            "2024-01-15 10:01:33",
        ],
        "user_id": [101, 101, 202],
        "event_type": ["page_view", "signup", "login"],
    }
)
events["timestamp"] = pd.to_datetime(events["timestamp"])

The DataFrame stores the event log, and pd.to_datetime() turns timestamp strings into real datetime values.

Why Events?

Pre-aggregated metrics (like "total signups this month") are great for final reports, but impossible to investigate. When the number of signups drops, you want to know: which user segments are affected? Which marketing channels are underperforming? Are there any time-based patterns?

Raw events give you flexibility. You can slice by user segment, time window, device, or any property attached to the event. The tradeoff is that you need to do the aggregation yourself – which is exactly what pandas is good at.

Make sure timestamp columns are always parsed as a datetime with pd.to_datetime() before doing any time-based analysis.