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:
- Initialization: The class should be initialized with a unique application identifier (e.g., "website", "mobile_app").
- Record Event: A method called
record_eventshould 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.
- Get Events: A method called
get_eventsshould return a list of all recorded events. Each event in the list should be a dictionary containing theevent_name,event_data, and a timestamp (usingdatetime.datetime.now()). - Clear Events: A method called
clear_eventsshould remove all recorded events from the tracker.
Key Requirements:
- The
EventTrackerclass must be implemented. - The
record_eventmethod must store the event name, event data, and a timestamp. - The
get_eventsmethod must return a list of events in the specified format. - The
clear_eventsmethod 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_datadictionary. event_namebeing an empty string. (Handle gracefully - perhaps log a warning or skip the event).- Multiple calls to
record_eventwith 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_namemust be a string.event_datamust be a dictionary.- The timestamp should be a
datetimeobject. - The
get_eventsmethod 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
datetimemodule 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.