0
0
Android Kotlinmobile~20 mins

Firebase Analytics in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: AnalyticsDemoScreen
This screen logs a custom event to Firebase Analytics when a button is clicked and shows a confirmation message.
Target UI
-------------------------
| Firebase Analytics Demo |
|-----------------------|
|                       |
|   [ Log Event Button ] |
|                       |
-------------------------
Add a Button labeled 'Log Event Button' in the center of the screen.
When the button is clicked, log a custom event named 'button_click' with a parameter 'button_name' set to 'Log Event Button' using Firebase Analytics.
Show a Toast message confirming the event was logged.
Starter Code
Android Kotlin
package com.example.analyticsdemo

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.analytics.FirebaseAnalytics

class AnalyticsDemoScreen : AppCompatActivity() {
    private lateinit var firebaseAnalytics: FirebaseAnalytics

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_analytics_demo_screen)

        firebaseAnalytics = FirebaseAnalytics.getInstance(this)

        val logEventButton: Button = findViewById(R.id.logEventButton)

        // TODO: Add click listener to log event and show Toast
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.analyticsdemo

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.analytics.FirebaseAnalytics

class AnalyticsDemoScreen : AppCompatActivity() {
    private lateinit var firebaseAnalytics: FirebaseAnalytics

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_analytics_demo_screen)

        firebaseAnalytics = FirebaseAnalytics.getInstance(this)

        val logEventButton: Button = findViewById(R.id.logEventButton)

        logEventButton.setOnClickListener {
            val params = Bundle().apply {
                putString("button_name", "Log Event Button")
            }
            firebaseAnalytics.logEvent("button_click", params)
            Toast.makeText(this, "Event logged!", Toast.LENGTH_SHORT).show()
        }
    }
}

We first get the FirebaseAnalytics instance for the current context. Then we find the button by its ID. We add a click listener to the button. When clicked, we create a Bundle to hold event parameters, here setting 'button_name' to identify which button was clicked. We call logEvent on the FirebaseAnalytics instance with the event name 'button_click' and the parameters. Finally, we show a Toast message to confirm the event was logged. This helps track user interactions in the app.

Final Result
Completed Screen
-------------------------
| Firebase Analytics Demo |
|-----------------------|
|                       |
|   [ Log Event Button ] |
|                       |
-------------------------
User taps the 'Log Event Button'.
App logs 'button_click' event with parameter 'button_name' = 'Log Event Button' to Firebase Analytics.
A short Toast message 'Event logged!' appears at the bottom.
Stretch Goal
Add a second button labeled 'Log Screen View' that logs a screen_view event with the screen name 'AnalyticsDemoScreen'.
💡 Hint
Use firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, Bundle().apply { putString(FirebaseAnalytics.Param.SCREEN_NAME, "AnalyticsDemoScreen") }) inside the button's click listener.