A native ad is a collection of ‘assets’ such as title, description, and image that are plugged into a publisher-defined HTML template. The template includes placeholder macros for those assets, and may be styled to match the form of the surrounding page.
At a high level, here’s now native ads work in Prebid.js:
To determine whether a bidder can supply native demand, check the bidder parameter page.
Create your ad server in-page implementation as usual. See Setting Up Prebid Native in GAM for instructions for how to do this with Google Ad Manager.
There’s no reason you couldn’t use Prebid native ads with other ad servers, but we don’t have any instructions handy. Ad server vendors are welcome to submit documentation.
There are three options for defining the native template:
This table summarizes how the 3 approaches work:
Table 1: Native Implementation Approaches
Component | AdServer-Defined Creative Scenario | AdUnit-Defined Creative Scenario | Custom Renderer Scenario |
---|---|---|---|
Prebid.js | mediaTypes. native.sendTargetingKeys: false | sendTargetingKeys:false and mediaTypes.native.adTemplate contains ##macros## | sendTargetingKeys:false and mediaTypes.native.rendererUrl |
Ad Server Key Value Pairs | hb_adid | hb_adid | hb_adid |
Ad Server | Native template loads native.js and calls renderNativeAd(). Uses Prebid ##macro## format. | Native creative loads native.js and calls renderNativeAd() with requestAllAssets: true | Native creative loads native.js and calls renderNativeAd(), with requestAllAssets:true |
Prebid Universal Creative | renderNativeAd resolves macros in the creative body and CSS. | renderNativeAd resolves ##macros## in adTemplate and CSS, appending the adTemplate to the creative body | renderNativeAd loads javascript from renderUrl, calls the renderAd function, appending the results to the creative body. |
Javascript rendering function | n/a | n/a | Receives the ortb response into bid.ortb , and the renderer is responsible for resolving any macro format and returning an HTML block. |
The Prebid.js AdUnit needs to define a native mediatype object to tell bidders which assets are required. This table defines all attributes that could be included in AdUnit.mediatypes.native.
Table 2: Prebid.js AdUnit Native MediaType Options
Attribute | Scope | Description | Example | Type |
---|---|---|---|---|
adTemplate | optional | Used in the ‘AdUnit-Defined Creative Scenario’, this value will contain the Native template right in the page. | See example below. | escaped ES5 string |
rendererUrl | optional | Used in the ‘Custom Renderer Scenario’, this points to javascript code that will produce the Native template. | 'https://host/path.js' |
string |
ortb | recommended | OpenRTB configuration of the Native assets. The Native 1.2 specification can be found here | { assets: [], eventtrackers: [] } | object |
sendTargetingKeys | deprecated | Defines whether or not to send the hb_native_ASSET targeting keys to the ad server. Defaults to false . |
false |
boolean |
Prebid.js supports most assets defined by the OpenRTB native 1.2 specification. The exception is ‘video’, which could be passed through to bidders, but cannot currently be rendered.
As a gentle introduction to the OpenRTB in Prebid: a request object is a javascript object that contains some well-defined properties, like
{
native: {
...
ortb: {
assets: [...],
eventtrackers: [...]
}
}
}
assets
are the components of the ad that will assembled using the template. An asset must have an id
, used for matching the request with the response.
Each asset should have one of the following properties:
This is a regular title and the most important property is len
, to specify the length. Here’s an example:
{
id: 2,
required: 1,
title: {
len: 80
}
},
Contains an image request. Images can be of type 1
(Icon) or 3
(Main image). There are several ways to specify the image size or aspect ratio; the OpenRTB spec contains all the possibilities. Here’s an example:
{
id: 1,
required: 1,
img: {
type: 3,
w: 150,
h: 50
}
}
A ‘data’ asset is a ‘misc’ component like “sponsored by”, “rating”, likes”, or other fields that have been standardized in OpenRTB 1.2. Each data asset has a type
, and based on that type the bidders will respond with the appropriate data. For example, you can request for the name of the sponsoring company by using the type 1
.
Here’s an example:
{
id: 3,
required: 1,
data: {
type: 1
}
},
For reference, this is the table that specifies all data types:
Type ID | Name |
---|---|
1 | sponsored |
2 | desc |
3 | rating |
4 | likes |
5 | downloads |
6 | price |
7 | saleprice |
8 | phone |
9 | address |
10 | desc2 |
11 | displayurl |
12 | ctatext |
500+ | Reserved for exchange specific usage |
Please consult the OpenRTB Native spec for more details.
In order to fit special bidder requirements, publisher can utilize various ext
objects provided by OpenRTB specs.
Bid adapters will declare which custom assets they support in their documentation.
Note: The native.js::renderNativeAd()
function must be called with requestAllAssets: true
.
In the native template, simply access the custom value with the normal Prebid ##macro##
format assuming hb_native_asset_id_
as the prefix. For example, if your custom asset has id: 7
:
<div id="mytemplate">
... render a lovely creative ...
... refer to ##hb_native_asset_id_7## when appropriate ...
</div>
For instructions on implementing the native template within Google Ad Manager, see GAM Step by Step - Native Creative
In this scenario, the body of the native creative template is managed within the ad server and includes special Prebid.js macros.
When the native AdUnit is defined in the page, declare sendTargetingKeys: false
in the native Object. This will prevent Prebid.js from sending all the native-related ad server targeting variables.
Example Native AdUnit:
pbjs.addAdUnits({
code: slot.code,
mediaTypes: {
native: {
sendTargetingKeys: false,
ortb: {
assets: [{
id: 1,
required: 1,
img: {
type: 3,
w: 150,
h: 50,
}
},
{
id: 2,
required: 1,
title: {
len: 80
}
},
{
id: 3,
required: 1,
data: {
type: 1
}
},
{
id: 4,
required: 1,
data: {
type: 2
}
},
{
id: 6,
required: 1,
img: {
type: 1,
w: 50,
h: 50,
}
}]
}
}
},
bids: [{
...
}]
});
There are three key aspects of the native template:
##hb_native_asset_id_{id}##.
Note that macros can be placed in the body (HTML) and/or head (CSS) of the native creative.Example creative HTML:
<div class="sponsored-post">
<div class="thumbnail" style="background-image: url(##hb_native_asset_id_1##);"></div>
<div class="content">
<h1><a href="%%CLICK_URL_UNESC%%##hb_native_linkurl##" target="_blank" class="pb-click" hb_native_asset_id="2">##hb_native_asset_id_2##</a></h1>
<p>##hb_native_asset_id_4##</p>
<div class="attribution">##hb_native_asset_id_3##</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/prebid-universal-creative@latest/dist/%%PATTERN:hb_format%%.js"></script>
<script>
var pbNativeTagData = {};
pbNativeTagData.pubUrl = "%%PATTERN:url%%"; // GAM specific
pbNativeTagData.adId = "%%PATTERN:hb_adid%%"; // GAM specific
// if you're using 'Send All Bids' mode, you should use %%PATTERN:hb_adid_BIDDER%%
pbNativeTagData.requestAllAssets = true;
window.pbNativeTag.renderNativeAd(pbNativeTagData);
</script>
When using ‘Send All Bids’ mode you should update pbNativeTagData.adId = "%%PATTERN:hb_adid_BIDDERCODE%%";
for each bidder’s creative.
Note the URL to the Prebid Universal Creative shown in this example uses the format where %%PATTERN:hb_format%% resolves to load native.js instead of banner.js or video.js.
Example CSS:
.sponsored-post {
background-color: #fffdeb;
font-family: sans-serif;
padding: 10px 20px 10px 20px;
}
.content {
overflow: hidden;
}
.thumbnail {
width: 120px;
height: 100px;
float: left;
margin: 0 20px 10px 0;
background-image: url(##native_image##);
background-size: cover;
}
h1 {
font-size: 18px;
margin: 0;
}
a {
color: #0086b3;
text-decoration: none;
}
{: .alert.alert-info :}
See [Managing the Native Template in GAM](/adops/gam-native.html#managing-the-native-template-in-gam) for ad server instructions.
.attribution {
color: #fff;
font-size: 9px;
font-weight: bold;
display: inline-block;
letter-spacing: 2px;
background-color: #ffd724;
border-radius: 2px;
padding: 4px;
}
In this scenario, the body of the native creative template is managed within the Prebid.js AdUnit and includes special Prebid.js macros.
When the Native AdUnit is defined in the page:
sendTargetingKeys: false
in the native Object. This will prevent Prebid.js from sending all the native-related ad server targeting variables.Example AdUnit:
var adUnits = [{
code: 'native-div',
mediaTypes: {
native: {
sendTargetingKeys: false,
adTemplate: `<div class="sponsored-post">
<div class="thumbnail" style="background-image: url(##hb_native_asset_id_1##);"></div>
<div class="content">
<h1>
<a href="%%CLICK_URL_UNESC%%##hb_native_linkurl##" target="_blank" class="pb-click" hb_native_asset_id="2">##hb_native_asset_id_2##</a>
</h1>
<p>##hb_native_asset_id_4##</p>
<div class="attribution">##hb_native_asset_id_3##</div>
</div>
</div>`,
ortb: {
assets: [
{
id: 1,
required: 1,
img: {
type: 3,
w: 989,
h: 742,
}
}
{
id: 2,
required: 1,
title: {
len: 800,
}
},
{
id: 3,
required: true,
data: {
type: 1
}
},
{
id: 4,
required: true,
data: {
type: 2
}
}
]
}
}
}
}
}];
Even though the body of the native creative is defined in the AdUnit, an AdServer creative is still needed. There are two key aspects of the native creative in this scenario:
Example Creative HTML
<script src="https://cdn.jsdelivr.net/npm/prebid-universal-creative@latest/dist//%%PATTERN:hb_format%%.js"></script>
<script>
var pbNativeTagData = {};
pbNativeTagData.pubUrl = "%%PATTERN:url%%"; // GAM specific
pbNativeTagData.adId = "%%PATTERN:hb_adid%%"; // GAM specific
// if you're using 'Send All Bids' mode, you should use %%PATTERN:hb_adid_BIDDER%%
pbNativeTagData.requestAllAssets = true;
// if you want to track clicks in GAM, add the following variable
pbNativeTagData.clickUrlUnesc = "%%CLICK_URL_UNESC%%";
window.pbNativeTag.renderNativeAd(pbNativeTagData);
</script>
When using ‘Send All Bids’ mode you should update pbNativeTagData.adId = "%%PATTERN:hb_adid_BIDDERCODE%%";
for each bidder’s creative.
See Managing the Native Template Outside of GAM for ad server instructions.
In this scenario, the body of the native creative is managed from an external JavaScript file.
When the Native AdUnit is defined in the page:
sendTargetingKeys: false
in the Native Object. This will prevent Prebid.js from sending all the native-related ad server targeting variables.rendererUrl
as a URL that defines a window.renderAd
function in the creative iframe. The html returned by the window.renderAd
function will be attached to the creative’s DOM.Example AdUnit setup:
var adUnits = [{
code: 'native-div',
mediaTypes: {
native: {
sendTargetingKeys: false,
rendererUrl: "https://files.prebid.org/creatives/nativeRenderFunction.js",
ortb: {
assets: [{
id: 1,
required: 1,
img: {
type: 3,
w: 989,
h: 742,
}
},
{
id: 2,
required: 1,
title: {
len: 800,
}
},
{
id: 3,
required: true,
data: {
type: 1
}
},
{
id: 4,
required: true,
data: {
type: 2
}
}
]
}
}
}
}
}];
Even though the body of the native creative is defined in the external JavaScript, an AdServer creative is still needed. There are two key aspects of the native creative in this scenario:
Example creative HTML:
<script src="https://cdn.jsdelivr.net/npm/prebid-universal-creative@latest/dist//%%PATTERN:hb_format%%.js"></script>
<script>
var pbNativeTagData = {};
pbNativeTagData.pubUrl = "%%PATTERN:url%%"; // GAM specific
pbNativeTagData.adId = "%%PATTERN:hb_adid%%"; // GAM specific
// if you're using 'Send All Bids' mode, you should use %%PATTERN:hb_adid_BIDDER%%
pbNativeTagData.requestAllAssets = true;
// if you want to track clicks in GAM, add the following variable
pbNativeTagData.clickUrlUnesc = "%%CLICK_URL_UNESC%%";
window.pbNativeTag.renderNativeAd(pbNativeTagData);
</script>
When using Send All Bids
you should update pbNativeTagData.adId = "%%PATTERN:hb_adid_biddercode%%";
for each bidder’s creative
See Managing the Native Template Outside of GAM for ad server instructions.
Requirements for a native rendering function:
window.renderAd()
function that will be called by the Prebid Universal CreativerenderAd()
function is passed an object containing an ortb
property that contains the response in OpenRTB format, and must return a fully resolved and ready-to-display block of HTML that will be appended to the body.window.postRenderAd()
function that can be useful to trigger javascript functions.Here’s an example script:
window.renderAd = function(bid) {
const { ortb } = bid;
let template = `
<div class="sponsored-post">
<div class="thumbnail" style="background-image: url(##hb_native_asset_id_1##);"></div>
<div class="content">
<h1>
<a href="%%CLICK_URL_UNESC%%##hb_native_linkurl##" target="_blank" class="pb-click" hb_native_asset_id="2">
##hb_native_asset_id_2##
</a>
</h1>
<p>##hb_native_asset_id_4##</p>
<div class="attribution">##hb_native_asset_id_3##</div>
</div>
</div>`;
const getAssetValue = (asset) => {
if (asset.img) {
return asset.img.url;
}
if (asset.data) {
return asset.data.value;
}
if (asset.title) {
return asset.title.text;
}
if (asset.video) {
return asset.video.vasttag;
}
}
ortb.assets.forEach(asset => {
template = template.replace(`##hb_native_asset_id_${asset.id}##`, getAssetValue(asset));
});
if (ortb.privacy) {
template = template.replace("##hb_native_privacy##", ortb.privacy);
}
template = template.replace("##hb_native_linkurl##", ortb.link.url);
return template;
}
window.postRenderAd(bid) {
console.log('Native ad has been attached to the DOM');
// again, the ortb is in bid.ortb
...
}
Note that the format of any macros in external render JavaScript is totally up to you. The data object coming is in OpenRTB format.
A few details that may help understand and debug your setup:
window.pbNativeTag.renderNativeAd()
function is called, an HTML5 postmessage is made. Prebid.js is listening for this message, and responds with the bidder’s OpenRTB assets.Native ads support 3 different types of event tracking:
Prebid supports both methods (img
and js
) for impression tracking. Publishers just need to specify which method is support for which adUnit. This is configured in adUnit.mediaTypes.native.ortb.eventtrackers
. For more details take a look at the OpenRTB spec.
How impression tracking works step by step:
window.pbNativeTag.renderNativeAd()
is called, the Prebid Universal Creative will request OpenRTB response from Prebid.js (via postmessage).eventtrackers
img
method, Prebid Universal Creative will fire pixel provided in the url
propertyjs
method, Prebid Universal Creative will load the script in the same iframe where native ad is renderedThe publisher doesn’t need to implement anything for impression tracking to work, all that is needed is proper configuration of OpenRTB request on adUnit level. Prebid.js + Prebid Universal Creative will automatically take care of the impression tracking.
According to the OpenRTB spec, click trackers can be found in link
objects. There is ‘master’ link
object for the entire native ad and also each asset in the response can have it’s own separate link
object.
When native ad is rendered prebid universal creative will attach click
listeners on all DOM elements that have class pb-click
. Which means it’s up to the publisher to decide which DOM objects are ‘clickable’.
Furthermore, prebid universal creative can also track if some specific asset is clicked. To enable this, publisher needs to assign custom attribute to associated DOM element: hb_native_asset_id="5"
. In that case if user clicks on asset with id: 5
prebid universal creative will take link
object from that asset and fire all click trackers. If asset doesn’t have link
object, prebid universal creative will fire all click trackers associated with ‘master’ link
object (as described in openRTB specs).
<div class="pb-click" hb_native_asset_id="5">
If the user clicks on this div, the Prebid Universal Creative will take the link
object from the identified asset and fire any click trackers. If the asset doesn’t have a link
object, it will just fire the click trackers associated with ‘master’ link
object.
Example of how to configure a template so that the Prebid Universal Creative can fire click trackers:
<div class="sponsored-post">
<div class="thumbnail" style="background-image: url(##hb_native_asset_id_1##);"></div>
<div class="content">
<h1><a href="%%CLICK_URL_UNESC%%##hb_native_linkurl##" target="_blank" class="pb-click" hb_native_asset_id="2">##hb_native_asset_id_2##</a></h1>
<p class="pb-click" hb_native_asset_id="4">##hb_native_asset_id_4##</p>
<div class="attribution" class="pb-click" hb_native_asset_id="3">##hb_native_asset_id_3##</div>
</div>
</div>
There are detailed instructions for setting up native in GAM, but none of the Prebid functionality is specific to GAM. The requirements to use any of these approaches in a different ad server are: