Implement squeezeback ads (beta)

Squeezeback

Squeezeback ads inset your app content into a smaller view and display a native ad in the surrounding space. This format lets users interact with your app while the ad remains visible. To display ads that don't cover your app content, choose this format. For more information, see Squeezeback ads using native advanced.

This guide covers requesting and displaying squeezeback ads in your app using Google Mobile Ads SDK.

Before you begin

Before you continue, do the following:

Manage the ad lifecycle across screens

By default, a squeezeback ad remains visible when the user navigates across screens. To handle ad events or hide the ad from other screens, your app must retain access to the squeezeback ad. We recommend holding the ad in an app-level singleton or shared state manager rather than in a single screen's instance variable.

Load an ad

To load a GADSqueezebackAd object, create an ad request and call the load method. The following example saves the loaded squeezeback ad, registers for ad events, and logs an error on failure:

Swift

private func loadSqueezebackAd() async {
  do {
    // Capture the SqueezebackAd reference for later use.
    squeezebackAd = try await SqueezebackAd.load(
      with: adUnitID, request: Request())
    // Set the delegate to be notified of ad events.
    squeezebackAd?.delegate = self
  } catch {
    print("Squeezeback ad failed to load: \(error.localizedDescription)")
  }
}

Replace adUnitID with your ad unit ID.

Objective-C

- (void)loadSqueezebackAd {
  GADRequest *request = [GADRequest request];

  [GADSqueezebackAd loadWithAdUnitID:kAdUnitID
                             request:request
                   completionHandler:^(GADSqueezebackAd *_Nullable ad, NSError *_Nullable error) {
                     if (error) {
                       NSLog(@"Squeezeback ad failed to load: %@", error);
                       return;
                     }
                     // Capture the GADSqueezebackAd reference for later use.
                     self.squeezebackAd = ad;
                     // Set the delegate to be notified of ad events.
                     self.squeezebackAd.delegate = self;
                   }];
}

Replace kAdUnitID with your ad unit ID.

Show the ad

To display the squeezeback ad on screen, call the show method:

Swift

private func showSqueezebackAd() {
  // Use the loaded SqueezebackAd instance.
  guard let squeezebackAd else {
    print("No ad to show.")
    return
  }
  let options = SqueezebackAdOptions()
  squeezebackAd.show(with: options)
}

Objective-C

- (void)showSqueezebackAd {
  // Use the loaded GADSqueezebackAd instance.
  if (!self.squeezebackAd) {
    NSLog(@"No ad to show.");
    return;
  }
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  [self.squeezebackAd showWithOptions:options];
}

Squeezeback ads display in portrait mode only. If you call the show method in landscape mode, the ad fails to show. Turning the device to landscape mode or presenting a full-screen ad hides an active ad. Google Mobile Ads SDK displays at most one squeezeback ad at a time per window scene.

Set the fallback background color

By default, Google Mobile Ads SDK displays a dark gray background behind the reduced app content and ad. To customize this color, specify a fallback background color in your ad options when showing the ad. The following example sets the fallback background color to dark gray:

Swift

private func createFallbackBackgroundColorOptions() -> SqueezebackAdOptions {
  let options = SqueezebackAdOptions()
  // Sets the fallback background color behind the ad and squeezed content.
  options.fallbackBackgroundColor = .darkGray
  return options
}

Objective-C

- (GADSqueezebackAdOptions *)createFallbackBackgroundColorOptions {
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  // Sets the fallback background color behind the ad and squeezed content.
  options.fallbackBackgroundColor = UIColor.darkGrayColor;
  return options;
}

Set the window scene

By default, Google Mobile Ads SDK resolves the active window scene automatically. To specify the UIWindowScene object where the squeezeback ad appears, set the windowScene property on your squeezeback ad options when showing the ad. The following example sets the window scene in your squeezeback ad options:

Swift

private func createOptions(with windowScene: UIWindowScene)
  -> SqueezebackAdOptions
{
  let options = SqueezebackAdOptions()
  // Sets the window scene where the squeezeback ad will be presented.
  options.windowScene = windowScene
  return options
}

