Add Ad Units
Takes one ad unit object or an array of ad unit objects and adds them to the Prebid auction. For usage examples, see examples below and the Getting Started page.
pbjs.addAdUnits(Array|Object);
Arguments:
  • Array: [Object]

    An array of adUnit objects.

    Accepted Values
    Field Scope Type Description
    code Required String Unique identifier that you create and assign to this ad unit. Used to set query string targeting on the ad. If using GPT, we recommend setting this to slot element ID.
    sizes Required Array[Number] or Array[Array[Number]] All the sizes that this ad unit can accept. Examples: [400, 600], [[300, 250], [300, 600]]. For 1.0 and later, prefer mediaTypes.banner.sizes.
    bids Required Array[Object] Each bid represents a request to a bidder. For a list of properties.
    mediaTypes Optional Object Defines one or multiple media types the ad unit supports.
    labelAny Optional Array An array of string labels, used for showing responsive ads. With the labelAny operator, just one label has to match for the condition to be true. Works with the sizeConfig object passed in to pbjs.setConfig.
    labelAll Optional Array An array of string labels, used for showing responsive and conditional ads. With the labelAll conditional, every element of the target array must match an element of the label array in order for the condition to be true. Works with the sizeConfig object passed in to pbjs.setConfig.
Bids
The table below describes the list of properties in the bids array of the ad unit.
Properties of Bid Array
Field Scope Type Description
bidder Required String Bidder code. Find the complete reference for all supported bidders here..
params Required Object Bidder’s preferred way of identifying a bid request. Find the complete reference for all supported bidders here.
labelAny Optional Array An array of string labels, used for showing responsive ads. With the labelAny operator, just one label has to match for the condition to be true. Works with the sizeConfig object passed in to pbjs.setConfig.
labelAll Optional Array An array of string labels, used for showing responsive and conditional ads. With the labelAll conditional, every element of the target array must match an element of the label array in order for the condition to be true. Works with the sizeConfig object passed in to pbjs.setConfig.
Media Types
The table below describes the list of properties in the mediaTypes object of the ad unit.
Properties of Media Types
Field Scope Type Description
banner Optional (If no other properties are specified, this is the default.) Object Defines properties of a banner ad.
native Optional Object Defines properties of a native ad.
video Optional Object Defines properties of a video ad.
Examples
Banner
					
pbjs.addAdUnits({
    code: slot.code,
    mediaTypes: {
        banner: {
            sizes: [[300, 250]]
        }
    },
    bids: [
        {
            bidder: 'appnexus',
            params: {
                placementId: 13144370
            }
        }
    ]
})
					
For more detailed instructions, see Getting Started.
Native
					
pbjs.addAdUnits({
    code: slot.code,
    mediaTypes: {
        native: {
            image: {
                required: true,
                sizes: [150, 50]
            },
            title: {
                required: true,
                len: 80
            },
            sponsoredBy: {
                required: true
            },
            clickUrl: {
                required: true
            },
            body: {
                required: true
            },
            icon: {
                required: true,
                sizes: [50, 50]
            }
        }
    },
    bids: [
        {
            bidder: 'appnexus',
            params: {
                placementId: 13232354
            }
        }
    ]
});
					
For more detailed instructions, see Show Native Ads.
There are two methods for defining sizes for image-like assets (image and icon). Both are shown below, but the first example (using sizes) is more widely supported by demand partners.
Using mediaTypes.native.image.sizes (or mediaTypes.native.icon.sizes for icons):
						
mediaTypes: {
    native: {
        image: {
            required: true,
            sizes: [150, 50]
        }
    }
}
					
Using mediaTypes.native.image.aspect_ratios (or mediaTypes.native.icon.aspect_ratios for icons):
						
mediaTypes: {
    native: {
        image: {
            required: true,
            aspect_ratios: [{
                min_width: 300,        /* Optional */
                ratio_width: 2,        /* Required */
                ratio_height: 3,       /* Required */
         }]
        }
    }
}					
						
Video
For an example of an instream video ad unit, see below. For more detailed instructions, see Show Video Ads.
						
pbjs.addAdUnits({
    code: slot.code,
    mediaTypes: {
        video: {
            context: 'instream',
            playerSize: [640, 480]
        },
    },
    bids: [{
        bidder: 'appnexus',
        params: {
            placementId: 13232361,
            video: {
                skippable: true,
                playback_methods: ['auto_play_sound_off']
            }
        }
    }]
});		
						
For an example of an outstream video ad unit, see below. For more detailed instructions, see Show Outstream Video Ads.
						
pbjs.addAdUnit({
    code: slot.code,
    mediaTypes: {
        video: {
            context: 'outstream',
            playerSize: [640, 480]
        }
    },
    renderer: {
        url: 'http://cdn.adnxs.com/renderer/video/ANOutstreamVideo.js',
        render: function(bid) {
            ANOutstreamVideo.renderAd({
                targetId: bid.adUnitCode,
                adResponse: bid.adResponse,
            });
        }
    },
    ...
})	
						
Multi-format
Multiple media formats may be declared on a single ad unit, allowing any bidder that supports at least one of those media formats to participate in the auction. Any bidder that isn’t compatible with the specified mediaTypes will be dropped from the ad unit. If mediaTypes is not specified on an ad unit, banner is the assumed format and any banner bidder is eligible for inclusion.
For examples of a multi-format ad units and behavior, see below.
						
// each bidder supports at least one of the formats, so all will participate
pbjs.addAdUnits({
  code: 'div-banner-outstream-native',
  mediaTypes: {
    banner: { sizes: [[300, 250], [300, 600]] },
    native: {
        title: {required: true},
        image: {required: true},
        body: {required: false},
    },
    video: {
        context: 'outstream',
        playerSize: [400, 600],
    },
  },
  bids: [
    {
      bidder: 'bannerBidder',
      params: {placementId: '481'}
    },
    {
      bidder: 'nativeBidder',
      params: {titleAsset: '516'}
    },
    {
      bidder: 'videoBidder',
      params: {vidId: '234'}
    },
  ]
});
						
						
// only nativeBidder and videoBidder will participate
pbjs.addAdUnits({
  code: 'div-native-outstream',
  mediaTypes: {
    native: { type: 'image' },
    video: { context: 'outstream', playerSize: [400, 600] },
  },
  bids: [
    {
      bidder: 'bannerBidder',
      params: {placementId: '481'}
    },
    {
      bidder: 'nativeBidder',
      params: {titleAsset: '516'}
    },
    {
      bidder: 'videoBidder',
      params: {vidId: '234'}
    },
  ]
});
						
Response
  • param1: string

    String indicating some value that has to be passed as a string type.

  • param2: number

    Number indicating some value that has to be passed as a number type.

    Note: This number should be between 0 and infinity.
Response Description
{ response: example }