Skip to content

Instantly share code, notes, and snippets.

@nanek
Created December 10, 2018 15:13
Show Gist options
  • Save nanek/cfcd43a1dc343dabfad505eb4e63653c to your computer and use it in GitHub Desktop.
Save nanek/cfcd43a1dc343dabfad505eb4e63653c to your computer and use it in GitHub Desktop.
Prebid.js native renderer
window.track = function (action, adId) {
var message = {message: 'Prebid Native', adId: adId};
if (action === 'click') { message.action = 'click'; }
window.parent.postMessage(JSON.stringify(message), '*');
}
var render = function(bid, adUnitDivId) {
// Can include CSS styles here, or just use styles from your site.
var markup = `
<style>
.native-ad {
background-color: #fff;
border: 1px solid #EDEDED;
border-radius: 4px;
overflow: hidden;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 12px;
height: 248px;
width: 298px;
position: relative;
}
.inset-box {
border-radius: 2px;
background-color: #fff;
position: absolute;
padding: 2px 5px;
}
.inset-box-text {
display: block;
height: 15px;
font-size: 12px;
line-height: 14px;
text-align: center;
color: #333333;
text-decoration: none;
}
.attribution {
left: 10px;
top: 10px;
}
.image img {
height: 158px;
width: 298px;
border-radius: 4px 4px 0 0;
object-fit: cover;
}
.advertiser {
width: 96px;
right: 10px;
top: 10px;
}
.title {
height: 24px;
text-align: center;
margin-top: 10px;
margin-bottom: 10px;
}
.title a {
color: #000000;
font-size: 20px;
font-weight: bold;
line-height: 25px;
text-decoration: none;
}
.call-to-action {
display: block;
width: 77px;
border-radius: 4px;
background-color: #3E86C7;
margin: 0 auto;
padding: 8px 15.5px;
text-align: center;
text-decoration: none;
}
.call-to-action-text {
height: 18px;
color: #FFFFFF;
font-size: 14px;
font-weight: bold;
line-height: 17px;
}
</style>
<div class='native-ad'>
<div class='attribution inset-box'>
<span class='inset-box-text'>Ad</span>
</div>
<div class='image'>
<a href='${bid.native.clickUrl}' target='_blank' onclick="window.track('click', '${bid.adId}');"><img src='${bid.native.image.url}'></a>
</div>
<div class='advertiser inset-box'>
<a id='native-brand' class='inset-box-text' href='${bid.native.clickUrl}' target='_blank' onclick="window.track('click', '${bid.adId}');">${bid.native.sponsoredBy}</a>
</div>
<div class='title'>
<a id='native-title' href='${bid.native.clickUrl}' target='_blank' onclick="window.track('click', '${bid.adId}');">${bid.native.title}</a>
</div>
<a class='call-to-action' href='${bid.native.clickUrl}' target='_blank' onclick="window.track('click', '${bid.adId}');">
<span class='call-to-action-text'>Learn More</span>
</a>
</div>`
// Insert ad into document.
var container = document.createElement("div");
container.innerHTML = markup;
document.getElementById(adUnitDivId).appendChild(container);
window.track('impression', bid.adId);
};
window.NativeRenderer = render
// Example ad unit config.
var adUnitDivId = "adUnitDivId";
var prebidAdUnit = {
code: adUnitDivId,
bids: [],
sizes: [[1, 1]],
mediaTypes: {
native: {
image: {
required: true,
sizes: [150, 50]
},
title: {
required: true,
len: 80
},
sponsoredBy: {
required: true
},
clickUrl: {
required: true
}
}
},
renderer: {
url: '//cdn/public/native-renderer.js',
render: function (bid) {
window.NativeRenderer(bid, adUnitDivId);
}
}
}
pbjs.bidderSettings = {
standard: {
adserverTargeting: [
key: 'hb_format',
val: function (bidResponse){
if (bidResponse.mediaType === 'native') {
return 'banner';
} else {
return bidResponse.mediaType;
}
}
]
}
}
@spormeon
Copy link

spormeon commented Aug 4, 2019

hi nanek, i'm also getting problems with prebid native. I'm uisng multi format units and the majority of the time native ads dont render with all the details in them , i think becasuse its over send limits. So I'm looking at you script here but can this work with multi format units, as i already difine a renderer for the video "oustream" mediaType

@jbartek25
Copy link

@spormeon did you try using native placeholders? Placeholders solved my issues with native ads.

@spormeon
Copy link

spormeon commented Aug 6, 2019

@jbartek25 i am trying that, but still getting probelms, my probelm seem to be that ehen i get more than 1 bid on a native ad, its trying to send all the bids adid on the targeting. I'm not 100% sure i have my native creative propeply setup as when i get 1 bid and it sends that 1 bid targeting, some links work, others dont. Would you have sample "native styles" code that works 100%, I can try that, see if it eliminates anything.

more on my prob here: prebid/Prebid.js#4059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment