배너 광고 맞춤 이벤트
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
기본 요건
맞춤 이벤트 설정을 완료합니다.
배너 광고 요청
폭포식 구조 미디에이션 체인에서 맞춤 이벤트 광고 항목에 도달하면 맞춤 이벤트를 만들 때 제공한 클래스 이름에 loadBanner:adConfiguration:completionHandler:
메서드가 호출됩니다. 이 경우 이 메서드는 SampleCustomEvent
에 있고 SampleCustomEventBanner
에서 loadBanner:adConfiguration:completionHandler:
메서드를 호출합니다.
배너 광고를 요청하려면 GADMediationAdapter
및 loadBanner:adConfiguration:completionHandler:
를 구현하는 클래스를 만들거나 수정하세요. GADMediationAdapter
를 확장하는 클래스가 이미 있다면 이 클래스에 loadBanner:adConfiguration:completionHandler:
를 구현합니다. 또한 GADMediationBannerAd
를 구현할 새 클래스를 만듭니다.
맞춤 이벤트 예에서 SampleCustomEvent
는 GADMediationAdapter
인터페이스를 구현한 다음 SampleCustomEventBanner
에 위임합니다.
Swift
import GoogleMobileAds
class SampleCustomEvent: NSObject, MediationAdapter {
fileprivate var bannerAd: SampleCustomEventBanner?
...
func loadBanner(
for adConfiguration: MediationBannerAdConfiguration,
completionHandler: @escaping GADMediationBannerLoadCompletionHandler
) {
self.bannerAd = SampleCustomEventBanner()
self.bannerAd?.loadBanner(
for: adConfiguration, completionHandler: completionHandler)
}
}
Objective-C
#import "SampleCustomEvent.h"
@implementation SampleCustomEvent
...
SampleCustomEventBanner *sampleBanner;
- (void)loadBannerForAdConfiguration:
(GADMediationBannerAdConfiguration *)adConfiguration
completionHandler:(GADMediationBannerLoadCompletionHandler)
completionHandler {
sampleBanner = [[SampleCustomEventBanner alloc] init];
[sampleBanner loadBannerForAdConfiguration:adConfiguration
completionHandler:completionHandler];
}
SampleCustomEventBanner
는 다음 작업을 담당합니다.
Ad Manager UI에 정의된 선택적 매개변수는 광고 구성에 포함됩니다.
adConfiguration.credentials.settings[@"parameter"]
를 통해 매개변수에 액세스할 수 있습니다. 이 매개변수는 일반적으로 광고 네트워크 SDK가 광고 객체를 인스턴스화할 때 요구하는 광고 단위 식별자입니다.
Swift
class SampleCustomEventBanner: NSObject, MediationBannerAd {
/// The Sample Ad Network banner ad.
var bannerAd: SampleBanner?
/// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.
var delegate: MediationBannerAdEventDelegate?
/// Completion handler called after ad load
var completionHandler: GADMediationBannerLoadCompletionHandler?
func loadBanner(
for adConfiguration: MediationBannerAdConfiguration,
completionHandler: @escaping GADMediationBannerLoadCompletionHandler
) {
// Create the bannerView with the appropriate size.
let adSize = adConfiguration.adSize
bannerAd = SampleBanner(
frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height))
bannerAd?.delegate = self
bannerAd?.adUnit = adConfiguration.credentials.settings["parameter"] as? String
let adRequest = SampleAdRequest()
adRequest.testMode = adConfiguration.isTestRequest
self.completionHandler = completionHandler
bannerAd?.fetchAd(adRequest)
}
}
Objective-C
#import "SampleCustomEventBanner.h"
@interface SampleCustomEventBanner () <SampleBannerAdDelegate,
GADMediationBannerAd> {
/// The sample banner ad.
SampleBanner *_bannerAd;
/// The completion handler to call when the ad loading succeeds or fails.
GADMediationBannerLoadCompletionHandler _loadCompletionHandler;
/// The ad event delegate to forward ad rendering events to the Google Mobile
/// Ads SDK.
id <GADMediationBannerAdEventDelegate> _adEventDelegate;
}
@end
@implementation SampleCustomEventBanner
- (void)loadBannerForAdConfiguration:
(GADMediationBannerAdConfiguration *)adConfiguration
completionHandler:(GADMediationBannerLoadCompletionHandler)
completionHandler {
__block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
__block GADMediationBannerLoadCompletionHandler originalCompletionHandler =
[completionHandler copy];
_loadCompletionHandler = ^id<GADMediationBannerAdEventDelegate>(
_Nullable id<GADMediationBannerAd> ad, NSError *_Nullable error) {
// Only allow completion handler to be called once.
if (atomic_flag_test_and_set(&completionHandlerCalled)) {
return nil;
}
id<GADMediationBannerAdEventDelegate> 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"];
_bannerAd = [[SampleBanner alloc]
initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width,
adConfiguration.adSize.size.height)];
_bannerAd.adUnit = adUnit;
_bannerAd.delegate = self;
SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
adRequest.testMode = adConfiguration.isTestRequest;
[_bannerAd fetchAd:adRequest];
}
광고를 성공적으로 가져왔거나 오류가 발생하면 GADMediationBannerLoadCompletionHandler
를 호출합니다. 성공하면 오류 매개변수의 nil
값으로 GADMediationBannerAd
를 구현하는 클래스를 전달합니다. 오류가 발생하면 발생한 오류를 전달합니다.
일반적으로 이러한 메서드는 어댑터가 구현하는 서드 파티 SDK의 콜백 내에서 구현됩니다. 다음 예시의 샘플 SDK에는 관련 콜백이 있는 SampleBannerAdDelegate
가 있습니다.
Swift
func bannerDidLoad(_ banner: SampleBanner) {
if let handler = completionHandler {
delegate = handler(self, nil)
}
}
func banner(
_ banner: SampleBanner, 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)bannerDidLoad:(SampleBanner *)banner {
_adEventDelegate = _loadCompletionHandler(self, nil);
}
- (void)banner:(SampleBanner *)banner
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);
}
GADMediationBannerAd
에서는 UIView
속성을 구현해야 합니다.
Swift
var view: UIView {
return bannerAd ?? UIView()
}
Objective-C
- (nonnull UIView *)view {
return _bannerAd;
}
로드된 광고로 GADMediationBannerLoadCompletionHandler
를 호출한 후 반환된 GADMediationBannerAdEventDelegate
대리자 객체를 어댑터에서 사용하여 프레젠테이션 이벤트를 서드 파티 SDK에서 Google 모바일 광고 SDK로 전달할 수 있습니다. SampleCustomEventBanner
클래스는 SampleBannerAdDelegate
프로토콜을 구현하여 샘플 광고 네트워크의 콜백을 Google 모바일 광고 SDK에 전달합니다.
맞춤 이벤트가 이러한 콜백을 최대한 많이 전달하여 앱이 Google 모바일 광고 SDK에서 동등한 이벤트를 수신하도록 하는 것이 중요합니다.
다음은 콜백을 사용하는 방법의 예시입니다.
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) {
delegate?.reportClick()
}
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner {
[_adEventDelegate reportClick];
}
배너 광고용 맞춤 이벤트 구현이 완료되었습니다. 전체 예시는 GitHub에서 확인할 수 있습니다.
이미 지원되는 광고 네트워크에 예시를 사용하거나 예시를 수정하여 맞춤 이벤트 배너 광고를 게재할 수 있습니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-09-02(UTC)
[null,null,["최종 업데이트: 2025-09-02(UTC)"],[[["\u003cp\u003eTo request a banner ad within a custom event, create a class that implements \u003ccode\u003eGADMediationAdapter\u003c/code\u003e and the \u003ccode\u003eloadBanner:adConfiguration:completionHandler:\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eA separate class implementing \u003ccode\u003eGADMediationBannerAd\u003c/code\u003e should handle loading the ad, reporting events to the Google Mobile Ads SDK, and providing a \u003ccode\u003eUIView\u003c/code\u003e property for display.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eGADMediationBannerLoadCompletionHandler\u003c/code\u003e is called upon ad load success or failure, passing the \u003ccode\u003eGADMediationBannerAd\u003c/code\u003e instance or an error, respectively.\u003c/p\u003e\n"],["\u003cp\u003eForward relevant ad events (like clicks) from the third-party ad network SDK to the Google Mobile Ads SDK using the \u003ccode\u003eGADMediationBannerAdEventDelegate\u003c/code\u003e received from the \u003ccode\u003eGADMediationBannerLoadCompletionHandler\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the complete custom event example on GitHub for implementation details and guidance.\u003c/p\u003e\n"]]],["To request a banner ad via custom events, implement `GADMediationAdapter` and `loadBanner:adConfiguration:completionHandler:`, delegating to a class that implements `GADMediationBannerAd`. This class is responsible for loading the banner, implementing `GADMediationBannerAd`, and reporting ad event callbacks. The `loadBanner:` method within this class fetches the ad, and then invokes a completion handler (`GADMediationBannerLoadCompletionHandler`) whether loading succeeds or fails, passing the banner ad or an error, respectively. Finally, use `GADMediationBannerAdEventDelegate` to forward presentation events.\n"],null,["Prerequisites\n\nComplete the [custom events setup](/ad-manager/mobile-ads-sdk/ios/custom-events/setup).\n\nRequest a banner ad\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadBanner:adConfiguration:completionHandler:` method is called on the\nclass name you provided when [creating a custom\nevent](/ad-manager/mobile-ads-sdk/ios/custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls\nthe `loadBanner:adConfiguration:completionHandler:` method in\n`SampleCustomEventBanner`.\n\nTo request a banner ad, create or modify a class that implements\n`GADMediationAdapter` and `loadBanner:adConfiguration:completionHandler:`. If a\nclass that extends `GADMediationAdapter` already exists, implement\n`loadBanner:adConfiguration:completionHandler:` there. Additionally, create a\nnew class to implement `GADMediationBannerAd`.\n\nIn our custom event example,\n[`SampleCustomEvent`](//github.com/googleads/googleads-mobile-ios-mediation/blob/main/example/CustomEvent/SampleCustomEvent.m) implements\nthe `GADMediationAdapter` interface and then delegates to\n`SampleCustomEventBanner`. \n\nSwift \n\n```swift\nimport GoogleMobileAds\n\nclass SampleCustomEvent: NSObject, MediationAdapter {\n\n fileprivate var bannerAd: SampleCustomEventBanner?\n ...\n\n func loadBanner(\n for adConfiguration: MediationBannerAdConfiguration,\n completionHandler: @escaping GADMediationBannerLoadCompletionHandler\n ) {\n self.bannerAd = SampleCustomEventBanner()\n self.bannerAd?.loadBanner(\n for: adConfiguration, completionHandler: completionHandler)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n#import \"SampleCustomEvent.h\"\n\n@implementation SampleCustomEvent\n...\n\nSampleCustomEventBanner *sampleBanner;\n\n- (void)loadBannerForAdConfiguration:\n (GADMediationBannerAdConfiguration *)adConfiguration\n completionHandler:(GADMediationBannerLoadCompletionHandler)\n completionHandler {\n sampleBanner = [[SampleCustomEventBanner alloc] init];\n [sampleBanner loadBannerForAdConfiguration:adConfiguration\n completionHandler:completionHandler];\n}\n```\n\n`SampleCustomEventBanner` is responsible for the following tasks:\n\n- Loading the banner ad and invoking a\n [`GADMediationBannerLoadCompletionHandler`](/ad-manager/mobile-ads-sdk/ios/api/reference/Protocols/GADMediationAdapter) method once\n loading completes.\n\n- Implementing the `GADMediationBannerAd` protocol.\n\n- Receiving and reporting ad event callbacks to Google Mobile Ads SDK.\n\nThe optional parameter defined in the Ad Manager UI is\nincluded in the ad configuration.\nThe parameter can be accessed through\n`adConfiguration.credentials.settings[@\"parameter\"]`. This parameter is\ntypically an ad unit identifier that an ad network SDK requires when\ninstantiating an ad object. \n\nSwift \n\n```swift\nclass SampleCustomEventBanner: NSObject, MediationBannerAd {\n /// The Sample Ad Network banner ad.\n var bannerAd: SampleBanner?\n\n /// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.\n var delegate: MediationBannerAdEventDelegate?\n\n /// Completion handler called after ad load\n var completionHandler: GADMediationBannerLoadCompletionHandler?\n\n func loadBanner(\n for adConfiguration: MediationBannerAdConfiguration,\n completionHandler: @escaping GADMediationBannerLoadCompletionHandler\n ) {\n // Create the bannerView with the appropriate size.\n let adSize = adConfiguration.adSize\n bannerAd = SampleBanner(\n frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height))\n bannerAd?.delegate = self\n bannerAd?.adUnit = adConfiguration.credentials.settings[\"parameter\"] as? String\n let adRequest = SampleAdRequest()\n adRequest.testMode = adConfiguration.isTestRequest\n self.completionHandler = completionHandler\n bannerAd?.fetchAd(adRequest)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n#import \"SampleCustomEventBanner.h\"\n\n@interface SampleCustomEventBanner () \u003cSampleBannerAdDelegate,\n GADMediationBannerAd\u003e {\n /// The sample banner ad.\n SampleBanner *_bannerAd;\n\n /// The completion handler to call when the ad loading succeeds or fails.\n GADMediationBannerLoadCompletionHandler _loadCompletionHandler;\n\n /// The ad event delegate to forward ad rendering events to the Google Mobile\n /// Ads SDK.\n id \u003cGADMediationBannerAdEventDelegate\u003e _adEventDelegate;\n}\n@end\n\n@implementation SampleCustomEventBanner\n\n- (void)loadBannerForAdConfiguration:\n (GADMediationBannerAdConfiguration *)adConfiguration\n completionHandler:(GADMediationBannerLoadCompletionHandler)\n completionHandler {\n __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;\n __block GADMediationBannerLoadCompletionHandler originalCompletionHandler =\n [completionHandler copy];\n\n _loadCompletionHandler = ^id\u003cGADMediationBannerAdEventDelegate\u003e(\n _Nullable id\u003cGADMediationBannerAd\u003e ad, NSError *_Nullable error) {\n // Only allow completion handler to be called once.\n if (atomic_flag_test_and_set(&completionHandlerCalled)) {\n return nil;\n }\n\n id\u003cGADMediationBannerAdEventDelegate\u003e delegate = nil;\n if (originalCompletionHandler) {\n // Call original handler and hold on to its return value.\n delegate = originalCompletionHandler(ad, error);\n }\n\n // Release reference to handler. Objects retained by the handler will also\n // be released.\n originalCompletionHandler = nil;\n\n return delegate;\n };\n\n NSString *adUnit = adConfiguration.credentials.settings[@\"parameter\"];\n _bannerAd = [[SampleBanner alloc]\n initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width,\n adConfiguration.adSize.size.height)];\n _bannerAd.adUnit = adUnit;\n _bannerAd.delegate = self;\n SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];\n adRequest.testMode = adConfiguration.isTestRequest;\n [_bannerAd fetchAd:adRequest];\n}\n```\n\nWhether the ad is successfully fetched or encounters an error, you\nwould call `GADMediationBannerLoadCompletionHandler`. In the event of success,\npass through the class that implements `GADMediationBannerAd` with a `nil` value\nfor the error parameter; in the event of failure, pass through the error you\nencountered.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleBannerAdDelegate` with relevant callbacks: \n\nSwift \n\n```swift\nfunc bannerDidLoad(_ banner: SampleBanner) {\n if let handler = completionHandler {\n delegate = handler(self, nil)\n }\n}\n\nfunc banner(\n _ banner: SampleBanner, didFailToLoadAdWith errorCode: SampleErrorCode\n) {\n let error =\n SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription(\n code: SampleCustomEventErrorCodeSwift\n .SampleCustomEventErrorAdLoadFailureCallback,\n description:\n \"Sample SDK returned an ad load failure callback with error code: \\(errorCode)\"\n )\n if let handler = completionHandler {\n delegate = handler(nil, error)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n- (void)bannerDidLoad:(SampleBanner *)banner {\n _adEventDelegate = _loadCompletionHandler(self, nil);\n}\n\n- (void)banner:(SampleBanner *)banner\n didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {\n NSError *error = SampleCustomEventErrorWithCodeAndDescription(\n SampleCustomEventErrorAdLoadFailureCallback,\n [NSString stringWithFormat:@\"Sample SDK returned an ad load failure \"\n @\"callback with error code: %@\",\n errorCode]);\n _adEventDelegate = _loadCompletionHandler(nil, error);\n}\n```\n\n`GADMediationBannerAd` requires implementing a `UIView` property: \n\nSwift \n\n```swift\nvar view: UIView {\n return bannerAd ?? UIView()\n}\n```\n\nObjective-C \n\n```objective-c\n- (nonnull UIView *)view {\n return _bannerAd;\n}\n```\n\nForward mediation events to Google Mobile Ads SDK\n\nOnce you've called `GADMediationBannerLoadCompletionHandler` with a loaded ad,\nthe returned `GADMediationBannerAdEventDelegate` delegate object can then be\nused by the adapter to forward presentation events from the third-party SDK to\nGoogle Mobile Ads SDK. The `SampleCustomEventBanner` class implements the\n`SampleBannerAdDelegate` protocol to forward callbacks from the sample ad\nnetwork to Google Mobile Ads SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from Google Mobile Ads SDK.\nHere's an example of using callbacks: \n\nSwift \n\n```swift\nfunc bannerWillLeaveApplication(_ banner: SampleBanner) {\n delegate?.reportClick()\n}\n```\n\nObjective-C \n\n```objective-c\n- (void)bannerWillLeaveApplication:(SampleBanner *)banner {\n [_adEventDelegate reportClick];\n}\n```\n\nThis completes the custom events implementation for banner ads. The full example\nis available on\n[GitHub](//github.com/googleads/googleads-mobile-ios-mediation/tree/main/example/CustomEvent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event banner ads."]]