Custom events in Firebase - Time & Space Complexity
When using Firebase custom events, it's important to know how the number of events affects performance.
We want to understand how the time to send events grows as we send more events.
Analyze the time complexity of sending multiple custom events to Firebase Analytics.
for (let i = 0; i < n; i++) {
firebase.analytics().logEvent('custom_event', { event_number: i });
}
This code sends n custom events, each with a unique number, to Firebase Analytics.
We look at what repeats when sending events.
- Primary operation: Calling
logEventto send one event to Firebase. - How many times: Exactly
ntimes, once per event.
Each event requires one call to send it. So, if you double the number of events, you double the calls.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The number of calls grows directly with the number of events sent.
Time Complexity: O(n)
This means the time to send events grows in a straight line with the number of events.
[X] Wrong: "Sending many events at once only takes the same time as sending one event."
[OK] Correct: Each event requires its own call, so more events mean more time.
Understanding how event sending scales helps you design efficient analytics and avoid slowdowns in apps.
"What if we batch multiple events into one call? How would the time complexity change?"
Practice
Solution
Step 1: Understand default vs custom events
Default events cover common actions, but some user actions need custom tracking.Step 2: Identify the role of custom events
Custom events let you track specific actions unique to your app that default events don't cover.Final Answer:
To track specific user actions not covered by default events -> Option CQuick Check:
Custom events = track unique user actions [OK]
- Thinking custom events replace default events
- Assuming custom events send notifications
- Believing custom events collect location automatically
Solution
Step 1: Recall Firebase Analytics API for events
The correct method to log events islogEvent.Step 2: Confirm method signature
logEventtakes the event name and optional parameters as arguments.Final Answer:
firebase.analytics().logEvent(eventName, params) -> Option AQuick Check:
logEvent = log custom events [OK]
- Using trackEvent instead of logEvent
- Using sendEvent or createEvent which don't exist
- Confusing parameter order or names
firebase.analytics().logEvent('purchase', {item: 'book', price: 15});Solution
Step 1: Analyze the logEvent call
The code callslogEventwith event name 'purchase' and parameters object.Step 2: Check if 'purchase' is valid and parameters are correct
'purchase' is a valid event name and parameters are passed as an object correctly.Final Answer:
Logs a custom event named 'purchase' with item and price parameters -> Option BQuick Check:
logEvent('purchase', params) = logs event with params [OK]
- Thinking 'purchase' is reserved and causes error
- Assuming parameters must be strings only
- Confusing syntax causing error
firebase.analytics().logEvent('level_up', 'level', 5);Solution
Step 1: Review logEvent parameter requirements
logEvent expects the second argument as an object containing key-value pairs.Step 2: Identify the mistake in argument passing
The code passes parameters as separate arguments instead of a single object.Final Answer:
Incorrect parameter format; parameters must be in an object -> Option AQuick Check:
Parameters must be an object, not separate arguments [OK]
- Passing parameters as multiple arguments
- Assuming event name case matters
- Thinking logEvent takes only one argument
Solution
Step 1: Check event name format
Firebase recommends lowercase with underscores for event names, so 'tutorial_complete' is correct.Step 2: Verify parameters object keys and values
Parameters should be an object with clear keys. firebase.analytics().logEvent('tutorial_complete', {stepNumber: stepNumber, success: isSuccess}); uses keys 'stepNumber' and 'success' with values from variables.Step 3: Compare options for best practice
firebase.analytics().logEvent('tutorial_complete', {step: stepNumber, success: isSuccess}); uses 'step' key which is less descriptive than 'stepNumber'. firebase.analytics().logEvent('tutorialComplete', {stepNumber, isSuccess}); uses camelCase event name which is less recommended. firebase.analytics().logEvent('tutorial_complete', stepNumber, isSuccess); passes parameters incorrectly.Final Answer:
firebase.analytics().logEvent('tutorial_complete', {stepNumber: stepNumber, success: isSuccess}); -> Option DQuick Check:
Use descriptive keys and correct event name format [OK]
- Passing parameters as separate arguments
- Using camelCase event names instead of underscores
- Using unclear parameter keys
Ready to prove it?
Pass the test and this pattern moves unknown
