Skip to content

Firebase Analytics Event — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Firebase Analytics Event. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your Firebase Analytics events don't appear in the dashboard, or you hit the 25-parameter limit and events are dropped.

Wrong Approach ❌

// Logging events with too many parameters
val params = Bundle()
params.putString("item_name", "Product Name That Is Way Too Long...")
params.putString("item_category", "Electronics")
params.putString("item_brand", "Brand Name")
params.putString("item_variant", "Blue")
params.putString("item_size", "Large")
params.putString("item_color", "Navy Blue")
params.putString("item_material", "Cotton")
// 20 more params...
FirebaseAnalytics.getInstance(this).logEvent("purchase_complete", params)

Output: Event parameters truncated or event dropped entirely.

Right Approach ✅

class AnalyticsService(private val firebaseAnalytics: FirebaseAnalytics) {

    fun logPurchase(itemId: String, price: Double, currency: String = "USD") {
        // Bundle limited to 25 params, each string max 100 chars
        val params = Bundle().apply {
            putString(FirebaseAnalytics.Param.ITEM_ID, itemId.take(100))
            putDouble(FirebaseAnalytics.Param.PRICE, price)
            putString(FirebaseAnalytics.Param.CURRENCY, currency.take(10))
            putString(FirebaseAnalytics.Param.ITEM_NAME, "Item Name".take(100))
            putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "Electronics".take(100))
            // Max 25 params total — use recommended params when possible
        }
        firebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE, params)
    }

    fun logScreenView(screenName: String, screenClass: String) {
        firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
            param(FirebaseAnalytics.Param.SCREEN_NAME, screenName.take(100))
            param(FirebaseAnalytics.Param.SCREEN_CLASS, screenClass.take(100))
        }
    }

    // Custom event
    fun logCustomEvent(name: String, properties: Map<String, String>) {
        // Custom event names: max 40 chars, alphanumeric + underscore
        val validEventName = name.take(40).replace(Regex("[^a-zA-Z0-9_]"), "_")

        val params = Bundle()
        // Max 25 custom params
        properties.entries.take(25).forEach { (key, value) ->
            // Param names: max 40 chars
            params.putString(key.take(40), value.take(100))
        }
        firebaseAnalytics.logEvent(validEventName, params)
    }
}

// For Compose
@Composable
fun TrackedScreen(screenName: String) {
    val firebaseAnalytics = remember { FirebaseAnalytics.getInstance(LocalContext.current) }
    LaunchedEffect(screenName) {
        firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
            param(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
        }
    }
    // Screen content
}

Output: Events logged correctly and visible in Firebase console.

Prevention

  • Limit event names to 40 characters (alphanumeric + underscore).
  • Limit parameter names to 40 characters, values to 100 characters.
  • Limit to 25 parameters per event.
  • Use Firebase recommended events/params for automatic reporting features.

Common Mistakes with Firebase analytics event

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How long does it take for events to appear in the dashboard?

Events typically appear within 24 hours. Real-time view is available in the DebugView (enable debug mode with adb shell setprop debug.<a href="/apis/firebase/">Firebase</a>.analytics.app .your.package.name).

### What are recommended events?

Predefined events like purchase, add_to_cart, screen_view unlock Firebase features like funnel analysis, revenue tracking, and audience building. Use them when applicable.

### Can I log events from a background service?

Yes. Use FirebaseAnalytics.getInstance(context) in any context. Events are batched and sent periodically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro