Commit 00cfa807 authored by wanglei's avatar wanglei Committed by songjianyu

【修复】 广告问题

parent ad432fec
......@@ -23,6 +23,8 @@ abstract class AdEvent {
var adUnit: String = ""
var from: String = ""
val reqId = UUID.randomUUID().toString()
var isUnLimit: Boolean = false
fun adPrepareShow() {
val obj1 = JSONObject()
......
......@@ -8,6 +8,7 @@ import androidx.annotation.LayoutRes
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.base.appzxhy.business.ads.admob.AdOpenMgr
import com.easy.clean.BuildConfig
import com.easy.clean.GlobalConfig
import com.easy.clean.MyApplication
......@@ -16,7 +17,6 @@ import com.easy.clean.bean.config.ConfigBean.Companion.configBean
import com.easy.clean.business.ads.admob.AdBannerMgr
import com.easy.clean.business.ads.admob.AdInterMgr
import com.easy.clean.business.ads.admob.AdNativeMgr
import com.easy.clean.business.ads.admob.AdOpenMgr
import com.easy.clean.business.ads.admob.AdmobEvent
import com.easy.clean.business.ads.applovin.AdMaxEvent
import com.easy.clean.business.ads.applovin.MaxInsertMgr
......@@ -181,12 +181,14 @@ object AdsMgr {
val from = activity::class.java.simpleName
if (adsConfigBean.adSwitch) {
val admobEvent = AdmobEvent("openAd", from)
admobEvent.isUnLimit = isUnLimit
if (isAdmobInit) {
adOpenMgr.show(activity, isUnLimit, AdmobEvent("openAd", from), showCallBack)
adOpenMgr.show(activity, admobEvent, showCallBack)
} else {
admobInitCallBack = {
}
adOpenMgr.show(activity, isUnLimit, AdmobEvent("openAd", from), showCallBack)
adOpenMgr.show(activity, admobEvent, showCallBack)
}
} else {
if (isMaxInit) {
......
......@@ -42,7 +42,7 @@ class AdBannerMgr {
adView = AdView(parent.context)
parent.addView(adView)
adView?.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(adView)
adView?.onPaidEventListener = AdmobOnPaidEventListener(adView, admobEvent.scope)
listener = ViewTreeObserver.OnGlobalLayoutListener {
val screenPixelDensity = parent.context.resources.displayMetrics.density
......
......@@ -18,6 +18,9 @@ import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import java.lang.ref.WeakReference
import kotlin.div
import kotlin.text.get
import kotlin.text.toInt
/**
......@@ -150,7 +153,7 @@ class AdInterMgr {
}
(adEvent as AdmobEvent).pullAd(ad.responseInfo)
LimitUtils.addRequestNum()
ad.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(ad)
ad.onPaidEventListener = AdmobOnPaidEventListener(adEvent.scope)
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
......
......@@ -52,7 +52,7 @@ class AdNativeMgr {
).forNativeAd { nativeAd ->
lastTime = System.currentTimeMillis()
nativeAd.setOnPaidEventListener(AdmobEvent.EventOnPaidEventListener(nativeAd))
nativeAd.setOnPaidEventListener(AdmobOnPaidEventListener(nativeAd, admobEvent.scope))
currentNativeAd = nativeAd
admobEvent.pullAd(nativeAd.responseInfo)
......
package com.easy.clean.business.ads.admob
package com.base.appzxhy.business.ads.admob
import android.app.Activity
import android.content.Context
......@@ -9,6 +9,9 @@ import com.easy.clean.business.ads.AdState
import com.easy.clean.business.ads.AdsShowCallBack
import com.easy.clean.business.ads.AdsType
import com.easy.clean.business.ads.LimitUtils
import com.easy.clean.business.ads.admob.AdmobEvent
import com.easy.clean.business.ads.admob.AdmobOnPaidEventListener
import com.easy.clean.utils.LogEx
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
......@@ -27,11 +30,11 @@ class AdOpenMgr {
private var showCallBack: AdsShowCallBack? = null
fun show(activity: Activity, isUnLimit: Boolean, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
fun show(activity: Activity, adEvent: AdEvent, showCallBack: AdsShowCallBack?) {
if (activity.isFinishing || activity.isDestroyed) {
return
}
if (!isUnLimit) {
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
showCallBack?.failed()
return
......@@ -53,7 +56,9 @@ class AdOpenMgr {
val needLoad = adState.currentAd == null || !adAvailable()
if (needLoad) {
if (!adState.loadingAd) {
loadAd(activity, adEvent, isUnLimit, true)
loadAd(activity, adEvent) {
showReadyAd(adEvent)
}
}
} else {
showReadyAd(adEvent)
......@@ -62,10 +67,16 @@ class AdOpenMgr {
private fun showReadyAd(adEvent: AdEvent) {
val ac = adState.activityRef?.get()
if (ac == null || ac.isFinishing || ac.isDestroyed) {
LogEx.logDebug(TAG, "showReadyAd ac=null isFinishing isDestroyed")
return
}
adState.currentAd?.run {
fullScreenContentCallback = object : FullScreenContentCallback() {
override fun onAdShowedFullScreenContent() {
val ac = adState.activityRef?.get()
(adEvent as AdmobEvent).showAd(this@run.responseInfo, ac)
showCallBack?.show()
......@@ -114,11 +125,10 @@ class AdOpenMgr {
fun loadAd(
context: Context,
adEvent: AdEvent,
isUnLimit: Boolean = false,
isShow: Boolean = false,
loadCallBack: (() -> Unit)? = null
) {
if (!isUnLimit) {
if (!adEvent.isUnLimit) {
if (!LimitUtils.isAdShow(AdsType.OPEN, adEvent)) {
this.showCallBack?.close()
this.showCallBack = null
......@@ -127,7 +137,6 @@ class AdOpenMgr {
}
}
adEvent.adPulStart()
AppOpenAd.load(
......@@ -137,13 +146,10 @@ class AdOpenMgr {
object : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdLoaded(appOpenAd: AppOpenAd) {
adState.onAdLoaded(appOpenAd)
if (this@AdOpenMgr.showCallBack != null || isShow) {
showReadyAd(adEvent)
}
loadCallBack?.invoke()
(adEvent as AdmobEvent).pullAd(appOpenAd.responseInfo)
appOpenAd.onPaidEventListener = AdmobEvent.EventOnPaidEventListener(appOpenAd)
appOpenAd.onPaidEventListener = AdmobOnPaidEventListener(appOpenAd, adEvent.scope)
LimitUtils.addRequestNum()
}
override fun onAdFailedToLoad(loadAdError: LoadAdError) {
......
......@@ -25,25 +25,34 @@ import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.math.BigDecimal
import java.util.Currency
import kotlin.Any
import kotlin.div
import kotlin.random.Random
import kotlin.text.toFloat
class AdmobEvent : AdEvent {
override val TAG: String = "AdmobEvent"
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
constructor(adUnit: String, from: String) : super() {
this.adUnit = adUnit
this.from = from
}
fun pullAd(
responseInfo: ResponseInfo?,
error: LoadAdError? = null,
) {
) = scope.launch {
var key = "ad_pull"
val obj = JSONObject()
obj.put("req_id", reqId)
if (responseInfo != null) {
......@@ -63,21 +72,18 @@ class AdmobEvent : AdEvent {
obj.put("networkname", responseInfo?.mediationAdapterClassName)
obj.put("ad_unit", adUnit)
obj.put("from", from)
var key:String? =null
if (error == null) {
obj.put("status", "1")
key="ad_pull"
} else {
obj.put("errMsg", error.toString())
obj.put("status", "2")
key="ad_pull_error"
key = "ad_pull_error"
}
EventUtils.event("ad_pull", ext = obj)
LogEx.logDebug(TAG, "ad_pull obj=$obj")
EventUtils.event(key, ext = obj)
LogEx.logDebug(TAG, "$key obj=$obj")
}
fun clickAd(responseInfo: ResponseInfo?) {
fun clickAd(responseInfo: ResponseInfo?) = scope.launch {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
......@@ -101,7 +107,7 @@ class AdmobEvent : AdEvent {
}
}
fun showAd(responseInfo: ResponseInfo?, activity: Activity? = null) {
fun showAd(responseInfo: ResponseInfo?, activity: Activity? = null) = scope.launch {
val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject()
obj.put("req_id", reqId)
......@@ -124,37 +130,75 @@ class AdmobEvent : AdEvent {
LogEx.logDebug(TAG, "ad_show $obj")
}
class EventOnPaidEventListener(private val ad: Any?) : OnPaidEventListener {
fun adShowError(adError: AdError) = scope.launch {
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("reason", adError.message)
obj.put("code", adError.code)
obj.put("from", from)
obj.put("ad_unit", "openAd")
EventUtils.event("ad_show_error", ext = obj)
LogEx.logDebug(TAG, "ad_show_error $obj")
}
}
class AdmobOnPaidEventListener(
private val ad: Any?,
private val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
) : OnPaidEventListener {
override fun onPaidEvent(adValue: AdValue) {
ad ?: return
onPaidEvent2(ad, adValue)
}
fun onPaidEvent2(ad: Any?, adValue: AdValue) {
coroutineScope.launch {
try {
val flag = eventAdRatio()
if (flag) {
SolarEngineEvent.eventSolar(ad, adValue.valueMicros)
val adRatio = AdConfigBean.adsConfigBean.adRatio
eventFireBase(adValue)
eventEvent(adValue)
}
} catch (e: Exception) {
EventUtils.event("onPaidEvent_Error")
}
}
}
fun eventAdRatio(): Boolean {
val adRatio = AdConfigBean.Companion.adsConfigBean.adRatio
val random = Random.Default.nextInt(1, 100)
if (random > adRatio) {
taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", 0f)
taichiSharedPreferencesEditor.commit()
EventUtils.event("ad_price_limit")
return
return false
}
return true
}
/**
* 上报给firebase
*/
fun eventFireBase(adValue: AdValue) {
val valueMicros = adValue.valueMicros
val currencyCode = adValue.currencyCode
val precision = adValue.precisionType
val currentImpressionRevenue = adValue.valueMicros.toDouble() / 1000000.0
val fbLogger = AppEventsLogger.newLogger(MyApplication.appContext)
fbLogger.logPurchase(BigDecimal.valueOf(valueMicros / 1000000), Currency.getInstance(currencyCode))
//sp里面的价格值
val previousTaichiTroasCache = taichiPref.getFloat("TaichiTroasCache", 0f)
val obj = JSONObject()
obj.put("valueMicros", valueMicros)
obj.put("currencyCode", currencyCode)
obj.put("precision", precision)
//当前缓存加个值
val currentTaichiTroasCache = (previousTaichiTroasCache + currentImpressionRevenue).toFloat()
Firebase.analytics.logEvent("ad_price", Bundle().apply {
putDouble("valueMicros", valueMicros / 1000000.0)
})
//价值上报阀值
val taichiAdValue = AdConfigBean.Companion.adsConfigBean.taichiAdValue / 100f
val params = Bundle()
val currentImpressionRevenue = adValue.valueMicros.toDouble() / 1000000.0
params.putDouble(FirebaseAnalytics.Param.VALUE, currentImpressionRevenue)
params.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
LogEx.logDebug("EventOnPaidEventListener", "precisionType=${adValue.precisionType}")
......@@ -166,31 +210,51 @@ class AdmobEvent : AdEvent {
else -> "Invalid"
}
params.putString("precisionType", precisionType)
Firebase.analytics.logEvent("Ad_Impression_Revenue", params)
val previousTaichiTroasCache = taichiPref.getFloat("TaichiTroasCache", 0f)
val currentTaichiTroasCache = (previousTaichiTroasCache +
currentImpressionRevenue).toFloat()
val taichiAdValue = AdConfigBean.adsConfigBean.taichiAdValue / 100f
val fbLogger = AppEventsLogger.newLogger(MyApplication.Companion.appContext)
//上报
fbLogger.logPurchase(BigDecimal.valueOf(currentImpressionRevenue), Currency.getInstance(currencyCode))
//上报ad_price
Firebase.analytics.logEvent("ad_price", Bundle().apply {
putDouble("valueMicros", valueMicros / 1000000.0)
})
//上报Ad_Impression_Revenue
Firebase.analytics.logEvent("Ad_Impression_Revenue", params)
//上报Total_Ads_Revenue_001
if (currentTaichiTroasCache >= taichiAdValue) {//如果超过0.01就触发一次tROAS taichi事件
val roasbundle = Bundle()
roasbundle.putDouble(
FirebaseAnalytics.Param.VALUE,
currentTaichiTroasCache.toDouble()
)
roasbundle.putDouble(FirebaseAnalytics.Param.VALUE, currentTaichiTroasCache.toDouble())
roasbundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
Firebase.analytics.logEvent("Total_Ads_Revenue_001", roasbundle)
taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", 0f)//重新清零,开始计算
val logger = AppEventsLogger.newLogger(MyApplication.appContext)
val parameters = Bundle()
parameters.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, "USD")
logger.logEvent("ad_value", currentTaichiTroasCache.toDouble(), parameters)
fbLogger.logEvent("ad_value", currentTaichiTroasCache.toDouble(), parameters)
} else {
taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", currentTaichiTroasCache)
}
taichiSharedPreferencesEditor.commit()
}
/**
* 上报给服务器
*/
fun eventEvent(adValue: AdValue) {
val valueMicros = adValue.valueMicros
val currencyCode = adValue.currencyCode
val precision = adValue.precisionType
val obj = JSONObject()
obj.put("valueMicros", valueMicros)
obj.put("currencyCode", currencyCode)
obj.put("precision", precision)
var key = "ad_price"
when (ad) {
......@@ -314,17 +378,6 @@ class AdmobEvent : AdEvent {
}
}
EventUtils.event(key, ext = obj)
}
}
fun adShowError(adError: AdError) {
val obj = JSONObject()
obj.put("req_id", reqId)
obj.put("reason", adError.message)
obj.put("code", adError.code)
obj.put("from", from)
obj.put("ad_unit", "openAd")
EventUtils.event("ad_show_error", ext = obj)
LogEx.logDebug(TAG, "ad_show_error $obj")
}
}
\ No newline at end of file
......@@ -40,7 +40,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>(FragmentSettingsB
binding.tvCleanedUp.text = cleanedSize.toFormatSize()
binding.tvVersion.text = "v${BuildConfig.VERSION_NAME}"
AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_medium)
AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
if (BuildConfig.DEBUG) {
binding.tvToken.visibility = View.VISIBLE
......
......@@ -65,15 +65,16 @@
<com.easy.clean.business.ads.NativeParentView
android:id="@+id/flAd"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginHorizontal="12dp"
app:layout_constraintTop_toBottomOf="@id/cl_top">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/img_ad_medium_zhanwei"
android:src="@drawable/zhanwei1"
tools:ignore="ContentDescription,ImageContrastCheck" />
</com.easy.clean.business.ads.NativeParentView>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment