Build a client side Analytics Library for tracking events
Topic: Javascript
Difficulty: Medium
👑 Premium

Clarifying Questions
- How will this library be loaded in the browser?
- What kind of events should be tracked?
- What is the expected volume of events?
- Should the library support batching of events or send events immediately?
- Should the tracking persist across browser reloads?
- Should we handle retries on failed network calls?
Explanation
This is a client-side analytics library built in javascript that can batch events, retry on failure, and persist them in localStorage. It ensures reliability using `navigator.sendBeacon` during page unload and attempts to resend failed events with a configurable retry limit. The design emphasizes reliability and fault tolerance in a minimal analytics tracking setup.
Solution Code
Browser Preview
Console Output
Scale-Ups
- Implement batching of events to reduce network calls
- Add retry logic for failed API calls with a configurable retry limit
- How can we ensure that event data is not lost on page reloads or navigation?
- How can we minimize the impact of the library on page performance?
Interviewer's Expectations
- Queue events and persist them using localStorage
- Batch and send events at regular intervals
- Retry failed API calls with a retry limit
- Use navigator.sendBeacon on page unload to ensure no data loss
- Ensure the library can handle high event volumes efficiently
- Include unit tests to validate the functionality of the library
#Analytics#Javascript#Design Patterns