firebase加入 common 和 user param
This commit is contained in:
parent
9be0e2246b
commit
7a7af80d31
|
|
@ -82,15 +82,15 @@ object StatisticUtil {
|
||||||
eventName: String,
|
eventName: String,
|
||||||
data: Map<String, Any>
|
data: Map<String, Any>
|
||||||
) {
|
) {
|
||||||
StatisticUtil.reportEvents(eventName, data)
|
reportEvents(eventName, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setCommonParams(params: Map<String, Any>) {
|
override fun setCommonParams(params: Map<String, Any>) {
|
||||||
val aaa = 1
|
FireBaseManager.instance().setCommonParams(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setUserParams(params: Map<String, Any>) {
|
override fun setUserParams(params: Map<String, Any>) {
|
||||||
val aaa = 1
|
FireBaseManager.instance().setUserParams(params)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -115,10 +115,8 @@ class AdjustManager private constructor() {
|
||||||
if (!mSpHelper.hasIdentityUserType()) {
|
if (!mSpHelper.hasIdentityUserType()) {
|
||||||
mChecker.startPolling()
|
mChecker.startPolling()
|
||||||
}
|
}
|
||||||
setReporter2Bill()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setReporter2Bill() {
|
|
||||||
AdRevenueManager.addReporter(object: AdRevenueReporter {
|
AdRevenueManager.addReporter(object: AdRevenueReporter {
|
||||||
override fun reportAdRevenue(adRevenueData: AdRevenueData) {
|
override fun reportAdRevenue(adRevenueData: AdRevenueData) {
|
||||||
reportAdRevenueInfo(adRevenueData)
|
reportAdRevenueInfo(adRevenueData)
|
||||||
|
|
@ -126,7 +124,9 @@ class AdjustManager private constructor() {
|
||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reportAdRevenueInfo(revenueValue: AdRevenueData) {
|
|
||||||
|
|
||||||
|
private fun reportAdRevenueInfo(revenueValue: AdRevenueData) {
|
||||||
val adjustAdRevenue = AdjustAdRevenue(getRandomFixSourceStr()).apply {
|
val adjustAdRevenue = AdjustAdRevenue(getRandomFixSourceStr()).apply {
|
||||||
setRevenue(revenueValue.revenue.value, revenueValue.revenue.currencyCode /*"USD"*/) // ad收益数值及单位
|
setRevenue(revenueValue.revenue.value, revenueValue.revenue.currencyCode /*"USD"*/) // ad收益数值及单位
|
||||||
setAdRevenueNetwork(revenueValue.adRevenueNetwork) // 渠道来源
|
setAdRevenueNetwork(revenueValue.adRevenueNetwork) // 渠道来源
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ package com.gamedog.statisticreporter.firbase
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import com.ama.core.architecture.BaseApp
|
import com.ama.core.architecture.BaseApp
|
||||||
|
import com.ama.core.architecture.util.AndroidUtil.Companion.gson
|
||||||
|
import com.gamedog.statisticreporter.StatisticLogger
|
||||||
import com.google.firebase.Firebase
|
import com.google.firebase.Firebase
|
||||||
import com.google.firebase.FirebaseApp
|
import com.google.firebase.FirebaseApp
|
||||||
import com.google.firebase.analytics.FirebaseAnalytics
|
import com.google.firebase.analytics.FirebaseAnalytics
|
||||||
|
|
@ -11,6 +13,7 @@ import com.google.firebase.analytics.analytics
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -152,6 +155,132 @@ class FireBaseManager private constructor() {
|
||||||
mDefaultParams.clear()
|
mDefaultParams.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setUserParams(params: Map<String, Any>) {
|
||||||
|
try {
|
||||||
|
// 如果获取为空,则异步等待阻塞获取
|
||||||
|
/*if (analytics == null) {
|
||||||
|
StatisticLogger.d("Firebase Analytics未就绪,开始异步等待...")
|
||||||
|
analytics = runBlocking {
|
||||||
|
waitForFirebaseAnalytics()
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if (mFbAnalytics == null) {
|
||||||
|
StatisticLogger.w("无法获取Firebase Analytics实例,跳过设置用户参数")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Firebase Analytics使用setUserProperty设置用户属性
|
||||||
|
params.forEach { (key, value) ->
|
||||||
|
try {
|
||||||
|
mFbAnalytics.setUserProperty(key, value.toString())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("设置Firebase Analytics用户属性失败: $key = $value", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StatisticLogger.d("Firebase Analytics用户参数设置完成: ${mapToJson(params)}")
|
||||||
|
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("Firebase Analytics设置用户参数失败", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setCommonParams(params: Map<String, Any>) {
|
||||||
|
try {
|
||||||
|
// 如果获取为空,则异步等待阻塞获取
|
||||||
|
/*if (mFbAnalytics == null) {
|
||||||
|
StatisticLogger.d("Firebase Analytics未就绪,开始异步等待...")
|
||||||
|
mFbAnalytics = runBlocking {
|
||||||
|
waitForFirebaseAnalytics()
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if (mFbAnalytics == null) {
|
||||||
|
StatisticLogger.w("无法获取Firebase Analytics实例,跳过设置公共参数")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Firebase Analytics使用setDefaultEventParameters设置默认事件参数
|
||||||
|
val bundle = mapToBundle(params)
|
||||||
|
try {
|
||||||
|
mFbAnalytics.setDefaultEventParameters(bundle)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("设置Firebase Analytics默认事件参数失败", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
StatisticLogger.d("Firebase Analytics公共参数设置完成: ${mapToJson(params)}")
|
||||||
|
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("Firebase Analytics设置公共参数失败", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mapToBundle(map: Map<String, Any>): Bundle {
|
||||||
|
val bundle = Bundle()
|
||||||
|
try {
|
||||||
|
map.forEach { (key, value) ->
|
||||||
|
when (value) {
|
||||||
|
is String -> bundle.putString(key, value)
|
||||||
|
is Int -> bundle.putInt(key, value)
|
||||||
|
is Long -> bundle.putLong(key, value)
|
||||||
|
is Double -> bundle.putDouble(key, value)
|
||||||
|
is Float -> bundle.putFloat(key, value)
|
||||||
|
is Boolean -> bundle.putBoolean(key, value)
|
||||||
|
else -> bundle.putString(key, value.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("Map转Bundle失败: ${e.message}")
|
||||||
|
}
|
||||||
|
return bundle
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将Map转换为JSON字符串(用于日志)
|
||||||
|
* @param map 要转换的Map
|
||||||
|
* @return JSON字符串
|
||||||
|
*/
|
||||||
|
private fun mapToJson(map: Map<String, Any>): String {
|
||||||
|
return try {
|
||||||
|
gson.toJson(map)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("Map转JSON失败: ${e.message}")
|
||||||
|
"{}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private suspend fun waitForFirebaseAnalytics(): FirebaseAnalytics? {
|
||||||
|
var attempts = 0
|
||||||
|
val maxAttempts = 10 // 最多尝试10次
|
||||||
|
val delayMs = 100L // 每次等待100ms
|
||||||
|
|
||||||
|
while (attempts < maxAttempts) {
|
||||||
|
val analytics = getFirebaseAnalytics()
|
||||||
|
if (analytics != null) {
|
||||||
|
return analytics
|
||||||
|
}
|
||||||
|
|
||||||
|
StatisticLogger.d("Firebase Analytics未就绪,等待中... (${attempts + 1}/$maxAttempts)")
|
||||||
|
delay(delayMs)
|
||||||
|
attempts++
|
||||||
|
}
|
||||||
|
|
||||||
|
StatisticLogger.e("等待Firebase Analytics超时,无法获取实例")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getFirebaseAnalytics(): FirebaseAnalytics? {
|
||||||
|
return try {
|
||||||
|
val context = BaseApp.appContext()
|
||||||
|
return FirebaseAnalytics.getInstance(context)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("获取Firebase Analytics实例失败", e)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue