GPT Dynamic Size Passbacks

The below code snippet will dynamically request ads based on the Ad slot size (e.g Ad Slot: 300×250, Ad requested 300×250), this can be used to passback when you are unsure what slot the passback will be served into.

This method supports iframes and safe frames, replace XXX with the ad unit you want to passback too and the networkid with your appropriate Google Ad Manager (GAM) ID (e.g 7864).

<!-- Dynamic AdTech.dev AdSlot Passback for Ad Unit 'adtech.dev' ### Size: [[winW,winH]] -->
<script src='https://www.googletagservices.com/tag/js/gpt.js'>
  var getAdSlotSize = function getAdSlotSize (){
  var adslotW = winW = window.innerWidth || window.document.documentElement.clientWidth || 0;
  var adslotH = winH = window.innerHeight || window.document.documentElement.clientHeight || 0;
  // Handles ad slots that are a non-standard size.
  var supportedAdW = [300, 320, 728, 970];
  function closestSize(size, arr) {
    var value = match = 0;
    
    for(var i = 0; i < arr.length; i++) {
      value = arr[i];
      if(value <= size) { match = value; }
    }
  
    return match;
  }
  var adslotW = closestSize(winW, supportedAdW);
  if (adslotW == 0){
    var supportedAdH = [0];
  } else if (adslotW == 300){
    var supportedAdH = [50, 100, 155, 250, 480, 600];
  } else if (adslotW == 320){
    var supportedAdH = [50, 100, 480];
  } else if (adslotW == 728){
    var supportedAdH = [90];
  } else if (adslotW == 970){
    var supportedAdH = [250];
  }
  var adslotH = closestSize(winH, supportedAdH);
  if (adslotW == 0 || adslotH  == 0){
    console.log("Iframe is not large enough for a Standard IAB", winW + "x" + winH ,", Matched adslot size is", adslotW + "x" + adslotH ,", cancelling passback");
    return;
  }
  console.log("iFrame is", winW + "x" + winH, ", requesting", adslotW + "x" + adslotH);
  requestAd(winW, winH, adslotW, adslotH);
  }
  var setCollapseEmptyDiv = function setCollapseEmptyDiv(){
    try {
    googletag.cmd.push(function () {
      googletag.pubads().addEventListener('slotRenderEnded', function (event) {
        if (event.isEmpty) {
          //if adcall is empty, then display none on the parent frame
          top.document.getElementById(this.frameElement.id + '__container__').parentElement.style.display = "none";
        }
      });
    })
    }catch(e){console.log(e)}
  }
  var hostDomain = function hostDomain (){
    var domain = "unknown_undefined";
    if (window.location.host == "tpc.googlesyndication.com" || window.location.host == "partner.googleadservices.com"){
      domain = "unknown_safeframe";
    } else {
      try {
        var a = window.document.createElement('a');
        a.href = location;
        var hostName = a.hostname;
        var patharray = a.hostname.split('.').reverse();
        if (patharray !== null && patharray.length > 1) {
          domain = patharray[1] + '.' + patharray[0];
          if (hostName.toLowerCase().indexOf('.co.uk') !== -1 && patharray.length > 2) {
            domain = patharray[2] + '.' + domain;
          }
        } 
      } catch(e){console.log(e)}
    }
    return domain;
  }
  var requestAd = function requestAd (winW, winH, adslotW, adslotH){
    googletag.pubads().definePassback('/21834569717/adtech.dev/', [[adslotW,adslotH]])
                    .setTargeting('originsite', [hostDomain()])
                    .setTargeting('originsz', [winW + "x" + winH])               
                    .setTargeting('passback', ['XXX'])
                    .setClickUrl('%%CLICK_URL_UNESC%%')
                    .display();
                    setCollapseEmptyDiv();
  }
  getAdSlotSize();
</script>
<!-- End -->

Use passback tags to serve ads on another publisher’s website

Passback tags can be used to serve ads from your network to another publisher’s website. In these cases, the passback tag would be trafficked by the other publisher and would return an ad from your own Ad Manager network.

Additional information about passbacks can be found here.

LEAVE A COMMENT