native 广告行为事件上报

This commit is contained in:
juncong lee 2025-09-03 13:42:30 +08:00
parent a485b5eaa2
commit 251e0c7cce
2 changed files with 46 additions and 16 deletions

View File

@ -38,23 +38,19 @@ namespace WZ
public void Initialize() public void Initialize()
{ {
if (_initialized) return; if (_initialized) return;
_initialized = true;
MobileAds.RaiseAdEventsOnUnityMainThread = true; MobileAds.RaiseAdEventsOnUnityMainThread = true;
//
MobileAds.Initialize(initStatus => MobileAds.Initialize(initStatus =>
{ {
LoggerUtils.Debug("[Admob] init success"); _initialized = true;
});
if (_bannerAdUnits.Count > 0) LoadBanner(); if (_bannerAdUnits.Count > 0) LoadBanner();
if (_interstitialAdUnits.Count > 0) LoadInterstitial(); if (_interstitialAdUnits.Count > 0) LoadInterstitial();
if (_rewardedAdUnits.Count > 0) LoadRewarded(); if (_rewardedAdUnits.Count > 0) LoadRewarded();
if (_splashAdUnits.Count > 0) AdsSplashManager.Instance.InitSplash(); if (_splashAdUnits.Count > 0) AdsSplashManager.Instance.InitSplash();
if (_nativeAdUnits.Count > 0)LoadNative();
LoggerUtils.Debug("[Admob] init success");
});
if (_nativeAdUnits.Count > 0)
{
LoadNative();
}
} }
public void RefreshAdsData() public void RefreshAdsData()
@ -177,7 +173,7 @@ namespace WZ
} }
public bool IsInterstitialAvailable() public bool IsInterstitialAvailable()
{; {
return _admobInterstitialAdManager.GetAvailableAdUnits().Count > 0; return _admobInterstitialAdManager.GetAvailableAdUnits().Count > 0;
} }
@ -310,6 +306,7 @@ namespace WZ
_admobNativeAdManager.InitializeAdUnits( _admobNativeAdManager.InitializeAdUnits(
_nativeAdUnits _nativeAdUnits
); );
AdsActionEvents.TrackAdStartLoad(Platfrom, "", "", AdsType.Native);
} }
public bool IsNativeAvailable(string adUnitId) public bool IsNativeAvailable(string adUnitId)
@ -319,6 +316,7 @@ namespace WZ
public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position) public void DisplayNative(string _adPos, string adUnitId, NativeAdPosition position)
{ {
_admobNativeAdManager.ShowAd(position, adUnitId); _admobNativeAdManager.ShowAd(position, adUnitId);
} }

View File

@ -13,6 +13,8 @@ namespace WZ
private Dictionary<string, double> _adRevenueCache = new Dictionary<string, double>(); private Dictionary<string, double> _adRevenueCache = new Dictionary<string, double>();
private Dictionary<string, int> _retryCounters = new Dictionary<string, int>(); private Dictionary<string, int> _retryCounters = new Dictionary<string, int>();
private Dictionary<string,float> _adStartLoadTimes = new Dictionary<string, float>();
public void InitializeAdUnits(List<string> adUnitIds) public void InitializeAdUnits(List<string> adUnitIds)
{ {
foreach (var adUnitId in adUnitIds) foreach (var adUnitId in adUnitIds)
@ -39,6 +41,7 @@ namespace WZ
NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) => NativeOverlayAd.Load(adUnitId, new AdRequest(), new NativeAdOptions(), (NativeOverlayAd ad, LoadAdError error) =>
{ {
_adStartLoadTimes[adUnitId] = Time.realtimeSinceStartup;
LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end. {ad} error {error}"); LoggerUtils.Debug($"[Admob] Native Ad unit {adUnitId} load end. {ad} error {error}");
if (error != null || ad == null) if (error != null || ad == null)
{ {
@ -46,13 +49,22 @@ namespace WZ
{ {
_retryCounters[adUnitId]++; _retryCounters[adUnitId]++;
} }
AdsActionEvents.TrackAdFailToLoad(PlatformType.Admob,
ad.GetResponseInfo().GetLoadedAdapterResponseInfo().AdSourceName,
adUnitId,
AdsType.Native,
Time.realtimeSinceStartup - _adStartLoadTimes[adUnitId],error.GetMessage());
var retryDelay = Math.Pow(2, Math.Min(6, _retryCounters[adUnitId])); var retryDelay = Math.Pow(2, Math.Min(6, _retryCounters[adUnitId]));
TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId); }); TimerUtils.Instance.DelayExecute((float)retryDelay, () => { LoadAd(adUnitId); });
LoggerUtils.Debug("[Admob] Native ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay); LoggerUtils.Debug("[Admob] Native ad failed to load an ad with error : " + error + " \n retryDelay :" + retryDelay);
return; return;
} }
AdsActionEvents.TrackAdLoaded(PlatformType.Admob,
ad.GetResponseInfo().GetLoadedAdapterResponseInfo().AdSourceName,
adUnitId,
AdsType.Native,
Time.realtimeSinceStartup - _adStartLoadTimes[adUnitId]);
_retryCounters[adUnitId] = 0; _retryCounters[adUnitId] = 0;
var nativeEcpm = AdmobUtils.GetNativeEcpm(ad); var nativeEcpm = AdmobUtils.GetNativeEcpm(ad);
@ -72,11 +84,27 @@ namespace WZ
ad.OnAdImpressionRecorded += () => { LoggerUtils.Debug("[Admob] Native ad recorded an impression."); }; ad.OnAdImpressionRecorded += () => { LoggerUtils.Debug("[Admob] Native ad recorded an impression."); };
ad.OnAdClicked += () => { LoggerUtils.Debug("[Admob] Native ad was clicked."); }; ad.OnAdClicked += () =>
{
AdsActionEvents.TrackAdClicked(PlatformType.Admob,
ad.GetResponseInfo().GetLoadedAdapterResponseInfo().AdSourceName,
adUnitId,
AdsType.Native,
"",
AdmobUtils.GetNativeEcpm(ad));
LoggerUtils.Debug("[Admob] Native ad was clicked.");
};
ad.OnAdFullScreenContentOpened += () => { LoggerUtils.Debug("[Admob] Native ad full screen content opened."); }; ad.OnAdFullScreenContentOpened += () => { LoggerUtils.Debug("[Admob] Native ad full screen content opened."); };
ad.OnAdFullScreenContentClosed += () => { LoggerUtils.Debug("[Admob] Native ad full screen content closed."); }; ad.OnAdFullScreenContentClosed += () => {
AdsActionEvents.TrackAdClicked(PlatformType.Admob,
ad.GetResponseInfo().GetLoadedAdapterResponseInfo().AdSourceName,
adUnitId,
AdsType.Native,
"",
AdmobUtils.GetNativeEcpm(ad));
LoggerUtils.Debug("[Admob] Native ad full screen content closed."); };
}); });
} }
@ -124,6 +152,10 @@ namespace WZ
} }
private float GetLoadedTime(string adUnitId)
{
return _adStartLoadTimes.TryGetValue(adUnitId, out var time)? time : 0;
}
// 检查特定广告位是否可用 // 检查特定广告位是否可用
public bool IsAdAvailable(string adUnitId) public bool IsAdAvailable(string adUnitId)
{ {