Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

Android - Firebase Analytics doesnt log my event

I created a Roll dice app following the Google introduction course to Kotlin. I am now implementing Firebase Analytics to track each dice rolled.

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()
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try to Enabling debug mode To enable Analytics Debug mode on an Android device, execute the following commands (do not forget to add package name) to terminal:

adb shell setprop debug.firebase.analytics.app com.example.rolldiceapp

If you have more than one emulator running, you have to use adb -s SERIAL (SERIAL for the unique id of the emulator). If one device and one emulator are connected you can use shortcuts: adb -d ... for device and adb -e ... for emulator.

After performing steps from Enabling debug mode, make sure that date and time on your debug device or emulator and on your PC is correct. If after correcting the date and time events are still not showing on DebugView, clear the app storage. Then restart the app and try again.

Also make sure that you have the latest Google Play Services installed in the device/emulator or nothing is guaranteed to work. If you go to the settings on your emulator, there is Update button for that. Unfortunately it requires you to sign-in via your Google account.

You can also study this question. Seems that your problem similar to this questions. So examine all answers, there are very useful


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...