Prebid.js icon indicating copy to clipboard operation
Prebid.js copied to clipboard

Load an adunit which was not requested at initial load

Open bboigienman opened this issue 9 years ago • 2 comments

Type of issue

Question

Description

Hello,

I've a question on prebid implementation which is not covered by the Individual Ad Unit Refresh Example with DFP GPT. On my page, I've 3 adunits. 2 of them are loaded from the beginning and the third one is only loaded on user scroll.

I'm wondering if my code is correct:

<script>
		var googletag = googletag || {};
		googletag.cmd = googletag.cmd || [];

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

		pbjs.que.push(function() {
			pbjs.addAdUnits(adUnits);
			pbjs.requestBids({
				timeout: PREBID_TIMEOUT,
				adUnitCodes: ['adslot1', 'adslot2'],
				bidsBackHandler: sendAdserverRequest
		  });
		});
		
		function refreshBids(adUnitCodes, gptAdSlots) {
			pbjs.que.push(function() {
			  pbjs.requestBids({
				timeout: PREBID_TIMEOUT,
				adUnitCodes: adUnitCodes,
				bidsBackHandler: function() {
				  pbjs.setTargetingForGPTAsync(adUnitCodes);
				  googletag.pubads().refresh(gptAdSlots); 
				}
			  });
			});
		}
		
		function refreshNewSlot(){
			refreshBids(['adslot3'], [gptadslots['adslot3']])
		}
		
		function sendAdserverRequest() {
			if (pbjs.adserverRequestSent) return;
			pbjs.adserverRequestSent = true;
			googletag.cmd.push(function() {
				pbjs.setTargetingForGPTAsync(['adslot1', 'adslot2']);
				googletag.pubads().refresh([gptadslots['adslot1'], gptadslots['adslot2']]);
			});
		}

		setTimeout(function() {sendAdserverRequest();}, PREBID_TIMEOUT);
	}
</script>

<script type='text/javascript'>
	(function() {
		var gads = document.createElement('script');
		gads.async = true;
		gads.type = 'text/javascript';
		var useSSL = 'https:' == document.location.protocol;
		gads.src = (useSSL ? 'https:' : 'http:') +
		'//www.googletagservices.com/tag/js/gpt.js';
		var node = document.getElementsByTagName('script')[0];
		node.parentNode.insertBefore(gads, node);
	})();
</script>

<script>
	gptadslots = {}
	googletag.cmd.push(function() {				
		gptadslots['adslot1'] = googletag.defineSlot('/xxx/adslot1', [300, 250], 'adslot1').addService(googletag.pubads());
		gptadslots['adslot2'] = googletag.defineSlot('/xxx/adslot2', [300, 250], 'adslot2').addService(googletag.pubads());
		gptadslots['adslot3'] = googletag.defineSlot('/xxx/adslot3', [300, 250], 'adslot3').addService(googletag.pubads());
		
		googletag.pubads().enableSingleRequest();
		googletag.enableServices();
	});
</script>

Especially in the initial load, should I use 'timeout: PREBID_TIMEOUT' in pbjs.requestBids method and how it could conflict with the setTimeout(function() {sendAdserverRequest();}, PREBID_TIMEOUT);

Thank you

bboigienman avatar Oct 27 '17 09:10 bboigienman

Hi @bboigienman!

I have pretty much similar situation - and yes, your code seems to be correct. It's strange there's no such example within Prebid.js docs, as it's kind of common way of implementing lazy ads. There are 2 possible pitfalls with this:

  • Your code is slot-level dependent, so I would even think about making it more abstract (see below).
  • setTimeout() call is required ONLY if prebid.js library fails to load completely; theoretically, requestBids() function does exactly the same. I'm not using setTimeout() fallback, as if it's executed after all - which means bidding didn't happen at all - which meeeeeans something went completely wrong. But that's my case, your DFP may have fallback house ads to deliver.

The only thing which I'm unclear is global bidderTimeout variable, and manual timeout argument in requestBids() function - do I need a latter one if global is set. For a sake of compliance, I'm passing both.

See the code I'm using, you may want to grab some pieces for yourself:

// timeout to control how long bidders have to respond.
var PREBID_TIMEOUT = 2000;

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

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

var sheology_pbjs = sheology_pbjs || {};
sheology_pbjs.slots = sheology_pbjs.slots || [];
sheology_pbjs.slotsQueue = sheology_pbjs.slotsQueue || [];

pbjs.que.push( function()
{
	sheology_pbjs.slots = [
		// your full slots configuration here
	];

	// Set main configuration for request
	pbjs.setConfig({
		bidderTimeout: PREBID_TIMEOUT
	});

	pbjs.addAdUnits( sheology_pbjs.slots );

	/**
	 * Runs Prebid.js auction for selected sltos
	 * @param {Array} slots
	 * @private
	 */
	function _runAuction( slots )
	{
		var slotCodes = slots
				.map( function( slot ) {
					return slot.getSlotElementId() || '';
				} ).filter( function( id ) {
					return id;
				} );
		
		if ( slotCodes.length <= 0 )
			return;

		pbjs.requestBids({
			adUnitCodes: slotCodes,
			bidsBackHandler: function( bidResponses )
			{
				googletag.cmd.push( function()
				{
					pbjs.setTargetingForGPTAsync( slotCodes );
					googletag.pubads().refresh( slots );
				});
			},
			timeout: PREBID_TIMEOUT
		});
	}

	// Process queue, if any - because of async executing, store it to temporary value
	var slots = sheology_pbjs.slotsQueue.slice();
	sheology_pbjs.slotsQueue = [];

	// Overwrite function
	sheology_pbjs.slotsQueue.push = function()
	{
		_runAuction( [].slice.call(arguments) );
	};

	// Run auction on current queue
	if ( slots.length > 0 )
	{
		_runAuction( slots );
	}
} );

// USAGE

// Load slots on load, or immediatelly when it's available
sheology_pbjs.slotsQueue.push( gptadslots['adslot1'], gptadslots['adslot2'] );

// Load slots on request. Note - if Prebid.js has not finished loading yet, it will combine request by loading 3 ad units together!
function refreshNewSlot()
{
	sheology_pbjs.slotsQueue.push( gptadslots['adslot3'] );
}

YOzaz avatar Nov 01 '17 08:11 YOzaz

Thank you @YOzaz, I adapted your snippet and it's working on my side.

bboigienman avatar Jan 24 '18 15:01 bboigienman