Skip to content

Commit

Permalink
On ad click support (#505)
Browse files Browse the repository at this point in the history
* Add onAdClick support.

* Android unit tests.

* Update iOS tests.

* Update version and changelog.

* Fix version.

* Fix FLTRewardedInterstitialAdTest.

* Decrease tag length.

* Add onAdClicked to banner listeners; refactor tests.

* Create parent ios class for full screen ads.

* Add back some checks to iOS tests.

* Fix typo
  • Loading branch information
jjliu15 committed Feb 8, 2022
1 parent 7fe38f5 commit 976b267
Show file tree
Hide file tree
Showing 44 changed files with 1,221 additions and 515 deletions.
7 changes: 4 additions & 3 deletions packages/google_mobile_ads/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.0.2
* Add support for [Rewarded Interstitial](https://support.google.com/admob/answer/9884467) (beta) ad format.

## 1.1.0
* Adds support for [Rewarded Interstitial](https://support.google.com/admob/answer/9884467) (beta) ad format.
* Adds support for `onAdClicked` events to all ad formats. `NativeAdListener.onNativeAdClicked` is now deprecated.
* `FullScreenContentCallback` and `AdWithViewListeners` now have an `onAdClicked` event.
## 1.0.1

* Fix for [Issue 449](https://github.com/googleads/googleads-mobile-flutter/issues/449).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void onAdImpression(int id) {
invokeOnAdEvent(arguments);
}

void onNativeAdClicked(int id) {
void onAdClicked(int id) {
Map<Object, Object> arguments = new HashMap<>();
arguments.put("adId", id);
arguments.put("eventName", "onNativeAdClicked");
arguments.put("eventName", "onAdClicked");
invokeOnAdEvent(arguments);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/** Constants used in the plugin. */
public class Constants {
/** Version request agent. Should be bumped alongside plugin versions. */
public static final String REQUEST_AGENT_PREFIX_VERSIONED = "Flutter-GMA-1.0.2";
public static final String REQUEST_AGENT_PREFIX_VERSIONED = "Flutter-GMA-1.1.0";

static final String ERROR_CODE_UNEXPECTED_AD_TYPE = "unexpected_ad_type";
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void onAdOpened() {
public void onAdImpression() {
manager.onAdImpression(adId);
}

@Override
public void onAdClicked() {
manager.onAdClicked(adId);
}
}

/**
Expand Down Expand Up @@ -87,11 +92,6 @@ class FlutterNativeAdListener extends FlutterAdListener {
public void onAdLoaded() {
// Do nothing. Loaded event is handled from FlutterNativeAdLoadedListener.
}

@Override
public void onAdClicked() {
manager.onNativeAdClicked(adId);
}
}

/** {@link OnNativeAdLoadedListener} for native ads. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.admanager.AdManagerAdView;
import com.google.android.gms.ads.admanager.AppEventListener;
import com.google.android.gms.common.internal.Preconditions;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.util.Preconditions;
import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Google Mobile Ads Plugin.
*/
class FlutterAdManagerInterstitialAd extends FlutterAd.FlutterOverlayAd {
private static final String TAG = "FlutterAdManagerInterstitialAd";
private static final String TAG = "FltGAMInterstitialAd";

@NonNull private final AdInstanceManager manager;
@NonNull private final String adUnitId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void onAdDismissedFullScreenContent() {
public void onAdImpression() {
manager.onAdImpression(adId);
}

@Override
public void onAdClicked() {
manager.onAdClicked(adId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/** A wrapper for {@link RewardedInterstitialAd}. */
class FlutterRewardedInterstitialAd extends FlutterAd.FlutterOverlayAd {
private static final String TAG = "FlutterRewardedInterstitialAd";
private static final String TAG = "FlutterRIAd";

@NonNull private final AdInstanceManager manager;
@NonNull private final String adUnitId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
listener.onAdImpression();
listener.onAdClosed();
listener.onAdOpened();
listener.onAdClicked();
return null;
}
})
Expand All @@ -137,6 +138,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClosed(eq(1));
verify(mockManager).onAdOpened(eq(1));
verify(mockManager).onAdClicked(eq(1));

// Verify that ad is correctly put into container view.
FluidAdManagerBannerAd spy = spy(fluidAd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
listener.onAdImpression();
listener.onAdClosed();
listener.onAdOpened();
listener.onAdClicked();
return null;
}
})
Expand All @@ -140,6 +141,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClosed(eq(1));
verify(mockManager).onAdOpened(eq(1));
verify(mockManager).onAdClicked(eq(1));
assertEquals(flutterBannerAd.getPlatformView().getView(), mockAdView);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
callback.onAdShowedFullScreenContent();
callback.onAdImpression();
callback.onAdDismissedFullScreenContent();
callback.onAdClicked();
return null;
}
})
Expand All @@ -176,6 +177,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(mockAdManagerAd).setAppEventListener(any(AppEventListener.class));
verify(mockManager).onAdShowedFullScreenContent(eq(1));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
verify(mockManager).onAdDismissedFullScreenContent(eq(1));
assertNull(flutterAdManagerInterstitialAd.getPlatformView());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
FullScreenContentCallback callback = fullScreenContentCallback[0];
callback.onAdShowedFullScreenContent();
callback.onAdImpression();
callback.onAdClicked();
callback.onAdDismissedFullScreenContent();
;
return null;
}
})
Expand All @@ -341,6 +341,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {

verify(mockManager).onAdShowedFullScreenContent(eq(1));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
verify(mockManager).onAdDismissedFullScreenContent(eq(1));

assertNull(flutterAppOpenAd.getPlatformView());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
AdListener listener = invocation.getArgument(0);
listener.onAdLoaded();
listener.onAdImpression();
listener.onAdClicked();
listener.onAdClosed();
listener.onAdOpened();
return null;
Expand Down Expand Up @@ -149,6 +150,7 @@ public Object answer(InvocationOnMock invocation) {
verify(mockAdView).setAdSize(adSize);
verify(mockManager).onAdLoaded(eq(1), eq(responseInfo));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
verify(mockManager).onAdClosed(eq(1));
verify(mockManager).onAdOpened(eq(1));
assertEquals(flutterBannerAd.getPlatformView().getView(), mockAdView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
FullScreenContentCallback callback = invocation.getArgument(0);
callback.onAdShowedFullScreenContent();
callback.onAdImpression();
callback.onAdClicked();
callback.onAdDismissedFullScreenContent();
return null;
}
Expand All @@ -171,6 +172,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(mockManager).onAdShowedFullScreenContent(eq(1));
verify(mockManager).onAdDismissedFullScreenContent(eq(1));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
assertNull(flutterInterstitialAd.getPlatformView());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public Object answer(InvocationOnMock invocation) {
verify(mockNativeAdFactory).createNativeAd(eq(mockNativeAd), eq(mockOptions));
verify(testManager).onAdOpened(eq(1));
verify(testManager).onAdClosed(eq(1));
verify(testManager).onNativeAdClicked(eq(1));
verify(testManager).onAdClicked(eq(1));
verify(testManager).onAdImpression(eq(1));
verify(testManager).onAdLoaded(eq(1), eq(responseInfo));
FlutterLoadAdError expectedError = new FlutterLoadAdError(loadAdError);
Expand Down Expand Up @@ -237,7 +237,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(testManager).onAdLoaded(eq(1), eq(responseInfo));
verify(testManager).onAdOpened(eq(1));
verify(testManager).onAdClosed(eq(1));
verify(testManager).onNativeAdClicked(eq(1));
verify(testManager).onAdClicked(eq(1));
verify(testManager).onAdImpression(eq(1));
FlutterLoadAdError expectedError = new FlutterLoadAdError(loadAdError);
verify(testManager).onAdFailedToLoad(eq(1), eq(expectedError));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
FullScreenContentCallback callback = invocation.getArgument(0);
callback.onAdShowedFullScreenContent();
callback.onAdImpression();
callback.onAdClicked();
callback.onAdDismissedFullScreenContent();
return null;
}
Expand Down Expand Up @@ -237,6 +238,7 @@ public boolean matches(ServerSideVerificationOptions argument) {
ArgumentMatchers.argThat(serverSideVerificationOptionsArgumentMatcher));
verify(mockManager).onAdShowedFullScreenContent(eq(1));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
verify(mockManager).onAdDismissedFullScreenContent(eq(1));
verify(mockManager).onRewardedAdUserEarnedReward(1, new FlutterRewardItem(5, "$$"));
verify(mockManager).onAdMetadataChanged(eq(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
FullScreenContentCallback callback = invocation.getArgument(0);
callback.onAdShowedFullScreenContent();
callback.onAdImpression();
callback.onAdClicked();
callback.onAdDismissedFullScreenContent();
return null;
}
Expand Down Expand Up @@ -245,6 +246,7 @@ public boolean matches(ServerSideVerificationOptions argument) {
ArgumentMatchers.argThat(serverSideVerificationOptionsArgumentMatcher));
verify(mockManager).onAdShowedFullScreenContent(eq(1));
verify(mockManager).onAdImpression(eq(1));
verify(mockManager).onAdClicked(eq(1));
verify(mockManager).onAdDismissedFullScreenContent(eq(1));
verify(mockManager).onRewardedAdUserEarnedReward(1, new FlutterRewardItem(5, "$$"));
verify(mockManager).onAdMetadataChanged(eq(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,12 @@ public NativeAdView createNativeAd(
.build();
testManager.trackAd(nativeAd, 0);

testManager.onNativeAdClicked(0);
testManager.onAdClicked(0);

final MethodCall call = getLastMethodCall();
assertEquals("onAdEvent", call.method);
//noinspection rawtypes
assertThat(call.arguments, (Matcher) hasEntry("eventName", "onNativeAdClicked"));
assertThat(call.arguments, (Matcher) hasEntry("eventName", "onAdClicked"));
//noinspection rawtypes
assertThat(call.arguments, (Matcher) hasEntry("adId", 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
9E4163BB26CCE99400220C78 /* FLTAppOpenAdTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E4163BA26CCE99300220C78 /* FLTAppOpenAdTest.m */; };
9E5A534926AA235D00B9438D /* FLTAdUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E5A534826AA235D00B9438D /* FLTAdUtil.m */; };
9E60902C27A9E7BE003D5BAB /* FLTRewardedInterstitialAdTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E60902B27A9E7BE003D5BAB /* FLTRewardedInterstitialAdTest.m */; };
9E79684A271FF40900E06158 /* FLTAdRequestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E796848271FF40900E06158 /* FLTAdRequestTest.m */; };
9E79684B271FF40900E06158 /* FLTGAMAdRequestTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E796849271FF40900E06158 /* FLTGAMAdRequestTest.m */; };
9E7B9D8F273206F800C6A6AB /* FLTAppStateNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E7B9D8E273206F800C6A6AB /* FLTAppStateNotifier.m */; };
Expand Down Expand Up @@ -100,6 +101,7 @@
9E4163BA26CCE99300220C78 /* FLTAppOpenAdTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTAppOpenAdTest.m; path = ../../../ios/Tests/FLTAppOpenAdTest.m; sourceTree = "<group>"; };
9E5A534726AA235D00B9438D /* FLTAdUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FLTAdUtil.h; path = ../../ios/Classes/FLTAdUtil.h; sourceTree = "<group>"; };
9E5A534826AA235D00B9438D /* FLTAdUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTAdUtil.m; path = ../../ios/Classes/FLTAdUtil.m; sourceTree = "<group>"; };
9E60902B27A9E7BE003D5BAB /* FLTRewardedInterstitialAdTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTRewardedInterstitialAdTest.m; path = ../../../ios/Tests/FLTRewardedInterstitialAdTest.m; sourceTree = "<group>"; };
9E79682A271D415600E06158 /* FLTMediationNetworkExtrasProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FLTMediationNetworkExtrasProvider.h; path = ../../ios/Classes/FLTMediationNetworkExtrasProvider.h; sourceTree = "<group>"; };
9E796848271FF40900E06158 /* FLTAdRequestTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTAdRequestTest.m; path = ../../../ios/Tests/FLTAdRequestTest.m; sourceTree = "<group>"; };
9E796849271FF40900E06158 /* FLTGAMAdRequestTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FLTGAMAdRequestTest.m; path = ../../../ios/Tests/FLTGAMAdRequestTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -178,6 +180,7 @@
8FB13CD3248F07B80011A72F /* google_mobile_ads_exampleTests */ = {
isa = PBXGroup;
children = (
9E60902B27A9E7BE003D5BAB /* FLTRewardedInterstitialAdTest.m */,
9E796848271FF40900E06158 /* FLTAdRequestTest.m */,
9E796849271FF40900E06158 /* FLTGAMAdRequestTest.m */,
9EDE65D526F9300000783049 /* FLTFluidGAMBannerAdTest.m */,
Expand Down Expand Up @@ -481,6 +484,7 @@
8FB13CE22498136E0011A72F /* FLTGoogleMobileAdsReaderWriterTest.m in Sources */,
9EA747542637EC7000E0B0E4 /* FLTInterstitialAdTest.m in Sources */,
9EF1F00025F8351A0017A440 /* FLTGoogleMobileAdsPluginMethodCallsTest.m in Sources */,
9E60902C27A9E7BE003D5BAB /* FLTRewardedInterstitialAdTest.m in Sources */,
9EA747532637EC7000E0B0E4 /* FLTGamInterstitialAdTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
- (void)onAppEvent:(id<FLTAd> _Nonnull)ad
name:(NSString *_Nullable)name
data:(NSString *_Nullable)data;
- (void)onNativeAdClicked:(FLTNativeAd *_Nonnull)ad;
- (void)onNativeAdImpression:(FLTNativeAd *_Nonnull)ad;
- (void)onNativeAdWillPresentScreen:(FLTNativeAd *_Nonnull)ad;
- (void)onNativeAdDidDismissScreen:(FLTNativeAd *_Nonnull)ad;
Expand All @@ -56,6 +55,7 @@
- (void)adDidDismissFullScreenContent:(id<FLTAd> _Nonnull)ad;
- (void)adWillDismissFullScreenContent:(id<FLTAd> _Nonnull)ad;
- (void)adDidRecordImpression:(id<FLTAd> _Nonnull)ad;
- (void)adDidRecordClick:(id<FLTAd> _Nonnull)ad;
- (void)didFailToPresentFullScreenContentWithError:(id<FLTAd> _Nonnull)ad
error:(NSError *_Nonnull)error;
- (void)onFluidAdHeightChanged:(id<FLTAd> _Nonnull)ad height:(CGFloat)height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ - (void)onAppEvent:(id<FLTAd> _Nonnull)ad name:(NSString *)name data:(NSString *
}];
}

- (void)onNativeAdClicked:(FLTNativeAd *_Nonnull)ad {
[self sendAdEvent:@"onNativeAdClicked" ad:ad];
}

- (void)onNativeAdImpression:(FLTNativeAd *_Nonnull)ad {
[self sendAdEvent:@"onNativeAdImpression" ad:ad];
}
Expand Down Expand Up @@ -188,6 +184,10 @@ - (void)adDidRecordImpression:(id<FLTAd> _Nonnull)ad {
[self sendAdEvent:@"adDidRecordImpression" ad:ad];
}

- (void)adDidRecordClick:(id<FLTAd> _Nonnull)ad {
[self sendAdEvent:@"adDidRecordClick" ad:ad];
}

- (void)didFailToPresentFullScreenContentWithError:(id<FLTAd> _Nonnull)ad
error:(NSError *_Nonnull)error {
[_channel invokeMethod:@"onAdEvent"
Expand Down
12 changes: 7 additions & 5 deletions packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@
adId:(NSNumber *_Nonnull)adId;
@end

@interface FLTInterstitialAd : FLTBaseAd <FLTAd, FLTAdWithoutView, GADFullScreenContentDelegate>
@interface FLTFullScreenAd : FLTBaseAd <FLTAd, FLTAdWithoutView, GADFullScreenContentDelegate>
@end

@interface FLTInterstitialAd : FLTFullScreenAd
- (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId
request:(FLTAdRequest *_Nonnull)request
rootViewController:(UIViewController *_Nonnull)rootViewController
Expand All @@ -205,7 +208,7 @@
adId:(NSNumber *_Nonnull)adId;
@end

@interface FLTRewardedAd : FLTBaseAd <FLTAd, FLTAdWithoutView, GADFullScreenContentDelegate>
@interface FLTRewardedAd : FLTFullScreenAd
- (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId
request:(FLTAdRequest *_Nonnull)request
rootViewController:(UIViewController *_Nonnull)rootViewController
Expand All @@ -215,8 +218,7 @@
- (GADRewardedAd *_Nullable)rewardedAd;
@end

@interface FLTRewardedInterstitialAd
: FLTBaseAd <FLTAd, FLTAdWithoutView, GADFullScreenContentDelegate>
@interface FLTRewardedInterstitialAd : FLTFullScreenAd
- (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId
request:(FLTAdRequest *_Nonnull)request
rootViewController:(UIViewController *_Nonnull)rootViewController
Expand All @@ -226,7 +228,7 @@
- (GADRewardedInterstitialAd *_Nullable)rewardedInterstitialAd;
@end

@interface FLTAppOpenAd : FLTBaseAd <FLTAd, FLTAdWithoutView, GADFullScreenContentDelegate>
@interface FLTAppOpenAd : FLTFullScreenAd
- (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId
request:(FLTAdRequest *_Nonnull)request
rootViewController:(UIViewController *_Nonnull)rootViewController
Expand Down

0 comments on commit 976b267

Please sign in to comment.