展示机会层级的广告收入

发生展示时,Google 移动广告 SDK 会使用相关的收入数据调用付费事件处理脚本。通过实现此处理程序,您可以使用此数据来计算用户的生命周期价值,也可以将其传送到下游的其他相关系统。

本指南旨在帮助您在 iOS 应用中实现获取生命周期价值数据的功能。

前提条件

实现付费事件处理脚本

每种广告格式都有一个类型为 GADPaidEventHandlerpaidEventHandler 属性。在广告事件的生命周期内,Google 移动广告 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
单数
天津

植入方面的最佳做法

  • 在创建或有权访问广告对象后立即设置处理程序,且一定要在展示广告之前进行设置。这样可以确保您不会错过任何付费事件回调。
  • 在调用 paidEventHandler 方法时,立即将付费事件信息发送到您的首选分析服务器。这样可以确保您不会意外丢掉任何回调,并能避免数据差异。

GADAdValue

GADAdValue 是一个类,用于表示展示某个广告可赚取的货币价值,它包括价值的货币代码及精度类型,代码说明如下。

GADAdValuePrecision 说明
GADAdValuePrecisionUnknown 广告价值未知。如果启用了生命周期价值回 ping,但可用数据不足,就会返回此错误。
GADAdValuePrecisionEstimated 广告价值是根据汇总数据估算的。
GADAdValuePrecisionPublisherProvided 广告价值是由发布商提供的,例如中介组中的人工每千次展示费用。
GADAdValuePrecisionPrecise 广告价值是为广告支付的费用的精准值。