【埋点】Loading_Start 增加数数事件上报缓存处理,数数初始化前缓存事件,防止事件丢失
This commit is contained in:
parent
479e8aad93
commit
13ee0de952
|
|
@ -70,10 +70,12 @@ object StatisticUtil {
|
||||||
"adid" to AdjustManager.instance().getAaId()
|
"adid" to AdjustManager.instance().getAaId()
|
||||||
)
|
)
|
||||||
FireBaseManager.instance().reportEvent(eventKey, eventData, superPropertiesMap)
|
FireBaseManager.instance().reportEvent(eventKey, eventData, superPropertiesMap)
|
||||||
ShushuManager.instance().reportEvent(eventKey, eventData, superPropertiesMap)
|
val shushuManager = ShushuManager.instance()
|
||||||
|
shushuManager.reportEvent(eventKey, eventData, superPropertiesMap)
|
||||||
|
StatisticLogger.d("[reportEvents] shushuReady=${shushuManager.isSdkReady()} eventKey=$eventKey")
|
||||||
|
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
StatisticLogger.d("Events reported to shushu&Firebase: type=$eventKey")
|
StatisticLogger.d("[reportEvents] Events reported to shushu&Firebase: type=$eventKey")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,6 +97,14 @@ object StatisticUtil {
|
||||||
data: Map<String, Any>
|
data: Map<String, Any>
|
||||||
) {
|
) {
|
||||||
reportEvents(eventName, data)
|
reportEvents(eventName, data)
|
||||||
|
val dataStr = try {
|
||||||
|
data.entries
|
||||||
|
.sortedBy { it.key }
|
||||||
|
.joinToString(prefix = "{", postfix = "}") { (k, v) -> "$k=$v" }
|
||||||
|
} catch (t: Throwable) {
|
||||||
|
"format_failed:${t.javaClass.simpleName}"
|
||||||
|
}
|
||||||
|
StatisticLogger.d("[DataReporter] eventName: $eventName data=$dataStr")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setCommonParams(params: Map<String, Any>) {
|
override fun setCommonParams(params: Map<String, Any>) {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import cn.thinkingdata.analytics.ThinkingAnalyticsSDK
|
||||||
import com.ama.core.architecture.BaseApp
|
import com.ama.core.architecture.BaseApp
|
||||||
import com.ama.core.architecture.util.AndroidUtil.Companion.gson
|
import com.ama.core.architecture.util.AndroidUtil.Companion.gson
|
||||||
import com.gamedog.statisticreporter.StatisticLogger
|
import com.gamedog.statisticreporter.StatisticLogger
|
||||||
|
import java.util.ArrayDeque
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
|
@ -33,19 +35,43 @@ class ShushuManager private constructor() {
|
||||||
private val mAppContext = BaseApp.appContext()
|
private val mAppContext = BaseApp.appContext()
|
||||||
private val mBgScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
private val mBgScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||||
private val mMainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
private val mMainScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
||||||
private lateinit var mShushuSdk: ThinkingAnalyticsSDK
|
lateinit var mShushuSdk: ThinkingAnalyticsSDK
|
||||||
|
|
||||||
|
private data class PendingEvent(
|
||||||
|
val eventKey: String,
|
||||||
|
val params: Map<String, Any>?,
|
||||||
|
val superProperties: Map<String, Any>
|
||||||
|
)
|
||||||
|
|
||||||
|
private val initStarted = AtomicBoolean(false)
|
||||||
|
@Volatile
|
||||||
|
private var sdkReady: Boolean = false
|
||||||
|
|
||||||
|
private val pendingEventsLock = Any()
|
||||||
|
private val pendingEvents = ArrayDeque<PendingEvent>()
|
||||||
|
private val trackLock = Any()
|
||||||
|
private val maxPendingEvents = 200
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fun initShushu(shushuAppId: String, shushuServerUrl: String) {
|
fun initShushu(shushuAppId: String, shushuServerUrl: String) {
|
||||||
|
if (sdkReady) return
|
||||||
|
if (!initStarted.compareAndSet(false, true)) return
|
||||||
mMainScope.launch {
|
mMainScope.launch {
|
||||||
TDAnalytics.init(mAppContext, shushuAppId, shushuServerUrl)
|
try {
|
||||||
|
TDAnalytics.init(mAppContext, shushuAppId, shushuServerUrl)
|
||||||
|
|
||||||
val config = TDConfig.getInstance(mAppContext, shushuAppId, shushuServerUrl)
|
val config = TDConfig.getInstance(mAppContext, shushuAppId, shushuServerUrl)
|
||||||
config.setMode(/*if (BuildConfig.DEBUG) TDConfig.ModeEnum.DEBUG else*/ TDConfig.ModeEnum.NORMAL)
|
config.setMode(/*if (BuildConfig.DEBUG) TDConfig.ModeEnum.DEBUG else*/ TDConfig.ModeEnum.DEBUG)
|
||||||
mShushuSdk = ThinkingAnalyticsSDK.sharedInstance(config)
|
// config.setMode(/*if (BuildConfig.DEBUG) TDConfig.ModeEnum.DEBUG else*/ TDConfig.ModeEnum.NORMAL)
|
||||||
|
mShushuSdk = ThinkingAnalyticsSDK.sharedInstance(config)
|
||||||
configAutoTrack()
|
configAutoTrack()
|
||||||
|
sdkReady = true
|
||||||
|
flushPendingEventsAsync()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
initStarted.set(false)
|
||||||
|
StatisticLogger.e("数数SDK初始化失败", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,16 +80,12 @@ class ShushuManager private constructor() {
|
||||||
* 用户点击按钮、浏览页面、完成购买等。
|
* 用户点击按钮、浏览页面、完成购买等。
|
||||||
*/
|
*/
|
||||||
fun reportEvent(eventKey: String, params: Map<String, Any>?, superProperties: Map<String, Any>) {
|
fun reportEvent(eventKey: String, params: Map<String, Any>?, superProperties: Map<String, Any>) {
|
||||||
try {
|
if (!sdkReady) {
|
||||||
TDAnalytics.setSuperProperties(JSONObject(superProperties))
|
enqueuePendingEvent(eventKey, params, superProperties)
|
||||||
val jsonObj = params?.let {
|
if (sdkReady) flushPendingEventsAsync()
|
||||||
JSONObject(params)
|
return
|
||||||
}
|
|
||||||
TDAnalytics.track(eventKey, jsonObj)
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
}
|
||||||
|
reportEventInternal(eventKey, params, superProperties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -157,6 +179,55 @@ class ShushuManager private constructor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isSdkReady(): Boolean {
|
||||||
|
return sdkReady && this::mShushuSdk.isInitialized
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enqueuePendingEvent(eventKey: String, params: Map<String, Any>?, superProperties: Map<String, Any>) {
|
||||||
|
val safeParams = params?.toMap()
|
||||||
|
val safeSuper = superProperties.toMap()
|
||||||
|
synchronized(pendingEventsLock) {
|
||||||
|
if (pendingEvents.size >= maxPendingEvents) {
|
||||||
|
if (pendingEvents.isNotEmpty()) {
|
||||||
|
pendingEvents.removeFirst()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pendingEvents.addLast(PendingEvent(eventKey, safeParams, safeSuper))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun flushPendingEventsAsync() {
|
||||||
|
mBgScope.launch {
|
||||||
|
flushPendingEvents()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun flushPendingEvents() {
|
||||||
|
if (!sdkReady) return
|
||||||
|
val batch = ArrayList<PendingEvent>()
|
||||||
|
synchronized(pendingEventsLock) {
|
||||||
|
while (pendingEvents.isNotEmpty()) {
|
||||||
|
batch.add(pendingEvents.removeFirst())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (batch.isEmpty()) return
|
||||||
|
batch.forEach { pending ->
|
||||||
|
reportEventInternal(pending.eventKey, pending.params, pending.superProperties)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun reportEventInternal(eventKey: String, params: Map<String, Any>?, superProperties: Map<String, Any>) {
|
||||||
|
try {
|
||||||
|
synchronized(trackLock) {
|
||||||
|
TDAnalytics.setSuperProperties(JSONObject(superProperties))
|
||||||
|
val jsonObj = params?.let { JSONObject(it) }
|
||||||
|
TDAnalytics.track(eventKey, jsonObj)
|
||||||
|
StatisticLogger.d("数数SDK事件上报: $eventKey $jsonObj")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
StatisticLogger.e("数数SDK事件上报失败: $eventKey", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ plugins {
|
||||||
id("com.google.gms.google-services")
|
id("com.google.gms.google-services")
|
||||||
id("com.google.firebase.crashlytics")
|
id("com.google.firebase.crashlytics")
|
||||||
}
|
}
|
||||||
|
def buildTime = new Date().format('yyyyMMdd_HHmmss')
|
||||||
android {
|
android {
|
||||||
namespace = "com.viddin.videos.free"
|
namespace = "com.viddin.videos.free"
|
||||||
compileSdk libs.versions.compileSdk.get().toInteger()
|
compileSdk libs.versions.compileSdk.get().toInteger()
|
||||||
|
|
@ -23,6 +23,11 @@ android {
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applicationVariants.configureEach { variant ->
|
||||||
|
variant.outputs.configureEach {
|
||||||
|
outputFileName = "${variant.applicationId}_${variant.versionName}_${variant.versionCode}_${variant.buildType.name}_${buildTime}.apk"
|
||||||
|
}
|
||||||
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
debug {
|
debug {
|
||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue