How to check Google publisher tag ad slot is empty or not?

user2254912 picture user2254912 · Apr 7, 2013 · Viewed 24.1k times · Source

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?

Answer

Federico J. &#193;lvarez Valero picture Federico J. Álvarez Valero · Jun 19, 2015

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;     
    }
});