Hone logo
Hone
Problems

Event Tracking System in Python

This challenge focuses on building a basic event tracking system in Python. Event tracking is crucial for understanding user behavior, identifying areas for improvement, and measuring the effectiveness of features within an application. You'll design a system to record events with associated metadata, allowing for later analysis and reporting.

Problem Description

You are tasked with creating a Python class called EventTracker that allows you to record events with associated data. The EventTracker class should have the following functionalities:

  1. Initialization: The class should be initialized with a unique application identifier (e.g., "website", "mobile_app").
  2. Record Event: A method called record_event should accept two arguments:
    • event_name (string): A descriptive name for the event (e.g., "button_click", "page_view").
    • event_data (dictionary): A dictionary containing metadata associated with the event. This can include things like user ID, timestamp, page URL, etc.
  3. Get Events: A method called get_events should return a list of all recorded events. Each event in the list should be a dictionary containing the event_name, event_data, and a timestamp (using datetime.datetime.now()).
  4. Clear Events: A method called clear_events should remove all recorded events from the tracker.

Key Requirements:

  • The EventTracker class must be implemented.
  • The record_event method must store the event name, event data, and a timestamp.
  • The get_events method must return a list of events in the specified format.
  • The clear_events method must effectively remove all stored events.
  • The application identifier should be stored internally and not exposed as a method.

Expected Behavior:

The system should reliably record events with associated data and provide a mechanism to retrieve and clear these events. The timestamp should accurately reflect when the event was recorded.

Edge Cases to Consider:

  • Empty event_data dictionary.
  • event_name being an empty string. (Handle gracefully - perhaps log a warning or skip the event).
  • Multiple calls to record_event with the same event name and data. (Should be recorded as separate events with different timestamps).

Examples

Example 1:

Input:
tracker = EventTracker("my_app")
tracker.record_event("button_click", {"user_id": 123, "button_id": "submit"})
tracker.record_event("page_view", {"page_url": "/home"})
events = tracker.get_events()
Output:
[
    {'event_name': 'button_click', 'event_data': {'user_id': 123, 'button_id': 'submit'}, 'timestamp': datetime object},
    {'event_name': 'page_view', 'event_data': {'page_url': '/home'}, 'timestamp': datetime object}
]
Explanation: Two events were recorded and stored with their respective data and timestamps.

Example 2:

Input:
tracker = EventTracker("web_app")
tracker.record_event("login", {"user_id": 456})
tracker.clear_events()
events = tracker.get_events()
Output:
[]
Explanation: An event was recorded, then all events were cleared, resulting in an empty list.

Example 3: (Edge Case)

Input:
tracker = EventTracker("mobile_app")
tracker.record_event("", {"user_id": 789}) # Empty event name
tracker.record_event("purchase", {}) # Empty event data
events = tracker.get_events()
Output:
[
    {'event_name': 'purchase', 'event_data': {}, 'timestamp': datetime object}
]
Explanation: The empty event name was skipped (or handled gracefully), and the purchase event with empty data was recorded.

Constraints

  • The application identifier must be a string.
  • event_name must be a string.
  • event_data must be a dictionary.
  • The timestamp should be a datetime object.
  • The get_events method should return a list of dictionaries.
  • The solution should be reasonably efficient for a small number of events (up to 1000). Performance is not a primary concern for this challenge.

Notes

  • You can use the datetime module for timestamping.
  • Consider using a list to store the events internally.
  • Think about how to handle edge cases like empty event names or data.
  • Focus on clarity and readability of your code. Good variable names and comments are encouraged.
  • Error handling is not explicitly required, but consider how your code would behave with invalid input.
Loading editor...
python