Objective-C

- (GADSqueezebackAdOptions *)createOptionsWithWindowScene:(UIWindowScene *)windowScene {
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  // Sets the window scene where the squeezeback ad will be presented.
  options.windowScene = windowScene;
  return options;
}

Listen to ad events

To handle squeezeback ad lifecycle events, register for ad events before displaying your ad. These events report ad interactions, such as clicks and impressions. These events also report squeezeback-specific events, such as when the ad shows, fails to show, or hides.

Swift

func squeezebackAdDidShow(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad shown.")
}

func squeezebackAdDidHide(_ squeezebackAd: SqueezebackAd) {
  // Called when hide is invoked or the user taps the close button.
  // After the user taps the close button, the ad can't be shown again.
  print("Squeezeback ad hidden.")
}

func squeezebackAdDidFailToShow(
  _ squeezebackAd: SqueezebackAd, error: Error
) {
  print("Squeezeback ad failed to show: \(error.localizedDescription)")
}

func squeezebackAdDidRecordImpression(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad recorded an impression.")
}

func squeezebackAdDidRecordClick(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad recorded a click.")
}

func squeezebackAd(
  _ squeezebackAd: SqueezebackAd, didRecordPaidEventWith value: AdValue
) {
  print(
    "Squeezeback ad recorded a paid event: \(value.value) \(value.currencyCode)."
  )
}

func squeezebackAdWillPresentScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad will present screen.")
}

func squeezebackAdWillDismissScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad will dismiss screen.")
}

func squeezebackAdDidDismissScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad dismissed screen.")
}

Objective-C

- (void)squeezebackAdDidShow:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad shown.");
}

- (void)squeezebackAdDidHide:(GADSqueezebackAd *)squeezebackAd {
  // Called when hide is invoked or the user taps the close button.
  // After the user taps the close button, the ad can't be shown again.
  NSLog(@"Squeezeback ad hidden.");
}

- (void)squeezebackAdDidFailToShow:(GADSqueezebackAd *)squeezebackAd withError:(NSError *)error {
  NSLog(@"Squeezeback ad failed to show: %@", error);
}

- (void)squeezebackAdDidRecordImpression:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad recorded an impression.");
}

- (void)squeezebackAdDidRecordClick:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad recorded a click.");
}

- (void)squeezebackAd:(GADSqueezebackAd *)squeezebackAd
    didRecordPaidEventWithAdValue:(GADAdValue *)value {
  NSLog(@"Squeezeback ad recorded a paid event: %@ %@.", value.value, value.currencyCode);
}

- (void)squeezebackAdWillPresentScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad will present screen.");
}

- (void)squeezebackAdWillDismissScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad will dismiss screen.");
}

- (void)squeezebackAdDidDismissScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad dismissed screen.");
}

After the user dismisses the ad, Google Mobile Ads SDK invokes the ad hidden event, and you can't show the ad again.

Hide the ad

To remove the ad and restore your app content to full size, call the hide method. This method invokes the ad hidden event. The following example checks for a saved squeezeback ad reference and hides the ad if available:

Swift

private func hideSqueezebackAd() {
  // Use the loaded SqueezebackAd instance.
  guard let squeezebackAd else {
    print("No ad to hide.")
    return
  }
  squeezebackAd.hide()
}

Objective-C

- (void)hideSqueezebackAd {
  // Use the loaded GADSqueezebackAd instance.
  if (!self.squeezebackAd) {
    NSLog(@"No ad to hide.");
    return;
  }
  [self.squeezebackAd hide];
}

Clean up ad resources

To avoid memory leaks, retain the ad reference while the user navigates across screens. Drop the reference when the user dismisses the ad or your app finishes using the ad. The following example cleans up the ad:

Swift

private func cleanUpSqueezebackAd() {
  squeezebackAd?.hide()
  squeezebackAd = nil
}

Objective-C

- (void)cleanUpSqueezebackAd {
  [self.squeezebackAd hide];
  self.squeezebackAd = nil;
}