1. Help Center
  2. HB Manager
  3. Applications & Use-cases

Retrieve pbjs & amzn bids & post to 3rd party function

onBeforeAdRequest

Overview

This article outlines a method by which a publisher can retrieve the highest bids from prebid and amazon and push them to a 3rd party function.

This example involves the use of the 'onBeforeAdRequest' callback. A callback function invoked after the Prebid auction is done and immediately before the ad request is initiated. 

Using the onBeforeAdRequest exposes the auction.usedUnitDatas object from which we can garner both bid response information for prebid & amazon.

This is a particularly useful method should a publisher wish to handle bid rendering or retrieve bid data themselves.

Code

relevantDigital.addAuctionCallbacks({
    onBeforeAdRequest: ({ auction }) => {
        var yieldArray = [];
        auction.usedUnitDatas.forEach((unitData) => {
            const { pbAdUnit, amazonBid: amzn } = unitData;
            const { code } = pbAdUnit;
            const { cpm: bidCpmFloat = 0, adserverTargeting } = unitData.getHighestBid() || {};
          const keyMap = { ...adserverTargeting, amzn };
                if (code && adserverTargeting) {
                    yieldArray.push({
                    placementId: code,
                    hbValues: {
                        bidCpmFloat,
                        ...keyMap,
                    },
                });
            }
        });
        console.log("Relevant Yield to 3pfunction Map : ", JSON.stringify(yieldArray, null, 2));
        window.3pfunction(yieldArray);
    }
});

Walk-through

Opposed to demonstrating the usecase in relevantDigital.loadPrebid(), the code is using relevantDigital.addAuctionCallbacks instead. The addAuctionCallbacks function is useful for implementing callback functions in scenarios where it may not be possible to edit relevantDigital.loadPrebid(). 

onBeforeAdRequest is a function invoked after the auction and before the ad request is initiated. It yields an auction object that contains a 'usedUnitDatas' array of objects that have participated in the auction. For each of these 'usedUnits' we would then like to retrieve the 'pbAdUnit' & 'amazonBid' objects.

The 'pbAdUnit' object exposes all the information on what the prebid AdUnit setup was for the auction. 

The 'amazonBid' object exposes all the information on what the amazon setup was for the auction. In the example above it is aliased to 'amzn'.

From each 'usedUnit' we can use the pbjs getHighestBid function to retrieve information on the highest bid, from which we can also extract the bid cpm and adserver targeting. 

These can then be coupled to the 'amzn' information to produce a 'keyMap', which in turn can be pushed into an object along with the 'pbAdUnit.code' to provide a coherent bid object (pbjs & amzn) for a given placement 'pbAdUnit.code'.