> For the complete documentation index, see [llms.txt](https://docs.relevant-digital.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.relevant-digital.com/technical/integration/advanced-implementation-patterns/retrieve-prebid-and-amazon-bid-data.md).

# Retrieve Prebid and Amazon bid data

## **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](https://help.relevant-digital.com/knowledge/relevantdigital.loadprebid)' 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](https://help.relevant-digital.com/knowledge/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](https://docs.prebid.org/dev-docs/adunit-reference.html) 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'.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.relevant-digital.com/technical/integration/advanced-implementation-patterns/retrieve-prebid-and-amazon-bid-data.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
