prebid / Prebid.js

Setup and manage header bidding advertising partners without writing code or confusing line items. Prebid.js is open source and free.
https://docs.prebid.org
Apache License 2.0
1.33k stars 2.08k forks source link

Help needed: Calling renderAd twice #893

Closed lminarini closed 7 years ago

lminarini commented 7 years ago

Type of issue

Question

Description

Hi! Recently I've been trying a prebid implementation (here you can see the live example: http://guitadigital.com/larissa/testando-prebid-loucamente/?pbjs_debug=true), but somehow I'm getting the pbjs.renderAd call twice on a unique ad request.

Here's the main code I wrote (it has some changes/adaptations):

var PERSONARE_CDN_URL = 'https://s3.amazonaws.com/psr-commercial/header-bidding';
var PREBID_TIMEOUT = 5000;

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];

var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];

function loadAsyncScriptWithCallback(url, callback) {
  var s = document.createElement('script');

  s.type = 'text/javascript';

  if(s.readyState) {
    s.onreadystatechange = function() {
      var isLoaded = (s.readyState === 'loaded' || s.readyState === 'complete');
      if (isLoaded) {
        s.onreadystatechange = null;
        if (typeof callback === 'function') {
            callback();
        }
      }
    };
  } else {
    s.onload = function() {
      if (typeof callback === 'function') {
        callback();
      }
    };
  }

  s.async = true;
  s.src = url;

  document.getElementsByTagName('head')[0].appendChild(s);
};

function getRealValue(bidCpm) {
  var defaultCotation = 3.25;
  var hasPersonareCotation = typeof PERSONARE_HB_DOLLAR_COTATION !== 'undefined';

  if (hasPersonareCotation) {
      return bidCpm * PERSONARE_HB_DOLLAR_COTATION;
  } else {
      return bidCpm * defaultCotation;
  }
};

function sendAdserverRequest() {
  if (pbjs.adserverRequestSent) return;
  pbjs.adserverRequestSent = true;
  googletag.cmd.push(function() {
      pbjs.que.push(function() {
          pbjs.setTargetingForGPTAsync();
          googletag.pubads().refresh();
      });
  });
};

function callAdUnits () {
  googletag.cmd.push(function () {
    googletag.defineSlot('/8948849/opecTeste_SB', [728, 90], 'psr-ads-SR-qmfc1ee').addService(googletag.pubads());

    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
  });

    window.onload = function(){
      googletag.cmd.push(function() { googletag.display('psr-ads-SR-qmfc1ee'); });
    }
  };

loadAsyncScriptWithCallback('//www.googletagservices.com/tag/js/gpt.js', function(){

  loadAsyncScriptWithCallback(PERSONARE_CDN_URL + '/dollar-quotation.js');

  loadAsyncScriptWithCallback(PERSONARE_CDN_URL + '/prebid/prebid.js',function(){

    pbjs.bidderSettings = {
      appnexus: {
        bidCpmAdjustment : getRealValue
      },
      aol: {
        bidCpmAdjustment : getRealValue
      },
      rubicon: {
        bidCpmAdjustment : getRealValue
      }
    };

    loadAsyncScriptWithCallback('http://guitadigital.com/larissa/wp-content/uploads/2016/12/setorreciclagem.js', function(){

      googletag.cmd.push(function() {
        googletag.pubads().disableInitialLoad();
      });

      pbjs.que.push(function() {
          pbjs.addAdUnits(adUnits);
          pbjs.requestBids({
              bidsBackHandler: sendAdserverRequest
          });
      });
    });

  });

});

this code is kept in a .js external file and called through the following script tag in the of the site

 <script type="text/javascript" src="http://guitadigital.com/larissa/wp-content/uploads/2016/12/pbjs_main.js" onload="callAdUnits()" async></script>

the key objective here is to keep the less code possible in our header and to display the ad, we call an empty div tag matching the ID "psr-ads-SR-qmfc1ee"

<div id='psr-ads-SR-qmfc1ee'></div>

Here's the console log: image

I was hoping that someone here could help me with this. Thanks.

ialex commented 7 years ago

Seems that the problem is that the site americanexpress.com doesnt allow to be embeded in an iframe use another domain to test.

SRC: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

protonate commented 7 years ago

Closing this as answered, please reopen if additional info.