Skip to content

Commit a6fff72

Browse files
committed
feat(banner): Add onAdImpression and onAdClicked event listeners
1 parent 6dac2d6 commit a6fff72

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsBannerAdViewManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class ReactNativeGoogleMobileAdsBannerAdViewManager
5757
extends SimpleViewManager<ReactNativeAdView> {
5858
private static final String REACT_CLASS = "RNGoogleMobileAdsBannerView";
5959
private final String EVENT_AD_LOADED = "onAdLoaded";
60+
private final String EVENT_AD_IMPRESSION = "onAdImpression";
61+
private final String EVENT_AD_CLICKED = "onAdClicked";
6062
private final String EVENT_AD_FAILED_TO_LOAD = "onAdFailedToLoad";
6163
private final String EVENT_AD_OPENED = "onAdOpened";
6264
private final String EVENT_AD_CLOSED = "onAdClosed";
@@ -283,6 +285,16 @@ public void onAdOpened() {
283285
public void onAdClosed() {
284286
sendEvent(reactViewGroup, EVENT_AD_CLOSED, null);
285287
}
288+
289+
@Override
290+
public void onAdImpression {
291+
sendEvent(reactViewGroup, EVENT_AD_IMPRESSION, null);
292+
}
293+
294+
@Override
295+
public void onAdClicked {
296+
sendEvent(reactViewGroup, EVENT_AD_CLICKED, null);
297+
}
286298
});
287299
if (adView instanceof AdManagerAdView) {
288300
((AdManagerAdView) adView)

docs/displaying-ads.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ or network triggers an event:
456456
- `onAdFailedToLoad`
457457
- `onAdLeftApplication`
458458
- `onAdOpened`
459+
- `onAdImpression`
460+
- `onAdClicked`
459461
- `onPaid` &mdash; On [impression-level ad revenue](https://support.google.com/admob/answer/11322405?hl=en) events.
460462
461463
Each render of the component loads a single advert, allowing you to display multiple adverts at once. If you need to reload/change

ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerComponent.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ - (void)bannerViewWillPresentScreen:(GADBannerView *)bannerView {
184184
[self sendEvent:@"onAdOpened" payload:nil];
185185
}
186186

187+
- (void)bannerViewDidRecordImpression:(GADBannerView *)bannerView {
188+
[self sendEvent:@"onAdImpression" payload:nil];
189+
}
190+
191+
- (void)bannerViewDidRecordClick:(GADBannerView *)bannerView {
192+
[self sendEvent:@"onAdClicked" payload:nil];
193+
}
194+
187195
- (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
188196
// not in use
189197
}

ios/RNGoogleMobileAds/RNGoogleMobileAdsBannerView.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,25 @@ - (void)bannerViewWillPresentScreen:(GADBannerView *)bannerView {
225225
}
226226
}
227227

228+
- (void)bannerViewDidRecordImpression:(GADBannerView *)bannerView {
229+
if (_eventEmitter != nullptr) {
230+
std::dynamic_pointer_cast<const facebook::react::RNGoogleMobileAdsBannerViewEventEmitter>(
231+
_eventEmitter)
232+
->onNativeEvent(facebook::react::RNGoogleMobileAdsBannerViewEventEmitter::OnNativeEvent{
233+
.type = "onAdImpression"});
234+
}
235+
}
236+
237+
- (void)bannerViewDidRecordClick:(GADBannerView *)bannerView {
238+
if (_eventEmitter != nullptr) {
239+
std::dynamic_pointer_cast<const facebook::react::RNGoogleMobileAdsBannerViewEventEmitter>(
240+
_eventEmitter)
241+
->onNativeEvent(facebook::react::RNGoogleMobileAdsBannerViewEventEmitter::OnNativeEvent{
242+
.type = "onAdClicked"});
243+
}
244+
}
245+
246+
228247
- (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
229248
// not in use
230249
}

src/ads/BaseAd.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const BaseAd = React.forwardRef<
7979
width: number;
8080
height: number;
8181
}
82-
| { type: 'onAdOpened' | 'onAdClosed' }
82+
| { type: 'onAdOpened' | 'onAdClosed' | 'onAdImpression' | 'onAdClicked' }
8383
| {
8484
type: 'onAdFailedToLoad';
8585
code: string;

src/types/BannerAdProps.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ export interface BannerAdProps {
8080
*/
8181
onAdOpened?: () => void;
8282

83+
/**
84+
* Called when an impression is recorded for an ad.
85+
*/
86+
onAdImpression?: () => void;
87+
88+
/**
89+
* Called when a click is recorded for an ad
90+
*/
91+
onAdClicked?: () => void;
92+
8393
/**
8494
* Called when the user is about to return to the app after tapping on an ad.
8595
*/

0 commit comments

Comments
 (0)