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.