Bird
Raised Fist0
Firebasecloud~3 mins

Why Custom events in Firebase? - Purpose & Use Cases

Choose your learning style10 modes available
The Big Idea

What if you could instantly know exactly how users interact with your app without messy code?

The Scenario

Imagine you want to track when users perform specific actions in your app, like clicking a special button or completing a level. Without custom events, you'd have to write separate code everywhere to log these actions manually.

The Problem

This manual logging is slow and easy to forget. It leads to inconsistent data because some actions might not be tracked properly. Also, updating or adding new tracked actions means changing code in many places, which is error-prone and time-consuming.

The Solution

Custom events let you define and send specific action data easily from your app to Firebase Analytics. This way, you track exactly what matters without cluttering your code. It centralizes event tracking and makes your analytics clear and reliable.

Before vs After
✗ Before
console.log('User clicked special button'); // scattered logging
✓ After
firebase.analytics().logEvent('special_button_click');
What It Enables

Custom events enable you to understand user behavior deeply and improve your app based on real actions.

Real Life Example

A game developer tracks when players finish a level or buy an item using custom events to see which parts are most popular and where players get stuck.

Key Takeaways

Manual tracking is slow and inconsistent.

Custom events simplify and centralize action tracking.

They provide clear insights to improve your app.

Practice

(1/5)
1. What is the main purpose of custom events in Firebase Analytics?
easy
A. To replace default events with simpler ones
B. To automatically collect user location data
C. To track specific user actions not covered by default events
D. To send notifications to users

Solution

  1. Step 1: Understand default vs custom events

    Default events cover common actions, but some user actions need custom tracking.
  2. 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.
  3. Final Answer:

    To track specific user actions not covered by default events -> Option C
  4. Quick Check:

    Custom events = track unique user actions [OK]
Hint: Custom events track unique actions default events miss [OK]
Common Mistakes:
  • Thinking custom events replace default events
  • Assuming custom events send notifications
  • Believing custom events collect location automatically
2. Which Firebase function is used to log a custom event with parameters?
easy
A. firebase.analytics().logEvent(eventName, params)
B. firebase.analytics().trackEvent(eventName, params)
C. firebase.analytics().sendEvent(eventName, params)
D. firebase.analytics().createEvent(eventName, params)

Solution

  1. Step 1: Recall Firebase Analytics API for events

    The correct method to log events is logEvent.
  2. Step 2: Confirm method signature

    logEvent takes the event name and optional parameters as arguments.
  3. Final Answer:

    firebase.analytics().logEvent(eventName, params) -> Option A
  4. Quick Check:

    logEvent = log custom events [OK]
Hint: Use logEvent to send custom events with parameters [OK]
Common Mistakes:
  • Using trackEvent instead of logEvent
  • Using sendEvent or createEvent which don't exist
  • Confusing parameter order or names
3. What will happen if you run this code snippet?
firebase.analytics().logEvent('purchase', {item: 'book', price: 15});
medium
A. Throws a syntax error due to incorrect parameter format
B. Logs a custom event named 'purchase' with item and price parameters
C. Logs a default event ignoring parameters
D. Sends an error because 'purchase' is a reserved event name

Solution

  1. Step 1: Analyze the logEvent call

    The code calls logEvent with event name 'purchase' and parameters object.
  2. 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.
  3. Final Answer:

    Logs a custom event named 'purchase' with item and price parameters -> Option B
  4. Quick Check:

    logEvent('purchase', params) = logs event with params [OK]
Hint: logEvent with name and object logs event with parameters [OK]
Common Mistakes:
  • Thinking 'purchase' is reserved and causes error
  • Assuming parameters must be strings only
  • Confusing syntax causing error
4. Identify the error in this Firebase custom event logging code:
firebase.analytics().logEvent('level_up', 'level', 5);
medium
A. Incorrect parameter format; parameters must be in an object
B. Event name must be uppercase
C. logEvent requires only one argument
D. The event name 'level_up' is invalid

Solution

  1. Step 1: Review logEvent parameter requirements

    logEvent expects the second argument as an object containing key-value pairs.
  2. Step 2: Identify the mistake in argument passing

    The code passes parameters as separate arguments instead of a single object.
  3. Final Answer:

    Incorrect parameter format; parameters must be in an object -> Option A
  4. Quick Check:

    Parameters must be an object, not separate arguments [OK]
Hint: Pass parameters as one object, not separate arguments [OK]
Common Mistakes:
  • Passing parameters as multiple arguments
  • Assuming event name case matters
  • Thinking logEvent takes only one argument
5. You want to track when users complete a tutorial step with step number and success status. Which is the best way to log this custom event in Firebase?
hard
A. firebase.analytics().logEvent('tutorial_complete', {step: stepNumber, success: isSuccess});
B. firebase.analytics().logEvent('tutorial_complete', stepNumber, isSuccess);
C. firebase.analytics().logEvent('tutorialComplete', {stepNumber, isSuccess});
D. firebase.analytics().logEvent('tutorial_complete', {stepNumber: stepNumber, success: isSuccess});

Solution

  1. Step 1: Check event name format

    Firebase recommends lowercase with underscores for event names, so 'tutorial_complete' is correct.
  2. 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.
  3. 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.
  4. Final Answer:

    firebase.analytics().logEvent('tutorial_complete', {stepNumber: stepNumber, success: isSuccess}); -> Option D
  5. Quick Check:

    Use descriptive keys and correct event name format [OK]
Hint: Use descriptive keys and underscore event names [OK]
Common Mistakes:
  • Passing parameters as separate arguments
  • Using camelCase event names instead of underscores
  • Using unclear parameter keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard