廣告曝光時,Google Mobile Ads SDK 會呼叫付費事件處理常式和相關聯的收益資料。只要實作這個處理常式,您就能使用該資料計算使用者的生命週期價值,或是將資料下游轉送至其他相關系統。
本指南旨在協助您在 iOS 應用程式中導入 LTV 資料擷取功能。
必要條件
- 確認您已在 AdMob UI 中啟用曝光層級廣告收益功能。
- 匯入 Google Mobile Ads SDK 9.10.0 以上版本。
- 完成入門指南。
您必須先導入至少一種廣告格式,才能獲得任何曝光層級的廣告收益:
導入付費事件處理常式
每種廣告格式都有 GADPaidEventHandler
類型的 paidEventHandler
屬性。在廣告事件的生命週期中,Google Mobile Ads SDK 會監控曝光事件,並使用衍生值叫用處理常式。
Swift
class ViewController: UIViewController, GADFullScreenContentDelegate {
var rewardedAd: GADRewardedAd?
func requestRewardedAd() {
GADRewardedAd.load(
withAdUnitID: "AD_UNIT_ID", request: GADRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
return
}
if let ad = ad {
self.rewardedAd = ad
self.rewardedAd?.paidEventHandler = { adValue in
// TODO: Send the impression-level ad revenue information to your preferred analytics
// server directly within this callback.
// Extract the impression-level ad revenue data.
let value = adValue.value
let precision = adValue.precision
let currencyCode = adValue.currencyCode
// Get the ad unit ID.
let adUnitId = ad.adUnitID
let responseInfo = ad.responseInfo
let loadedAdNetworkResponseInfo = responseInfo?.loadedAdNetworkResponseInfo
let adSourceId = loadedAdNetworkResponseInfo?.adSourceID
let adSourceInstanceId = loadedAdNetworkResponseInfo?.adSourceInstanceID
let adSourceInstanceName = loadedAdNetworkResponseInfo?.adSourceInstanceName
let adSourceName = loadedAdNetworkResponseInfo?.adSourceName
let mediationGroupName = responseInfo?.extrasDictionary["mediation_group_name"]
let mediationABTestName = responseInfo?.extrasDictionary["mediation_ab_test_name"]
let mediationABTestVariant = responseInfo?.extrasDictionary["mediation_ab_test_variant"]
}
}
}
}
}
Objective-C
@import GoogleMobileAds;
@import UIKit;
@interface ViewController ()
@property(nonatomic, strong) GADRewardedAd *rewardedAd;
@end
@implementation ViewController
- (void)requestRewardedAd {
__weak ViewController *weakSelf = self;
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:@"AD_UNIT_ID"
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
return;
}
self.rewardedAd = ad;
self.rewardedAd.paidEventHandler = ^void(GADAdValue *_Nonnull value){
ViewController *strongSelf = weakSelf;
// TODO: Send the impression-level ad revenue information to your preferred analytics
// server directly within this callback.
// Extract the impression-level ad revenue data.
NSDecimalNumber *value; = value.value;
NSString *currencyCode = value.currencyCode;
GADAdValuePrecision precision = value.precision;
// Get the ad unit ID.
NSString *adUnitId = strongSelf.rewardedAd.adUnitID;
GADAdNetworkResponseInfo *loadedAdNetworkResponseInfo =
strongSelf.rewardedAd.responseInfo.loadedAdNetworkResponseInfo;
NSString *adSourceName = loadedAdNetworkResponseInfo.adSourceName;
NSString *adSourceID = loadedAdNetworkResponseInfo.adSourceID;
NSString *adSourceInstanceName = loadedAdNetworkResponseInfo.adSourceInstanceName;
NSString *adSourceInstanceID = loadedAdNetworkResponseInfo.adSourceInstanceID;
NSDictionary<NSString *, id> *extras = strongSelf.rewardedAd.responseInfo.extrasDictionary;
NSString *mediationGroupName = extras["mediation_group_name"];
NSString *mediationABTestName = extras["mediation_ab_test_name"];
NSString *mediationABTestVariant = extras["mediation_ab_test_variant"];
};
]};
}
如要進一步瞭解勝出的廣告來源,請參閱「擷取廣告回應的相關資訊」一文。
與應用程式歸因合作夥伴 (AAP) 整合
如需將廣告收益資料轉送至數據分析平台的完整詳細資料,請參閱該合作夥伴的指南:
合作夥伴 SDK |
---|
調整 |
AppsFlyer |
Singular |
Tenjin |
導入最佳做法
- 建立或取得廣告物件的存取權後,請立即設定處理常式,並在顯示廣告前確實設定。這可確保不會遺漏任何付費事件回呼。
- 呼叫
paidEventHandler
方法時,請立即將付費事件資訊傳送至偏好的分析伺服器。這可確保您不會意外捨棄任何回呼,並避免資料出現差異。
GADAdValue
GADAdValue
類別代表廣告賺取的金錢價值,包括值的貨幣代碼及其精確度類型 (編碼如下所示)。
GADAdValuePrecision | 說明 |
---|---|
GADAdValuePrecisionUnknown
|
不明的廣告值。如果已啟用 LTV 自動參照通知,但可用資料不足,系統就會傳回這個錯誤。 |
GADAdValuePrecisionEstimated
|
根據匯總資料預估的廣告值。 |
GADAdValuePrecisionPublisherProvided
|
發布商提供的廣告值,例如中介服務群組中的手動千次曝光出價。 |
GADAdValuePrecisionPrecise
|
這則廣告的精確價值。 |