Disabling Safe Frames for Specific Prebid Partners in Google Ad Manager

When using Prebid.js in conjunction with Google Ad Manager (GAM), you may encounter situations where a demand partner’s ad units cannot render properly within a safe frame. Safe frames are iframes that provide security and protect the publisher’s content by isolating the ad from the publisher’s page. In this article, we will discuss how to disable safe frames for specific Prebid partners, ensuring that their ads are displayed correctly without affecting other demand sources.

Step 1: Identify the Prebid Partner with the Issue First, identify which Prebid partner is experiencing issues with rendering their ads within a safe frame. Communicate with the partner to understand their technical requirements and confirm whether disabling safe frames for their demand would resolve the problem.

Step 2: Modify the Prebid.js Configuration Once you have identified the Prebid partner, update the Prebid.js configuration to include a custom key-value targeting parameter that indicates whether the ad should be rendered within a safe frame or not. You can add the custom key-value pair to the bid.adserverTargeting object in the Prebid configuration.

For example:

pbjs.setConfig({
  ...
  bid.adserverTargeting: [
    {
      key: "sf",
      val: function(bidResponse) {
        if (bidResponse.bidderCode === 'PREBID_PARTNER_CODE') {
          return 'no';
        } else {
          return 'yes';
        }
      }
    }
  ],
  ...
});

Replace PREBID_PARTNER_CODE with the actual bidder code for the Prebid partner in question.

Step 3: Update the Google Ad Manager Ad Unit Tags Next, you need to update the Google Ad Manager ad unit tags on your web pages to read the custom key-value pair and act accordingly. When generating the ad unit tags in GAM, make sure to enable the “Passback Tag” option. This will generate a tag that uses JavaScript to create an iframe dynamically.

Replace the default googletag.display function with a custom function that checks the custom key-value pair and creates an iframe with the appropriate sandbox attribute:

function displayAd(adUnitId) {
  const slot = googletag.slots[adUnitId];

  googletag.cmd.push(function() {
    googletag.pubads().getSlots().forEach(function(slot) {
      if (slot.getAdUnitPath().indexOf(adUnitId) !== -1) {
        googletag.pubads().refresh([slot]);
        slot.setTargeting('sf', 'yes');
      }
    });

    googletag.display(adUnitId);

    if (slot.getTargeting('sf')[0] === 'no') {
      const iframe = document.getElementById(slot.getSlotElementId()).querySelector('iframe');
      if (iframe) {
        iframe.removeAttribute('sandbox');
      }
    }
  });
}

Now, replace the googletag.display function call in the ad unit tag code with the displayAd function:

displayAd('your-ad-unit-id');

Conclusion:
By following these steps, you can disable safe frames for specific Prebid partners, ensuring that their ads render correctly without affecting the rest of your demand sources. Keep in mind that disabling safe frames may expose your site to potential security risks from malicious ad creatives. Always work closely with your demand partners to ensure that their ad content meets your security and quality standards.

Join the growing community of publishers who have already unlocked their full advertising potential with WaardeX. Register HERE now and begin your journey to maximizing your ad revenue with a powerful and reliable partner by your side.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn