Commit ed74f56a authored by maxiaoliang's avatar maxiaoliang

1

parent ae422211
package com.base.datarecovery.ads.max package com.base.datarecovery.ads.max
import android.app.Activity import android.app.Activity
import android.os.Bundle
import com.applovin.mediation.MaxAd import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdRevenueListener
import com.applovin.sdk.AppLovinSdk
import com.base.datarecovery.help.BaseApplication
import com.base.datarecovery.utils.EventUtils import com.base.datarecovery.utils.EventUtils
import com.facebook.FacebookSdk
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import org.json.JSONObject import org.json.JSONObject
object AdMaxEvent { object AdMaxEvent {
fun clickAd(nativeAd: MaxAd, adUnit: String) { fun pullAd(ad:MaxAd,adUnit: String, error: String? = null, reqId: String? = null){
// val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject() val obj = JSONObject()
// obj.put("source", response?.adSourceName) obj.put("UnitId", ad?.adUnitId)
obj.put("source", nativeAd.dspName)
obj.put("ad_unit", adUnit) obj.put("ad_unit", adUnit)
obj.put(
val credentials = mapOf( "creativeId",
"placementid" to nativeAd.placement, ad?.creativeId
"appid" to nativeAd.adUnitId,
// "pubid" to
) )
// obj.put("credentials", credentials.toString()) obj.put("req_id", reqId)
// obj.put("session_id", responseInfo?.responseId) obj.put("status", if (ad == null) "0" else "1")
// obj.put("networkname", responseInfo?.mediationAdapterClassName) obj.put("networkname", ad?.networkName)
obj.put("networkname", nativeAd.networkName) obj.put("placement", ad?.placement)
if (adUnit != "nativeAd") { obj.put("networkplacement", ad?.networkPlacement)
EventUtils.event("ad_click", ext = obj) obj.put("requestLatencyMillis", ad?.requestLatencyMillis)
obj.put("valueMicros", ad?.revenue?.times(1000000))
if (error == null) {
obj.put("status", "1")
} else { } else {
EventUtils.event("bigimage_ad_click", ext = obj) obj.put("errMsg", error)
obj.put("status", "2")
}
EventUtils.event("ad_pull", ext = obj)
} }
fun clickAd(ad: MaxAd?, adUnit: String) {
val obj = JSONObject()
obj.put("UnitId", ad?.adUnitId)
obj.put("ad_unit", adUnit)
obj.put(
"creativeId",
ad?.creativeId
)
obj.put("networkname", ad?.networkName)
obj.put("placement", ad?.placement)
obj.put("networkplacement", ad?.networkPlacement)
obj.put("requestLatencyMillis", ad?.requestLatencyMillis)
obj.put("valueMicros", ad?.revenue)
EventUtils.event("ad_click", ext = obj)
} }
fun showAd(nativeAd: MaxAd, adUnit: String, activity: Activity? = null) { fun showAd(ad: MaxAd?, adUnit: String) {
// val response = responseInfo?.adapterResponses?.getOrNull(0)
val obj = JSONObject() val obj = JSONObject()
// obj.put("source", response?.adSourceName) obj.put("UnitId", ad?.adUnitId)
obj.put("ad_unit", adUnit) obj.put("ad_unit", adUnit)
obj.put("networkname", nativeAd.networkName) obj.put(
// val credentials = mapOf( "creativeId",
// "placementid" to response?.credentials?.get("placementid"), ad?.creativeId
// "appid" to response?.credentials?.get("appid"), )
// "pubid" to response?.credentials?.get("pubid") obj.put("networkname", ad?.networkName)
// ) obj.put("placement", ad?.placement)
// obj.put("credentials", credentials.toString()) obj.put("networkplacement", ad?.networkPlacement)
// obj.put("session_id", responseInfo?.responseId) obj.put("requestLatencyMillis", ad?.requestLatencyMillis)
obj.put("from", activity?.javaClass?.simpleName) obj.put("valueMicros", ad?.revenue)
if (adUnit != "nativeAd") {
EventUtils.event("ad_show", ext = obj) EventUtils.event("ad_show", ext = obj)
}
private val taichiPref = FacebookSdk.getApplicationContext()
.getSharedPreferences("TaichiTroasCache", 0)
private val taichiSharedPreferencesEditor = taichiPref.edit()
class EventOnPaidEventListener : MaxAdRevenueListener {
override fun onAdRevenuePaid(ad: MaxAd) {
val params = Bundle()
val currentImpressionRevenue: Double = ad.revenue // In USD
val mFirebaseAnalytics = FirebaseAnalytics.getInstance(BaseApplication.context)
params.putString(FirebaseAnalytics.Param.AD_PLATFORM, "appLovin")
params.putString(FirebaseAnalytics.Param.AD_SOURCE, ad.networkName)
params.putString(FirebaseAnalytics.Param.AD_FORMAT, ad.format.getDisplayName())
params.putString(FirebaseAnalytics.Param.AD_UNIT_NAME, ad.adUnitId)
params.putDouble(FirebaseAnalytics.Param.VALUE, currentImpressionRevenue)
params.putString(FirebaseAnalytics.Param.CURRENCY, "USD")
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION, params)
mFirebaseAnalytics.logEvent("Ad_Impression_Revenue", params)
val previousTaichiTroasCache = taichiPref.getFloat("TaichiTroasCache", 0f)
val currentTaichiTroasCache = previousTaichiTroasCache + currentImpressionRevenue
if (currentTaichiTroasCache >= 0.01) {
val roasbundle = Bundle()
roasbundle.putDouble(FirebaseAnalytics.Param.VALUE, currentTaichiTroasCache)
roasbundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD")///(Required)tROAS事件必须
mFirebaseAnalytics.logEvent("Total_Ads_Revenue_001", roasbundle) // 给Taichi用
taichiSharedPreferencesEditor.putFloat("TaichiTroasCache", 0f)//重新清零,开始计算
} else { } else {
EventUtils.event("bigimage_ad_show", ext = obj) taichiSharedPreferencesEditor.putFloat(
"TaichiTroasCache",
currentTaichiTroasCache.toFloat()
)
taichiSharedPreferencesEditor.commit()
}
val obj = JSONObject()
val revenue = ad.revenue
val countryCode = AppLovinSdk.getInstance(BaseApplication.context).configuration.countryCode
val networkName = ad.networkName
val adUnitId = ad.adUnitId
val adFormat = ad.format
val placement = ad.placement
val networkPlacement = ad.networkPlacement
obj.put("valueMicros", revenue)
obj.put("currencyCode", countryCode)
obj.put("adUnitId", adUnitId)
obj.put("networkName", networkName)
obj.put("adFormat", adFormat)
obj.put("placement", placement)
obj.put("networkPlacement", networkPlacement)
EventUtils.event("ad_price", ext = obj)
} }
} }
} }
\ No newline at end of file
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