The "Mute This Ad" feature provides users with the ability to close or to stop seeing ads and signal which ads aren't interesting to them. Here's what the default, non-custom version looks like:
With GADNativeAd
,
you have the option to implement your own UI to enable a user to mute the native
ad. Here's how it's done:
Request custom Mute This Ad
The first step is to request the custom Mute This Ad feature using the
GADNativeMuteThisAdLoaderOptions
class.
Swift
adLoader = GADAdLoader(adUnitID: "[AD_UNIT_ID]", rootViewController: self, adTypes: [ .native ], options: [GADNativeMuteThisAdLoaderOptions()]) adLoader.delegate = self adLoader.load(GADRequest())
Objective-C
GADNativeMuteThisAdLoaderOptions *muteOptions = [GADNativeMuteThisAdLoaderOptions new]; self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:"[AD_UNIT_ID]" rootViewController:self adTypes:@[ GADAdLoaderAdTypeNative ] options:@[ muteOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]];
Check if custom Mute This Ad is available
Once the native ad is loaded, check the isCustomMuteThisAdAvailable
property on the native ad. If it's available, show your custom button.
Swift
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd){ nativeAdView.nativeAd = nativeAd if (nativeAd.isCustomMuteThisAdAvailable) { enableCustomMute(nativeAd.muteThisAdReasons) } else { hideCustomMute() } ... } func enableCustomMute(_ reasons:[GADMMuteThisAdReason]) { // TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes // the ad. } func hideCustomMute() { //TODO: Remove / hide the custom mute button from your user interface. }
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd: (GADNativeAd *)nativeAd { self.nativeAdView.nativeAd = nativeAd; if (nativeAd.isCustomMuteThisAdAvailable) { [self enableCustomMuteWithReasons:nativeAd.muteThisAdReasons]; } else { [self hideCustomMute]; } } - (void)enableCustomMuteWithReasons:(NSArray<GADMuteThisAdReason *> *)reasons { // TODO: This method should show your custom mute button and provide the list // of reasons to the interface that are to be displayed when the user mutes the // ad. } - (void)hideCustomMute { // TODO: Remove / hide the custom mute button from your user interface. }
The implementation of the custom mute interface is entirely your decision. You can place a small close button on the ad, or you can provide some other interface for the user to opt to mute the ad.
Show the Mute This Ad reasons
If custom Mute This Ad is available, there is an array of GADMuteThisAdReason
objects available in the muteThisAdReason
property on the native ad. GADMuteThisAdReason has a
reasonDescription
property that provides a displayable string.
As a best practice, we recommend displaying these reasons to the user and allowing them to select their reason for muting the ad. When the user clicks on one of the reasons, you should report the ad mute with the selected reason.
You may also choose not to display these reasons when the user clicks the close button, and report the mute action directly with no reason.
Mute the ad
Muting the ad should involve two actions:
- Report the reason for the mute to the native ad
- On your user interface, mute/hide the ad yourself in your preferred manner
Swift
func muteAdDialogDidSelect(reason: GADMuteThisAdReason) { // Report the mute action and reason to the ad. nativeAdView.nativeAd?.muteThisAd(with: reason) muteAd() } func muteAd() { // TODO: Mute / hide the ad in your preferred manner. }
Objective-C
- (void)muteAdDialogDidSelectReason:(GADMuteThisAdReason *)reason { // Report the mute action and reason to the ad. [self.nativeAdView.nativeAd muteThisAdWithReason:reason]; [self muteAd]; } - (void)muteAd { // TODO: Mute / hide the ad in your preferred manner. }
Receive confirmation of ad mute (optional)
If you want to be notified that reporting the ad mute was successful, you can
implement the
GADNativeAdDelegate
method,
nativeAdIsMuted:
.
This method will be called only if the ad was muted successfully.