Additional script features

The onBidsReceived callback function

The YaHeaderBiddingSettings method has the onBidsReceived callback function available. It's called when the monetizer survey ends and bids are received.

As an argument, the bids received after the last monetizer request are passed as an array with the Bids object.

A new-request to the monetizers and its completion triggers the callback function again.

Example of adding the onBidsReceived function
window.YaHeaderBiddingSettings = {
  biddersMap: adfoxBiddersMap,
  adUnits: adUnits,
  timeout: userTimeout,
  callbacks: {
    onBidsReceived: (bids) => {
      console.log('---> PUBLIC CALLBACK onBidsReceived: ', bids);
    }
  }
};

A solution for sites with “infinite” scroll

On sites with infinite scroll, new content and new placements load without reloading the page. If you use Header Bidding on pages of this kind of a site, there's no auction for new placements. This is because the bids were made at the initial page response and were already used for ad selection.

To describe a new set of monetizers for placements added as new responses, use the window.Ya.headerBidding.pushAdUnits(AdUnit[]) method. Instead of AdUnit[], list an array of objects with information about which monetizers can be requested to bid for newly added placements.

  1. Before calling the method for a new placement, add initialization of new queues. To do this, replace the context.js loader code in the head element:

    Old code

    New code

    <script>
    window.yaContextCb = window.yaContextCb || []
    </script>
    <script src="https://yandex.ru/ads/system/context.js" async>
    </script>
    

    <script>
    window.Ya || (window.Ya = {});
    window.yaContextCb = window.yaContextCb || [];
    window.Ya.adfoxCode || (window.Ya.adfoxCode = {});
    window.Ya.adfoxCode.hbCallbacks || (window.Ya.adfoxCode.hbCallbacks = []);
    </script>
    <script src="https://yandex.ru/ads/system/context.js" async>
    </script>
    
  2. Add the pushAdUnits method to the ad unit code. Please note the following:

    • For a new banner placement, the pushAdUnits method must be called before adfoxCode.create.
    • In the object description, the elements' containerId must differ from those previously saved in the configuration script, but you don't need to change the configuration script itself in the page head.
Example of final code for the placement
<div id="adfox_15218"></div>
<script>window.Ya.adfoxCode.hbCallbacks.push(function() {
    window.Ya.headerBidding.pushAdUnits([
        {
          code: 'adfox_15218',
          sizes: [[728, 90]],
          bids: [
            { bidder: 'adriver', params: { placementId: 'adfox_test' } }
          ]
        }
    ]);
    window.yaContextCb.push(function() {
        window.Ya.adfoxCode.create(
            {
               ownerId: 168627,
               containerId: 'adfox_15218',
               params: { p1: 'bzzvh', p2: 'fvxb' }
          }
        );
    });
});
</script>

Enable custom targeting via Adfox HB

If a monetizer account that uses Adfox HB has custom targeting set up, you need to add the puidN parameters and targeting values to the ad tag, where N is the characteristic number from 1 to 63. The puid parameters are added to the params object in name: value format, where:

  • name is the parameter name (puidN).
  • value is the parameter value.
Example of a configuration script with a monetizer that uses Adfox HB and the added puid parameters
window.YaHeaderBiddingSettings = {
  biddersMap: { 'adfox_example': '957356' },
  adUnits: [{
    code: 'adfox_154875622348837699',
    bids: [{
      bidder: 'adfox_example',
      params: {
        pp: 'g',
        ps: 'cqmv',
        p2: 'fqnq',
        puid1: 'woman',
        puid2: '16:17:18:19',
        puid3: 'green'
      }
    }]
  }],
  timeout: 500
};

Checking monetizer bids

There are multiple cases where bid requests can be sent to monetizers more than once:

  • When loading the page, requests are sent to all the monetizers listed in the configuration script.
  • When reloading a placement, only the monetizers related to the reloaded ad are polled again using the reload() method.
  • When calling the pushAdUnits() method, the request is only sent to the monetizers listed in it.

To get bids from the latest monetizer polling, use the ad console or the getLastBidsReceived method in the browser console:

window.Ya.headerBidding.getLastBidsReceived();

The response contains an array filled with Bid objects for each placement/monetizer pair. An object may only contain one of the following fields:

  • banner: If the monetizer responds with a regular banner.
  • native: If the monetizer responds with a native banner.
  • error: If an error occurs.
Example of a method call in the browser console

interface Bid {
    adapterName: string; // monetizer name
    containerId: string; // container ID
    campaignId: number; // campaign ID in Adfox
    requestDuration: number; // request duration
    cost?: {
        currency: string; // currency, "RUB"/"USD"
        cpm: number; // CPM rate returned by the server
    },
    banner?: {
        src: string; // banner contents     },
    size?: {
        width: number; // width in px
        height: number; // height in px
    },
    error?: {
        code: number; // error code        
        message: string; // error name    
    },
    native?: {
        nativePayload: any; // object with contents for a native banner impression
    }
};

Error codes:

Error code Value
0 Unknown error.
1 No bid or response.
2 Incorrect response.
3 Timeout.
4 HTTP error.
5 Incorrect user data in the configuration script.
6 Incorrect monetizer settings.
7 The container ID received from the monetizer differs from the ID listed in the request.
8 A correct response with no status is received from the monetizer.
9 The monetizer's response has no or an incorrect CodeType.

From the page where you placed the ad tag, you can call cookie-sync pixels at the time of the request. The pixel link is provided by the monetizer.

To trigger the cookie-sync pixel:

  1. Declare a variable and add the pixel link to it. If there are multiple links, add them as a comma-separated string to the pixels array.

    var syncPixels = [{
        bidder: 'sape',
        pixels: ['https://ssp-rtb.sape.ru/rmatch/.....']
      }]
    
  2. Add a pixel call to the YaHeaderBiddingSettings section:

    syncPixels: syncPixels
    
Example of a configuration script with a cookie-sync pixel for the Sape monetizer
<script>
  var adfoxBiddersMap = {
    'sape': '1648783'
  };

  var adUnits = [
    {
      code: 'adfox_container_id',
      bids: [
        {
          bidder: 'sape',
          params: {
            placementId: '634996'
          }
        }
      ]
    }
  ];

  var userTimeout = 500;

  var syncPixels = [
    {
      bidder: 'sape',
      pixels: [
        'https://ssp-rtb.sape.ru/rmatch/1',
        'https://ssp-rtb.sape.ru/rmatch/2'
      ]
    }
  ];

  window.YaHeaderBiddingSettings = {
    biddersMap: adfoxBiddersMap,
    adUnits: adUnits,
    timeout: userTimeout,
    syncPixels: syncPixels
  };
</script>

Contact support

Send an email