【问题标题】:Android - Firebase Analytics doesnt log my eventAndroid - Firebase Analytics 不记录我的事件
【发布时间】:2021-04-26 06:15:16
【问题描述】:

我按照 Google 的 Kotlin 介绍课程创建了一个掷骰子应用。 我现在正在实施 Firebase Analytics 来跟踪每个掷出的骰子。

但是,我不知道为什么我的事件没有被记录,它们没有出现在我的 Logcat 选项卡中......当我初始化我的应用程序时,我可以看到各种 Firebase 日志,但是当我单击按钮时我有一个事件,Firebase 没有记录它。我检查了我的代码,我认为问题不是来自这里。

有人帮我吗?

我与您分享我的代码以及我在终端和目录标签中的所有消息。

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

当我使用 Firebase 初始化我的应用程序时,我将在终端和 Logcat 选项卡中与您分享消息。

【问题讨论】:

    标签: android firebase google-analytics firebase-analytics


    【解决方案1】:

    你可以试试Enabling debug mode 要在 Android 设备上启用分析调试模式,请在终端执行以下命令(不要忘记添加包名称):

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

    如果你有多个模拟器在运行,你必须使用 adb -s SERIAL(SERIAL 代表模拟器的唯一 ID)。如果连接了一台设备和一台模拟器,您可以使用快捷方式: adb -d ... 用于设备, adb -e ... 用于模拟器。

    从启用调试模式执行步骤后,请确保调试设备或模拟器和 PC 上的日期和时间正确无误。 如果更正日期和时间事件后仍未在 DebugView 上显示,请清除应用存储。然后重启应用再试一次。

    另外make sure,您已在设备/模拟器中安装了最新的 Google Play 服务,否则无法保证一切正常。 如果您转到模拟器上的设置,则有更新按钮。很遗憾,它要求您通过 Google 帐户登录。

    你也可以学习this question。似乎你的问题类似于这个问题。所以检查所有的答案,有非常有用的

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 1970-01-01
      • 2019-06-21
      • 2016-11-22
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      相关资源
      最近更新 更多