I created a Roll dice app following the Google introduction course to Kotlin. I am now implementing Firebase Analytics to track each dice rolled.
- I followed Firebase instructions to install Firebase SDK within my gradle files.
- I followed Google instruction to implement event tracking
- I entered the 3 adb commands to see my log events in the Android Studio Logcat tab
But, I don′t know why my events aren't logged, they don't appear in my Logcat tab... When I initialize my app I can see various Firebase logs, but then when I click the button for which I have an event, Firebase doesn't log it. I checked my code and I don′t think problem comes from here.
Someone to help me?
I share with you my code and all messages I have in my terminal and catlog tabs.
package com.example.rolldiceapp
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.FirebaseAnalytics.Param.*
import com.google.firebase.analytics.ktx.logEvent
/*** This activity allows the user to roll a dice and view the result on the screen.***/
class MainActivity : AppCompatActivity() {
private lateinit var firebaseAnalytics: FirebaseAnalytics //Declare the FirebaseAnalytics object at the top of the activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) //Set layout with the Activity
firebaseAnalytics = FirebaseAnalytics.getInstance(this) //Initialize Firebase Analytics in the OnCreate method
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener {
rollDice()
trackClicks()
}
}
private fun rollDice() {
// Create new Dice object with 6 sides and roll it
val dice = Dice(6)
val diceRoll = dice.rollDice()
// Update the screen with the dice roll number
val resultTextView: TextView = findViewById(R.id.roll_textView)
resultTextView.text = diceRoll.toString()
// Update the screen with the dice roll image
val diceImage: ImageView = findViewById(R.id.roll_imageView)
diceImage.setImageResource(R.drawable.dice_2)
when (diceRoll) {
1 -> diceImage.setImageResource(R.drawable.dice_1)
2 -> diceImage.setImageResource(R.drawable.dice_2)
3 -> diceImage.setImageResource(R.drawable.dice_3)
4 -> diceImage.setImageResource(R.drawable.dice_4)
5 -> diceImage.setImageResource(R.drawable.dice_5)
6 -> diceImage.setImageResource(R.drawable.dice_6)
}
//Update the screen with result message
val luckyNumber = 4
val resultMessage: TextView = findViewById(R.id.resultRollText)
if (diceRoll == luckyNumber) {
resultMessage.text = ("You win! You rolled $diceRoll and it is the lucky number!").toString()
} else {
resultMessage.text = ("Sorry you rolled a $diceRoll and you need a $luckyNumber. Try again!").toString()
}
}
private fun trackClicks() {
firebaseAnalytics.logEvent("Click_Dice_track_2") {
param(SCREEN_NAME, "Dice_Homepage") // send predefined parameters
param(SCORE, value = "test")
param(SOURCE, "Local_Machine")
}
}
}
class Dice(val numSides: Int) {
fun rollDice(): Int {
return (1..numSides).random()
}
}