From 1123dc2c9ec1b3b8f07ad11da07a3770fe4e27c9 Mon Sep 17 00:00:00 2001
From: renhaoting <370797079@qq.com>
Date: Sun, 4 Jan 2026 18:18:31 +0800
Subject: [PATCH] =?UTF-8?q?firebase=20=E6=8E=A5=E5=85=A5=E5=92=8C=E9=83=A8?=
=?UTF-8?q?=E5=88=86=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
StatisticReporter/build.gradle.kts | 11 ++
.../src/main/AndroidManifest.xml | 1 +
.../firbase/FireBaseManager.kt | 118 +++++++++++++++++-
build.gradle | 3 +
gradle/libs.versions.toml | 9 +-
5 files changed, 135 insertions(+), 7 deletions(-)
diff --git a/StatisticReporter/build.gradle.kts b/StatisticReporter/build.gradle.kts
index 8140c31..70ae1cf 100644
--- a/StatisticReporter/build.gradle.kts
+++ b/StatisticReporter/build.gradle.kts
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinAndroid)
+ id("com.google.gms.google-services")
+ id("com.google.firebase.crashlytics")
}
android {
@@ -54,4 +56,13 @@ dependencies {
api("com.google.android.gms:play-services-ads-identifier:18.2.0")
implementation("cn.thinkingdata.android:ThinkingAnalyticsSDK:3.0.2")
+
+ // 导入 Firebase BoM(Bill of Materials)统一管理版本
+ implementation(platform("com.google.firebase:firebase-bom:33.16.0"))
+
+ // 添加你需要的 Firebase 产品依赖(无需指定版本号)
+ implementation("com.google.firebase:firebase-analytics")
+ implementation("com.google.firebase:firebase-crashlytics")
+ implementation("com.google.firebase:firebase-config")
+
}
\ No newline at end of file
diff --git a/StatisticReporter/src/main/AndroidManifest.xml b/StatisticReporter/src/main/AndroidManifest.xml
index 9141bca..dd6ea36 100644
--- a/StatisticReporter/src/main/AndroidManifest.xml
+++ b/StatisticReporter/src/main/AndroidManifest.xml
@@ -3,5 +3,6 @@
+
\ No newline at end of file
diff --git a/StatisticReporter/src/main/java/com/gamedog/statisticreporter/firbase/FireBaseManager.kt b/StatisticReporter/src/main/java/com/gamedog/statisticreporter/firbase/FireBaseManager.kt
index b8871a6..529cbb2 100644
--- a/StatisticReporter/src/main/java/com/gamedog/statisticreporter/firbase/FireBaseManager.kt
+++ b/StatisticReporter/src/main/java/com/gamedog/statisticreporter/firbase/FireBaseManager.kt
@@ -1,11 +1,14 @@
package com.gamedog.statisticreporter.firbase
+
+import android.content.Context
+import android.os.Bundle
import com.ama.core.architecture.BaseApp
+import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
-
-
+import java.util.concurrent.ConcurrentHashMap
class FireBaseManager private constructor() {
@@ -25,22 +28,125 @@ class FireBaseManager private constructor() {
private val mBgScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
private val mMainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
+ private lateinit var mFbAnalytics: FirebaseAnalytics
+ private val mDefaultParams = ConcurrentHashMap()
- fun init(fireAppId: String) {
+ init {
+ if (!::mFbAnalytics.isInitialized) {
+ mFbAnalytics = FirebaseAnalytics.getInstance(mAppContext)
+ }
+ }
+
+ fun reportEvent(eventName: String, parameters: Map? = null) {
+ if (!::mFbAnalytics.isInitialized) {
+ throw IllegalStateException("FirebaseAnalyticsUtil not initialized. Call init() first.")
+ }
+
+ val bundle = Bundle().apply {
+ // 默认参数添加
+ mDefaultParams.forEach { (key, value) ->
+ when (value) {
+ is String -> putString(key, value)
+ is Int -> putInt(key, value)
+ is Long -> putLong(key, value)
+ is Double -> putDouble(key, value)
+ is Float -> putFloat(key, value)
+ is Boolean -> putBoolean(key, value)
+ }
+ }
+
+ // 本次事件特定参数添加
+ parameters?.forEach { (key, value) ->
+ when (value) {
+ is String -> putString(key, value)
+ is Int -> putInt(key, value)
+ is Long -> putLong(key, value)
+ is Double -> putDouble(key, value)
+ is Float -> putFloat(key, value)
+ is Boolean -> putBoolean(key, value)
+ else -> putString(key, value.toString())
+ }
+ }
+ }
+
+ mFbAnalytics.logEvent(eventName, bundle)
}
- //----------------------- PRIVATE ------------------------//
+ /**
+ * 记录推荐事件(使用 Firebase 预设事件类型)
+ * @param eventType 事件类型,如 FirebaseAnalytics.Event.SELECT_CONTENT
+ * @param parameters 事件参数
+ */
+ fun reportRecommendedEvent(eventType: String, parameters: Bundle) {
+ if (!::mFbAnalytics.isInitialized) {
+ throw IllegalStateException("FirebaseAnalyticsUtil not initialized. Call init() first.")
+ }
+ mFbAnalytics.logEvent(eventType, parameters)
+ }
+ /**
+ * 设置用户属性
+ * @param propertyName 属性名称
+ * @param propertyValue 属性值
+ */
+ fun setUserProperty(propertyName: String, propertyValue: String) {
+ if (!::mFbAnalytics.isInitialized) return
+ mFbAnalytics.setUserProperty(propertyName, propertyValue)
+ }
+ /**
+ * 设置用户ID
+ * @param userId 用户唯一标识
+ */
+ fun setUserId(userId: String) {
+ if (!::mFbAnalytics.isInitialized) return
+ mFbAnalytics.setUserId(userId)
+ }
+ /**
+ * 设置当前屏幕
+ * @param screenName 屏幕名称
+ * @param screenClass 屏幕类名(可选)
+ */
+ fun setCurrentScreen(context: Context, screenName: String, screenClass: String? = null) {
+ if (!::mFbAnalytics.isInitialized) return
+
+ val bundle = Bundle().apply {
+ putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
+ screenClass?.let { putString(FirebaseAnalytics.Param.SCREEN_CLASS, it) }
+ }
+
+ mFbAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
+ }
+
+ /**
+ * 设置默认事件参数(将自动添加到每个事件中)
+ * @param parameters 默认参数映射
+ */
+ fun setDefaultEventParameters(parameters: Map) {
+ mDefaultParams.clear()
+ mDefaultParams.putAll(parameters)
+ }
+
+ /**
+ * 添加单个默认事件参数
+ */
+ fun addDefaultEventParameter(key: String, value: Any) {
+ mDefaultParams[key] = value
+ }
+
+ /**
+ * 清除所有默认事件参数
+ */
+ fun clearDefaultEventParameters() {
+ mDefaultParams.clear()
+ }
}
-
-
diff --git a/build.gradle b/build.gradle
index 44379a8..de87cb7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -21,6 +21,9 @@ plugins {
alias(libs.plugins.moduleGraph) apply true // Plugin applied to allow module graph generation
alias(libs.plugins.kotlin.kapt)
+
+ id("com.google.gms.google-services") version "4.4.3" apply false
+ id("com.google.firebase.crashlytics") version "2.9.9" apply false
}
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index b13ad87..0e7d485 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -28,6 +28,10 @@ swiperefreshlayout = "1.1.0"
material = "1.12.0"
protobuf = "4.29.3"
kotlinxSerializationJson = "1.8.1"
+
+google-services = "4.4.3"
+crashlytics-gradle = "2.9.9"
+
#================================Other库相关===================================
baseRecyclerViewAdapterHelper = "4.1.6"
magicIndicator = "1.7.0"
@@ -65,6 +69,7 @@ firebaseBom = "34.1.0"
lifecycleProcess = "2.8.3"
+
[libraries]
#================================Test相关===================================
junit = { group = "junit", name = "junit", version.ref = "junit" }
@@ -150,7 +155,6 @@ firebase-crashlytics = { group = "com.google.firebase", name = "firebase-crashly
-
[plugins]
# android
androidApplication = { id = "com.android.application", version.ref = "agp" }
@@ -177,3 +181,6 @@ versions = { id = "com.github.ben-manes.versions", version.ref = "check-versions
moduleGraph = { id = "com.jraska.module.graph.assertion", version.ref = "moduleGraph" }
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
room = { id = "androidx.room", version.ref = "roomRuntime" }
+
+google-services = { id = "com.google.gms.google-services", version.ref = "google-services" }
+firebase-crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "crashlytics-gradle" }
\ No newline at end of file