Doanh thu từ quảng cáo ở cấp lượt hiển thị

Chọn nền tảng: Android iOS Unity

Khi một lượt hiển thị xảy ra, SDK quảng cáo trên thiết bị di động của Google sẽ gọi trình xử lý sự kiện có tính phí cùng với dữ liệu doanh thu liên quan. Bằng việc triển khai trình xử lý này, bạn có thể sử dụng dữ liệu này để tính toán giá trị vòng đời của người dùng hoặc chuyển dữ liệu cho các hệ thống liên quan khác.

Hướng dẫn này nhằm giúp bạn triển khai tính năng thu thập dữ liệu LTV trong ứng dụng iOS.

Điều kiện tiên quyết

Triển khai trình xử lý sự kiện có tính phí

Mỗi định dạng quảng cáo đều có một thuộc tính paidEventHandler thuộc loại GADPaidEventHandler. Trong vòng đời của một sự kiện quảng cáo, SDK quảng cáo trên thiết bị di động của Google sẽ theo dõi các sự kiện hiển thị và gọi trình xử lý bằng giá trị nhận được.

Swift

class ViewController: UIViewController, FullScreenContentDelegate {
  var rewardedAd: RewardedAd?
  func requestRewardedAd() {
    RewardedAd.load(
      with: "AD_UNIT_ID", request: Request()
    ) { (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?.extras["mediation_group_name"]
          let mediationABTestName = responseInfo?.extras["mediation_ab_test_name"]
          let mediationABTestVariant = responseInfo?.extras["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"];
    };
  ]};
}

Xác định tên nguồn quảng cáo của sự kiện tuỳ chỉnh

Đối với nguồn quảng cáo sự kiện tuỳ chỉnh, thuộc tính adSourceName sẽ cung cấp cho bạn tên nguồn quảng cáo Custom event. Nếu bạn sử dụng nhiều sự kiện tuỳ chỉnh, thì tên nguồn quảng cáo sẽ không đủ chi tiết để phân biệt giữa nhiều sự kiện tuỳ chỉnh. Để xác định vị trí của một sự kiện tuỳ chỉnh cụ thể, hãy làm theo các bước sau:

  1. Lấy thuộc tính adNetworkClassName.
  2. Đặt một tên riêng biệt cho nguồn quảng cáo.

Ví dụ sau đây đặt một tên nguồn quảng cáo riêng biệt cho một sự kiện tuỳ chỉnh:

Swift

func uniqueAdSourceName(for loadedAdNetworkResponseInfo: AdNetworkResponseInfo) -> String {
  var adSourceName: String = loadedAdNetworkResponseInfo.adSourceName ?? ""
  if adSourceName == "Custom Event" {
    if loadedAdNetworkResponseInfo.adNetworkClassName
      == "MediationExample.SampleCustomEventSwift"
    {
      adSourceName = "Sample Ad Network (Custom Event)"
    }
  }
  return adSourceName
}

Objective-C

- (NSString *)uniqueAdSourceNameForAdNetworkResponseInfo:
    (GADAdNetworkResponseInfo *)loadedAdNetworkResponseInfo {
  NSString *adSourceName = loadedAdNetworkResponseInfo.adSourceName;
  if ([adSourceName isEqualToString:@"Custom Event"]) {
    if ([loadedAdNetworkResponseInfo.adNetworkClassName isEqualToString:@"SampleCustomEvent"]) {
      adSourceName = @"Sample Ad Network (Custom Event)";
    }
  }
  return adSourceName;
}

Để biết thêm thông tin về nguồn quảng cáo giành chiến thắng, hãy xem bài viết Truy xuất thông tin về nội dung phản hồi quảng cáo.

Tích hợp với Đối tác phân bổ ứng dụng (AAP)

Để biết thông tin đầy đủ về cách chuyển dữ liệu doanh thu từ quảng cáo đến các nền tảng phân tích, hãy tham khảo hướng dẫn của đối tác:

Partner SDK
Adjust
AppsFlyer
Singular
Tenjin

Các phương pháp triển khai hay nhất

  • Thiết lập trình xử lý ngay sau khi bạn tạo hoặc có quyền sử dụng đối tượng quảng cáo và ngay trước khi hiển thị quảng cáo. Điều này đảm bảo rằng bạn không bỏ lỡ bất kỳ lệnh gọi lại sự kiện có tính phí nào.
  • Gửi thông tin về sự kiện có tính phí đến máy chủ phân tích ưu tiên của bạn ngay tại thời điểm phương thức paidEventHandler được gọi. Điều này đảm bảo rằng bạn không vô tình bỏ qua bất kỳ lệnh gọi lại nào và tránh tạo ra sự chênh lệch về dữ liệu.

GADAdValue

GADAdValue là một lớp đại diện cho giá trị tiền tệ mà bạn nhận được từ một quảng cáo, bao gồm mã đơn vị tiền tệ của giá trị và loại mã chính xác được mã hoá như sau.

GADAdValuePrecision Mô tả
GADAdValuePrecisionUnknown Giá trị quảng cáo không xác định. Hệ thống sẽ trả về kết quả này khi bạn bật tính năng pingback LTV, nhưng không có đủ dữ liệu.
GADAdValuePrecisionEstimated Giá trị quảng cáo được ước tính từ dữ liệu tổng hợp.
GADAdValuePrecisionPublisherProvided Nhà xuất bản đã cung cấp giá trị quảng cáo (chẳng hạn như CPM thủ công) trong một nhóm dàn xếp.
GADAdValuePrecisionPrecise Giá trị chính xác mà bạn phải trả cho quảng cáo này.

Lượt hiển thị thử nghiệm từ các nguồn quảng cáo đặt giá thầu

Sau khi sự kiện doanh thu từ quảng cáo ở cấp lượt hiển thị xảy ra cho một nguồn quảng cáo đặt giá thầu thông qua một yêu cầu kiểm thử, bạn chỉ nhận được các giá trị sau:

  • GADAdValuePrecisionUnknown: cho biết loại độ chính xác.
  • 0: cho biết giá trị quảng cáo.

Trước đây, bạn có thể thấy loại độ chính xác là một giá trị khác với GADAdValuePrecisionUnknown và giá trị quảng cáo lớn hơn 0.

Để biết thông tin chi tiết về cách gửi yêu cầu quảng cáo kiểm thử, hãy xem phần Bật thiết bị kiểm thử.