Firebase Analytics Setup and Events
Why Analytics Matter in Production
After launch, you need data to guide decisions. Which features do users actually use? Where do they drop off in onboarding? Which campaigns drive installs? Analytics answers these questions with quantitative evidence instead of guesswork.
Setting Up Firebase Analytics
Add the dependency (analytics is automatically initialized):
dependencies {
implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
implementation("com.google.firebase:firebase-analytics-ktx")
}
Analytics automatically tracks certain events. To log custom events:
// Log a standard event
val params = bundleOf(
FirebaseAnalytics.Param.ITEM_ID to "product_123",
FirebaseAnalytics.Param.ITEM_NAME to "Wireless Earbuds",
FirebaseAnalytics.Param.PRICE to 49.99,
FirebaseAnalytics.Param.CURRENCY to "USD"
)
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE, params)
// Log a custom event
val searchParams = bundleOf(
"search_term" to "wireless earbuds",
"results_count" to 15,
"filter_applied" to true
)
firebaseAnalytics.logEvent("search_performed", searchParams)
Recommended Event Naming
Follow a consistent naming convention:
- Use
snake_casefor event and parameter names - Prefix custom events with your domain:
shop_purchase_completed,feed_post_shared - Keep names under 40 characters
- Avoid PII (personally identifiable information) in events
Automatic Events
Firebase Analytics automatically tracks:
- session_start — When a user begins a session
- screen_view — When a screen is displayed (with Compose, use
setCurrentScreen) - first_open — First time the app is opened after install
- app_update — When the app is updated
Events, Parameters, and User Properties
Structuring Your Event Schema
A well-designed event schema is critical. Define events upfront and document them. A typical e-commerce app might track:
| Event | Key Parameters |
|---|---|
| product_viewed | product_id, category, price |
| add_to_cart | product_id, quantity, price |
| checkout_started | cart_total, item_count |
| purchase_completed | transaction_id, revenue, currency |
| search_performed | search_term, results_count, filter_applied |
| share_tapped | content_type, content_id, share_method |
User Properties
User properties are attributes about your users that persist across sessions:
// Set user properties for segmentation
firebaseAnalytics.setUserProperty("account_tier", "premium")
firebaseAnalytics.setUserProperty("preferred_language", "en")
firebaseAnalytics.setUserProperty("onboarding_completed", "true")
// Set user ID for cross-device tracking
firebaseAnalytics.setUserID("user_12345")
User properties enable audience segmentation in the Firebase console. For example, you can compare crash rates between free and premium users, or measure retention by onboarding completion status.
Privacy Considerations
Analytics must respect user privacy:
- Never log PII — No email, phone, name, or address in events
- Use consent mode — Disable analytics until user consents in regions requiring it
- Implement data deletion — Support Firebase's user data deletion requests
- Respect device settings — Honor Google Analytics opt-out
// Disable analytics until consent is given
firebaseAnalytics.setAnalyticsCollectionEnabled(false)
// After user grants consent
firebaseAnalytics.setAnalyticsCollectionEnabled(true)
Debugging Analytics
Use the Firebase DebugView to see events in real-time during development:
# Enable debug mode
adb shell setprop debug.firebase.analytics.test_mode true
# Or add to AndroidManifest.xml
<meta-data android:name="firebase_analytics_collection_deactivated" android:value="false" />
Events appear in the DebugView panel of the Firebase console within seconds.
Quiz
1. Why should you avoid logging personally identifiable information (PII) in Firebase Analytics events?
2. What is the purpose of Firebase user properties?
3. How do you see analytics events in real-time during development?
4. Which of the following is a recommended practice for naming custom analytics events?
Flashcards
Question
How do you log a custom event in Firebase Analytics for Android?
Click to reveal answer
Answer
Use firebaseAnalytics.logEvent("event_name", bundleOf("param" to value))
Question
What is the difference between events and user properties?
Click to reveal answer
Answer
Events record specific actions a user takes. User properties are persistent attributes about the user (tier, preferences) used for segmentation.
Question
How do you enable Firebase DebugView for real-time event monitoring?
Click to reveal answer
Answer
Run adb shell setprop debug.firebase.analytics.test_mode true, then check the DebugView panel in Firebase Console.
Question
What automatic events does Firebase Analytics track?
Click to reveal answer
Answer
session_start, screen_view, first_open, and app_update are tracked automatically without manual logging.
Revision Notes
Key Takeaways
- 1. Firebase Analytics provides real-time event tracking for understanding user behavior in production
- 2. Design your event schema upfront with consistent naming conventions (snake_case, domain prefixes)
- 3. User properties enable audience segmentation — compare metrics across user groups
- 4. Never log PII in analytics events; implement consent mode for privacy compliance
- 5. Use DebugView during development to verify events fire correctly before shipping
Interview Tips
- • Design an analytics event schema for a specific app (e-commerce, social, productivity)
- • Discuss how analytics data informs product decisions and feature prioritization
- • Explain privacy considerations and how to implement consent-based analytics
- • Know when to use events vs user properties vs parameters
Cheat Sheet
Analytics Cheat Sheet
Setup:
implementation("com.google.firebase:firebase-analytics-ktx")
Logging Events:
"param1" to value1,
"param2" to value2
))
User Properties:
firebaseAnalytics.setUserProperty("key", "value")
firebaseAnalytics.setUserID("user_id")
Privacy Rules:
- Never log PII (email, phone, name)
- Implement consent mode for GDPR/CCPA
- Support data deletion requests
Debug Mode:
adb shell setprop debug.firebase.analytics.test_mode true
Event Naming: snake_case, max 40 chars, domain prefix