Geçiş reklamları özel etkinlikleri

Ön koşullar

Özel etkinlik kurulumunu tamamlayın.

Geçiş reklamı isteğinde bulunma

Şelale uyumlulaştırma zincirinde özel etkinlik satır öğesine ulaşıldığında, loadInterstitial:adConfiguration:completionHandler: yöntemi, özel etkinlik oluştururken sağladığınız sınıf adında çağrılır. Bu durumda, söz konusu yöntem SampleCustomEvent içinde yer alır ve SampleCustomEventInterstitial içinde loadInterstitial:adConfiguration:completionHandler: yöntemini çağırır.

Geçiş reklamı istemek için GADMediationAdapter ve loadInterstitial:adConfiguration:completionHandler: uygulayan bir sınıf oluşturun veya değiştirin. GADMediationAdapter öğesini genişleten bir sınıf zaten varsa loadInterstitial:adConfiguration:completionHandler: öğesini orada uygulayın. Ayrıca, GADMediationInterstitialAd'yı uygulamak için yeni bir sınıf oluşturun.

Özel etkinlik örneğimizde, SampleCustomEvent, GADMediationAdapter arayüzünü uygular ve ardından SampleCustomEventInterstitial'e devreder.

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, MediationAdapter {

  fileprivate var interstitialAd: SampleCustomEventInterstitial?
  ...

  func loadInterstitial(
    for adConfiguration: MediationInterstitialAdConfiguration,
    completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler
  ) {
    self.interstitialAd = SampleCustomEventInterstitial()
    self.interstitialAd?.loadInterstitial(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent

SampleCustomEventInterstitial *sampleInterstitial;

- (void)loadInterstitialForAdConfiguration:
            (GADMediationInterstitialAdConfiguration *)adConfiguration
                         completionHandler:
                             (GADMediationInterstitialLoadCompletionHandler)
                                 completionHandler {
  sampleInterstitial = [[SampleCustomEventInterstitial alloc] init];
  [sampleInterstitial loadInterstitialForAdConfiguration:adConfiguration
                                       completionHandler:completionHandler];
}

SampleCustomEventInterstitial aşağıdaki görevlerden sorumludur:

  • Geçiş reklamını yükleme ve yükleme tamamlandıktan sonra bir GADMediationInterstitialAdLoadCompletionHandler yöntemini çağırma.

  • GADMediationInterstitialAd protokolünü uygulama.

  • Reklam etkinliği geri çağırmalarını Google Mobile Ads SDK'sına alma ve bildirme.

kullanıcı arayüzünde tanımlanan isteğe bağlı parametre, reklam yapılandırmasına dahil edilir. Parametreye adConfiguration.credentials.settings[@"parameter"] üzerinden erişilebilir. Bu parametre, genellikle bir reklam ağı SDK'sının reklam nesnesi oluşturulurken ihtiyaç duyduğu bir reklam birimi tanımlayıcısıdır.

Swift

import GoogleMobileAds

class SampleCustomEventInterstitial: NSObject, MediationInterstitialAd {
  /// The Sample Ad Network interstitial ad.
  var interstitial: SampleInterstitial?

  /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK.
  var delegate: MediationInterstitialAdEventDelegate?

  var completionHandler: GADMediationInterstitialLoadCompletionHandler?

  func loadInterstitial(
    for adConfiguration: MediationInterstitialAdConfiguration,
    completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler
  ) {
    interstitial = SampleInterstitial.init(
      adUnitID: adConfiguration.credentials.settings["parameter"] as? String)
    interstitial?.delegate = self
    let adRequest = SampleAdRequest()
    adRequest.testMode = adConfiguration.isTestRequest
    self.completionHandler = completionHandler
    interstitial?.fetchAd(adRequest)
  }

  func present(from viewController: UIViewController) {
    if let interstitial = interstitial, interstitial.isInterstitialLoaded {
      interstitial.show()
    }
  }
}

Objective-C

#import "SampleCustomEventInterstitial.h"

@interface SampleCustomEventInterstitial () <SampleInterstitialAdDelegate,
                                             GADMediationInterstitialAd> {
  /// The sample interstitial ad.
  SampleInterstitial *_interstitialAd;

  /// The completion handler to call when the ad loading succeeds or fails.
  GADMediationInterstitialLoadCompletionHandler _loadCompletionHandler;

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  id <GADMediationInterstitialAdEventDelegate> _adEventDelegate;
}
@end

- (void)loadInterstitialForAdConfiguration:
            (GADMediationInterstitialAdConfiguration *)adConfiguration
                         completionHandler:
                             (GADMediationInterstitialLoadCompletionHandler)
                                 completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationInterstitialLoadCompletionHandler
      originalCompletionHandler = [completionHandler copy];

  _loadCompletionHandler = ^id<GADMediationInterstitialAdEventDelegate>(
      _Nullable id<GADMediationInterstitialAd> ad, NSError *_Nullable error) {
    // Only allow completion handler to be called once.
    if (atomic_flag_test_and_set(&completionHandlerCalled)) {
      return nil;
    }

    id<GADMediationInterstitialAdEventDelegate> delegate = nil;
    if (originalCompletionHandler) {
      // Call original handler and hold on to its return value.
      delegate = originalCompletionHandler(ad, error);
    }

    // Release reference to handler. Objects retained by the handler will also
    // be released.
    originalCompletionHandler = nil;

    return delegate;
  };

  NSString *adUnit = adConfiguration.credentials.settings[@"parameter"];
  _interstitialAd = [[SampleInterstitial alloc] initWithAdUnitID:adUnit];
  _interstitialAd.delegate = self;
  SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
  adRequest.testMode = adConfiguration.isTestRequest;
  [_interstitialAd fetchAd:adRequest];
}

Reklamın başarıyla getirilip getirilmediğine veya bir hatayla karşılaşıp karşılaşmadığına bakılmaksızın GADMediationInterstitialLoadCompletionHandler çağrısı yaparsınız. Başarı durumunda, hata parametresi için nil değeriyle GADMediationInterstitialAd uygulayan sınıfı iletin. Başarısızlık durumunda ise karşılaştığınız hatayı iletin.

Genellikle bu yöntemler, bağdaştırıcınızın uyguladığı üçüncü taraf SDK'sından gelen geri çağırmaların içinde uygulanır. Bu örnekte, Sample SDK'sında alakalı geri çağırmalar içeren bir SampleInterstitialAdDelegate var:

Swift

func interstitialDidLoad(_ interstitial: SampleInterstitial) {
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func interstitial(
  _ interstitial: SampleInterstitial,
  didFailToLoadAdWith errorCode: SampleErrorCode
) {
  let error =
    SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription(
      code: SampleCustomEventErrorCodeSwift
        .SampleCustomEventErrorAdLoadFailureCallback,
      description:
        "Sample SDK returned an ad load failure callback with error code: \(errorCode)"
    )
  if let handler = completionHandler {
    delegate = handler(nil, error)
  }
}

Objective-C

- (void)interstitialDidLoad:(SampleInterstitial *)interstitial {
  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)interstitial:(SampleInterstitial *)interstitial
    didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {
  NSError *error = SampleCustomEventErrorWithCodeAndDescription(
      SampleCustomEventErrorAdLoadFailureCallback,
      [NSString stringWithFormat:@"Sample SDK returned an ad load failure "
                                 @"callback with error code: %@",
                                 errorCode]);
  _adEventDelegate = _loadCompletionHandler(nil, error);
}

GADMediationInterstitialAd, reklamın gösterilmesi için present yönteminin uygulanmasını gerektirir:

Swift

func present(from viewController: UIViewController) {
  if let interstitial = interstitial, interstitial.isInterstitialLoaded {
    interstitial.show()
  }
}

Objective-C

- (void)presentFromViewController:(UIViewController *)viewController {
  if ([_interstitialAd isInterstitialLoaded]) {
    [_interstitialAd show];
  } else {
    NSError *error = SampleCustomEventErrorWithCodeAndDescription(
        SampleCustomEventErrorAdNotLoaded,
        [NSString stringWithFormat:@"The interstitial ad failed to present "
                                   @"because the ad was not loaded."]);
    [_adEventDelegate didFailToPresentWithError:error]
  }
}

Uyumlulaştırma etkinliklerini Google Mobile Ads SDK'sına yönlendirme

Yüklü bir reklamla GADMediationInterstitialLoadCompletionHandler işlevini çağırdıktan sonra döndürülen GADMediationInterstitialAdEventDelegate temsilci nesnesi, üçüncü taraf SDK'sından gelen sunum etkinliklerini Google Mobile Ads SDK'sına iletmek için bağdaştırıcı tarafından kullanılabilir. SampleCustomEventInterstitial sınıfı, örnek reklam ağından gelen geri çağırmaları Google Mobile Ads SDK'sına iletmek için SampleInterstitialAdDelegate protokolünü uygular.

Uygulamanızın Google Mobile Ads SDK'sından bu eşdeğer etkinlikleri alabilmesi için özel etkinliğinizin bu geri çağırmaların mümkün olduğunca çoğunu iletmesi önemlidir. Geri çağırmaları kullanma örneğini aşağıda bulabilirsiniz:

Swift

func interstitialWillPresentScreen(_ interstitial: SampleInterstitial) {
  delegate?.willPresentFullScreenView()
  delegate?.reportImpression()
}

func interstitialWillDismissScreen(_ interstitial: SampleInterstitial) {
  delegate?.willDismissFullScreenView()
}

func interstitialDidDismissScreen(_ interstitial: SampleInterstitial) {
  delegate?.didDismissFullScreenView()
}

func interstitialWillLeaveApplication(_ interstitial: SampleInterstitial) {
  delegate?.reportClick()
}

Objective-C

- (void)interstitialWillPresentScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate willPresentFullScreenView];
  [_adEventDelegate reportImpression];
}

- (void)interstitialWillDismissScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate willDismissFullScreenView];
}

- (void)interstitialDidDismissScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate didDismissFullScreenView];
}

- (void)interstitialWillLeaveApplication:(SampleInterstitial *)interstitial {
  [_adEventDelegate reportClick];
}

Bu işlem, geçiş reklamları için özel etkinlik uygulamasını tamamlar. Tam örneği GitHub'da bulabilirsiniz. Bu özelliği, halihazırda desteklenen bir reklam ağıyla kullanabilir veya özel etkinlik geçiş reklamları gösterecek şekilde değiştirebilirsiniz.