Skip to content

Commit 0a0ae29

Browse files
author
Amy Quispe
committed
Adding Custom Event Example
1 parent c6973db commit 0a0ae29

File tree

13 files changed

+364
-112
lines changed

13 files changed

+364
-112
lines changed

mediation_adapter/src/com/google/ads/mediation/sample/adapter/SampleAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void requestBannerAd(
111111

112112
// Implement a SampleAdListener and forward callbacks to mediation. The callback forwarding is
113113
// handled by SampleBannerEventFowarder.
114-
sampleAdView.setAdListener(new SampleBannerEventForwarder(listener, this));
114+
sampleAdView.setAdListener(new SampleMediationBannerEventForwarder(listener, this));
115115

116116
// Make an ad request.
117117
sampleAdView.fetchAd(createSampleRequest(mediationAdRequest));
@@ -172,7 +172,7 @@ public void requestInterstitialAd(
172172

173173
// Implement a SampleAdListener and forward callbacks to mediation. The callback forwarding is
174174
// handled by SampleInterstitialEventFowarder.
175-
sampleInterstitial.setAdListener(new SampleInterstitialEventForwarder(listener, this));
175+
sampleInterstitial.setAdListener(new SampleMediationInterstitialEventForwarder(listener, this));
176176

177177
// Make an ad request.
178178
sampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest));
@@ -183,4 +183,5 @@ public void showInterstitial() {
183183
// Show your interstitial ad.
184184
sampleInterstitial.show();
185185
}
186+
186187
}

