Angular Application Monitoring Dashboard
In modern web applications, understanding user behavior, application performance, and potential errors is crucial for providing a seamless user experience and maintaining a healthy application. This challenge asks you to build a basic monitoring dashboard within an Angular application that tracks key metrics and displays them in a user-friendly interface.
Problem Description
Your task is to implement a monitoring system in an Angular application that tracks and displays the following metrics:
- Page Views: Count how many times each route (or "page") has been visited.
- User Interactions: Track specific user actions, such as button clicks or form submissions.
- Error Reporting: Log any errors that occur within the application.
You will need to create services to handle the tracking and a component to display the collected data.
Key Requirements:
- Tracking Service: Create an
MonitoringServicethat exposes methods to:trackPageView(route: string): Increments the count for a given route.trackInteraction(interactionName: string, data?: any): Records a user interaction.reportError(error: any, component?: string): Logs an error with optional component context.
- Data Storage (In-Memory): For this challenge, use in-memory data structures (e.g., Maps or Objects) within the
MonitoringServiceto store the metrics. - Dashboard Component: Create a
MonitoringDashboardComponentthat:- Injects the
MonitoringService. - Displays the aggregated page view counts.
- Lists recent user interactions.
- Shows a log of reported errors.
- Injects the
- Integration: Ensure that page views are tracked automatically when routes change. Demonstrate tracking user interactions with at least one example (e.g., a button click). Demonstrate error reporting by intentionally triggering an error.
Expected Behavior:
- When a user navigates between different routes in the application, the
trackPageViewmethod should be called for each route. - When a specific action (like clicking a "Log Interaction" button) is performed, the
trackInteractionmethod should be called. - When an error occurs (e.g., calling an undefined function), the
reportErrormethod should be called and the error should be logged. - The
MonitoringDashboardComponentshould display real-time updates to the monitored data.
Edge Cases:
- What happens if
trackPageViewis called with an empty string for the route? (Should probably be ignored or logged as an invalid input). - How should identical interactions with different
datapayloads be handled? (They should be logged as distinct events ifdatais considered part of the interaction).
Examples
Example 1: Page View Tracking
Scenario: A user navigates from the home page (/) to the about page (/about).
MonitoringService State (after navigation):
pageViews: Map<string, number> = {
'/': 1,
'/about': 1
}
Explanation:
The MonitoringService's trackPageView method is called twice, once for the initial route / and once for /about. The internal count for each route is updated.
Example 2: User Interaction Tracking
Scenario: A user clicks a button labeled "Add Item" on the product details page.
MonitoringService State (after interaction):
interactions: Array<{ name: string, timestamp: Date, data?: any }> = [
{ name: 'AddItemClick', timestamp: <current_timestamp> }
]
Explanation:
The trackInteraction method is called with interactionName: 'AddItemClick'. A new entry is added to the interactions array, recording the event name and the timestamp.
Example 3: Error Reporting
Scenario: An attempt is made to access a property of undefined within a component.
MonitoringService State (after error):
errors: Array<{ error: any, timestamp: Date, component?: string }> = [
{ error: TypeError('Cannot read properties of undefined (reading 'someProperty')'), timestamp: <current_timestamp>, component: 'ProductListComponent' }
]
Explanation:
An error is caught, and the reportError method is called with the error object and the name of the component where it occurred. This error is added to the errors log.
Constraints
- Language: TypeScript
- Framework: Angular (latest stable version recommended)
- Data Storage: In-memory data structures are sufficient for this challenge. No persistence (e.g., to backend or local storage) is required.
- Performance: The monitoring system should not significantly impact the overall performance of the Angular application. For in-memory storage, operations should be O(1) or O(log n) where possible.
- Error Handling: Implement basic error handling for the monitoring system itself (e.g., graceful degradation if a tracking function fails).
Notes
- Consider using Angular's route guards or lifecycle hooks to automatically trigger page view tracking.
- For displaying data, use Angular's data binding and potentially pipes for formatting.
- Think about how you would clear or reset the monitoring data if needed (though not explicitly required for this challenge).
- This challenge focuses on the core implementation of tracking and displaying data. Advanced features like real-time updates via WebSockets or persistence are out of scope.
- You will need to simulate route changes and user interactions within your Angular application to test your solution.
- A good way to simulate an error is to intentionally throw an exception or call a method on
nullorundefined.