16

I am using Google publisher tags to fetch the ads.

How to check whether I am getting an ad or any empty ad for an specific ad slot. I am using the below code.

googletag.defineSlot("/1234/travel", [[300,250],[300x600]], "div-gpt-ad-123456789-0"))


 <div id="div-gpt-ad-123456789-0" style="width: 728px; height: 90px">
  <script type="text/javascript">
      googletag.cmd.push(function() {
      googletag.display("div-gpt-ad-123456789-0");
      });
    </script>
  </div>

How to check this div("div-gpt-ad-123456789-0") contains an ad or not?

user2254912
  • 169
  • 1
  • 1
  • 3

5 Answers5

20

You could do something like this:

googletag.pubads().addEventListener('slotRenderEnded', function(event) {
    if (event.slot.getSlotElementId() == "div-gpt-ad-123456789-0") {
        var containsAd = !event.isEmpty;     
    }
});
7

The GPT API provides a "SlotRenderEndedEvent" which fires when the ad is rendered ( or if nothing is returned )

https://developers.google.com/doubleclick-gpt/reference

You add the handler and check for isEmpty to determine if an ad has been served.

Also if you've enabled CollapsEmptyDivs for the ad slot, the ad div will be set to display:none by GPT so that is also a clue that nothing was returned.

dougwig
  • 368
  • 4
  • 7
6

You can use an on-screen debugging tool called the Google Publisher Console to troubleshoot delivery problems.Use "?googfc" as querystring. Reference: https://support.google.com/dfp_premium/answer/2462712?hl=en

Prasanna Jathan
  • 490
  • 6
  • 11
2

googletag.cmd.push(function () {
  googletag.pubads().addEventListener('slotRenderEnded', function (event) {
   if (event.isEmpty) {
    var id = event.slot.getSlotElementId();
    var x = document.getElementById(id);
    if (x.parentElement.classList.contains("ad-slot")) {
     x.parentElement.style.display = "none";
    }    
   }
  });
 });
Piyush Sahu
  • 682
  • 5
  • 10
1

Have you tried inspecting the div with firebug or chrome web inspector? There should be an iframe inside of the div if it is loading correctly and inside of the iframe there will be some html with the ad creative.

You can debug DFP pretty easily with the DFP console to test if your page is tagged correctly and that the ads are being delivered.

Matt Cooper
  • 7,992
  • 1
  • 28
  • 26
  • Hi, thank you How to achieve this technically. As per my requirement I need to check the ad and need to display the ad, if ad is not present I need to display default ad or image. Please suggest me how to proceed with this – user2254912 Apr 09 '13 at 17:43
  • I had the same problem so I have developed a jquery plugin to handle this requirement here: https://github.com/coop182/jquery.dfp.js If you are using jQuery on your page then my plugin can take over from the normal DFP script. It allows you to fill the ad unit div with default content which could be another ad or image but if an ad is found in DFP for that ad unit the ad will overwrite the contents of the div... Get in contact with me if you need an example. – Matt Cooper Apr 10 '13 at 13:35
  • Hi Thanks a lot. Please provide me example so that i can get some info on this issue. – user2254912 Apr 18 '13 at 18:20
  • Here is an example... well actually it is two examples. If you look at the source there are 6 ads on the page the second one will not load so the content inside of the ad div is just used instead. The 5th ad will also not load but the content of this div is loaded in by checking if the adunit is blank after it has been attempted to load it from google. http://coop182.github.io/jquery.dfp.js/dfptests/notfound.html – Matt Cooper Apr 30 '13 at 16:14