mediation_adapter/src/com/google/ads/mediation/sample/adapter/SampleBannerEventForwarder.java renamed to mediation_adapter/src/com/google/ads/mediation/sample/adapter/SampleMediationBannerEventForwarder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* A {@link SampleAdListener} that forwards events to AdMob Mediation's
1010
* {@link MediationBannerListener}.
1111
*/
12-
public class SampleBannerEventForwarder extends SampleAdListener {
12+
public class SampleMediationBannerEventForwarder extends SampleAdListener {
1313
private MediationBannerListener mediationListener;
1414
private SampleAdapter adapter;
1515

@@ -19,7 +19,8 @@ public class SampleBannerEventForwarder extends SampleAdListener {
1919
* forwarded events.
2020
* @param adapter A {@link SampleAdapter} mediation adapter.
2121
*/
22-
public SampleBannerEventForwarder(MediationBannerListener listener, SampleAdapter adapter) {
22+
public SampleMediationBannerEventForwarder(
23+
MediationBannerListener listener, SampleAdapter adapter) {
2324
this.mediationListener = listener;
2425
this.adapter = adapter;
2526
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* A {@link SampleAdListener} that forwards events to AdMob Mediation's
1010
* {@link MediationInterstitialListener}.
1111
*/
12-
public class SampleInterstitialEventForwarder extends SampleAdListener {
12+
public class SampleMediationInterstitialEventForwarder extends SampleAdListener {
1313
private MediationInterstitialListener mediationListener;
1414
private SampleAdapter adapter;
1515

@@ -19,7 +19,7 @@ public class SampleInterstitialEventForwarder extends SampleAdListener {
1919
* forwarded events.
2020
* @param adapter A {@link SampleAdapter} mediation adapter.
2121
*/
22-
public SampleInterstitialEventForwarder(
22+
public SampleMediationInterstitialEventForwarder(
2323
MediationInterstitialListener listener, SampleAdapter adapter) {
2424
this.mediationListener = listener;
2525
this.adapter = adapter;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.google.ads.mediation.sample.customevent;
2+
3+
import com.google.ads.mediation.sample.sdk.SampleAdListener;
4+
import com.google.ads.mediation.sample.sdk.SampleAdView;
5+
import com.google.ads.mediation.sample.sdk.SampleErrorCode;
6+
import com.google.android.gms.ads.AdRequest;
7+
import com.google.android.gms.ads.mediation.customevent.CustomEventBannerListener;
8+
9+
/**
10+
* A {@link SampleAdListener} that forwards events to AdMob's
11+
* {@link CustomEventBannerListener}.
12+
*/
13+
public class SampleCustomBannerEventForwarder extends SampleAdListener {
14+
private CustomEventBannerListener bannerListener;
15+
private SampleAdView adView;
16+
17+
/**
18+
* Creates a new {@code SampleBannerEventForwarder}.
19+
* @param listener An AdMob Mediation {@link CustomEventBannerListener} that should receive
20+
* forwarded events.
21+
* @param adView A {@link SampleAdView}.
22+
*/
23+
public SampleCustomBannerEventForwarder(
24+
CustomEventBannerListener listener, SampleAdView adView) {
25+
this.bannerListener = listener;
26+
this.adView = adView;
27+
}
28+
29+
@Override
30+
public void onAdFetchSucceeded() {
31+
bannerListener.onAdLoaded(adView);
32+
}
33+
34+
@Override
35+
public void onAdFetchFailed(SampleErrorCode errorCode) {
36+
switch(errorCode) {
37+
case UNKNOWN:
38+
bannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR);
39+
break;
40+
case BAD_REQUEST:
41+
bannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST);
42+
break;
43+
case NETWORK_ERROR:
44+
bannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR);
45+
break;
46+
case NO_INVENTORY:
47+
bannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
48+
break;
49+
}
50+
}
51+
52+
@Override
53+
public void onAdFullScreen() {
54+
bannerListener.onAdClicked();
55+
bannerListener.onAdOpened();
56+
// Only call onAdLeftApplication if your ad network actually exits the developer's app.
57+
bannerListener.onAdLeftApplication();
58+
}
59+
60+
@Override
61+
public void onAdClosed() {
62+
bannerListener.onAdClosed();
63+
}
64+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.google.ads.mediation.sample.customevent;
2+
3+
import com.google.ads.mediation.sample.sdk.SampleAdRequest;
4+
import com.google.ads.mediation.sample.sdk.SampleAdSize;
5+
import com.google.ads.mediation.sample.sdk.SampleAdView;
6+
import com.google.ads.mediation.sample.sdk.SampleInterstitial;
7+
import com.google.android.gms.ads.AdSize;
8+
import com.google.android.gms.ads.mediation.MediationAdRequest;
9+
import com.google.android.gms.ads.mediation.customevent.CustomEventBanner;
10+
import com.google.android.gms.ads.mediation.customevent.CustomEventBannerListener;
11+
import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitial;
12+
import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitialListener;
13+
14+
import android.content.Context;
15+
import android.os.Bundle;
16+
17+
/**
18+
* A custom event for the Sample ad network. Custom events allow publishers to write their own
19+
* mediation adapter.
20+
*/
21+
public class SampleCustomEvent implements CustomEventBanner, CustomEventInterstitial {
22+
23+
/** The {@link SampleAdView} representing a banner ad. */
24+
private SampleAdView sampleAdView;
25+
26+
/** Represents a {@link SampleInterstitial}. */
27+
private SampleInterstitial sampleInterstitial;
28+
29+
/** The event is being destroyed. Perform any necessary cleanup here. */
30+
@Override
31+
public void onDestroy() {
32+
if (sampleAdView != null) {
33+
sampleAdView.destroy();
34+
}
35+
}
36+
37+
/**
38+
* The app is being paused. This call will only be forwarded to the adapter if the developer
39+
* notifies mediation that the app is being paused.
40+
*/
41+
@Override
42+
public void onPause() {
43+
// The sample ad network doesn't have an onPause method, so it does nothing.
44+
}
45+
46+
/**
47+
* The app is being resumed. This call will only be forwarded to the adapter if the developer
48+
* notifies mediation that the app is being resumed.
49+
*/
50+
@Override
51+
public void onResume() {
52+
// The sample ad network doesn't have an onResume method, so it does nothing.
53+
}
54+
55+
56+
@Override
57+
public void requestBannerAd(Context context,
58+
CustomEventBannerListener listener,
59+
String serverParameter,
60+
AdSize size,
61+
MediationAdRequest mediationAdRequest,
62+
Bundle customEventExtras) {
63+
/*
64+
* In this method, you should:
65+
*
66+
* 1. Create your banner view.
67+
* 2. Set your ad network's listener.
68+
* 3. Make an ad request.
69+
*
70+
* When setting your ad network's listener, don't forget to send the following callbacks:
71+
*
72+
* listener.onAdLoaded(this);
73+
* listener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_*);
74+
* listener.onAdClicked(this);
75+
* listener.onAdOpened(this);
76+
* listener.onAdLeftApplication(this);
77+
* listener.onAdClosed(this);
78+
*/
79+
80+
sampleAdView = new SampleAdView(context);
81+
82+
// Assumes that the serverParameter is the AdUnit for the Sample Network.
83+
sampleAdView.setAdUnit(serverParameter);
84+
85+
sampleAdView.setSize(new SampleAdSize(size.getWidth(), size.getHeight()));
86+
87+
// Implement a SampleAdListener and forward callbacks to mediation. The callback forwarding is
88+
// handled by SampleBannerEventFowarder.
89+
sampleAdView.setAdListener(new SampleCustomBannerEventForwarder(listener, sampleAdView));
90+
91+
// Make an ad request.
92+
sampleAdView.fetchAd(createSampleRequest(mediationAdRequest));
93+
94+
}
95+
96+
/**
97+
* Helper method to create a {@link SampleAdRequest}.
98+
* @param mediationAdRequest The mediation request with targeting information.
99+
* @return The created {@link SampleAdRequest}.
100+
*/
101+
private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) {
102+
SampleAdRequest request = new SampleAdRequest();
103+
request.setTestMode(mediationAdRequest.isTesting());
104+
request.setKeywords(mediationAdRequest.getKeywords());
105+
return request;
106+
}
107+
108+
@Override
109+
public void requestInterstitialAd(Context context,
110+
CustomEventInterstitialListener listener,
111+
String serverParameter,
112+
MediationAdRequest mediationAdRequest,
113+
Bundle customEventExtras) {
114+
/*
115+
* In this method, you should:
116+
*
117+
* 1. Create your interstitial ad.
118+
* 2. Set your ad network's listener.
119+
* 3. Make an ad request.
120+
*
121+
* When setting your ad network's listener, don't forget to send the following callbacks:
122+
*
123+
* listener.onAdLoaded(this);
124+
* listener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_*);
125+
* listener.onAdOpened(this);
126+
* listener.onAdLeftApplication(this);
127+
* listener.onAdClosed(this);
128+
*/
129+
130+
sampleInterstitial = new SampleInterstitial(context);
131+
132+
// Here we're assuming the serverParameter is the ad unit for the Sample Ad Network.
133+
sampleInterstitial.setAdUnit(serverParameter);
134+
135+
// Implement a SampleAdListener and forward callbacks to mediation.
136+
sampleInterstitial.setAdListener(new SampleCustomInterstitialEventForwarder(listener));
137+
138+
// Make an ad request.
139+
sampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest));
140+
141+
}
142+
143+
@Override
144+
public void showInterstitial() {
145+
// Show your interstitial ad.
146+
sampleInterstitial.show();
147+
}
148+
149+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.google.ads.mediation.sample.customevent;
2+
3+
import com.google.ads.mediation.sample.sdk.SampleAdListener;
4+
import com.google.ads.mediation.sample.sdk.SampleErrorCode;
5+
import com.google.android.gms.ads.AdRequest;
6+
import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitialListener;
7+
8+
/**
9+
* A {@link SampleAdListener} that forwards events to AdMob Mediation's
10+
* {@link CustomEventInterstitialListener}.
11+
*/
12+
public class SampleCustomInterstitialEventForwarder extends SampleAdListener {
13+
private CustomEventInterstitialListener interstitialListener;
14+
/**
15+
* Creates a new {@code SampleInterstitialEventForwarder}.
16+
* @param listener An AdMob Mediation {@link MediationInterstitialListener} that should receive
17+
* forwarded events.
18+
*/
19+
public SampleCustomInterstitialEventForwarder(CustomEventInterstitialListener listener) {
20+
this.interstitialListener = listener;
21+
}
22+
23+
@Override
24+
public void onAdFetchSucceeded() {
25+
interstitialListener.onAdLoaded();
26+
}
27+
28+
@Override
29+
public void onAdFetchFailed(SampleErrorCode errorCode) {
30+
switch(errorCode) {
31+
case UNKNOWN:
32+
interstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR);
33+
break;
34+
case BAD_REQUEST:
35+
interstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST);
36+
break;
37+
case NETWORK_ERROR:
38+
interstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR);
39+
break;
40+
case NO_INVENTORY:
41+
interstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
42+
break;
43+
}
44+
}
45+
46+
@Override
47+
public void onAdFullScreen() {
48+
interstitialListener.onAdOpened();
49+
// Only call onAdLeftApplication if your ad network actually exits the developer's app.
50+
interstitialListener.onAdLeftApplication();
51+
}
52+
53+
@Override
54+
public void onAdClosed() {
55+
interstitialListener.onAdClosed();
56+
}
57+
}

0 commit comments

Comments
 (0)