Skip to content

Commit

Permalink
Preciso Bid adapter : Native implemented (prebid#12278)
Browse files Browse the repository at this point in the history
* Bid adapter added

* added the coverage code

* precisonatBidAdapter.md file added

* bid price macro replacement

* tracking url encoding removed

* fix

* test

* modified the adapter for native

* test logs removed

* updated

* error fixed

* Added new library bidNativeUtils.js

---------

Co-authored-by: PrecisoSRL <[email protected]>
  • Loading branch information
NikhilGopalChennissery and PrecisoSRL authored Oct 24, 2024
1 parent aafef47 commit 260a1b6
Show file tree
Hide file tree
Showing 12 changed files with 787 additions and 231 deletions.
203 changes: 203 additions & 0 deletions integrationExamples/gpt/precisonativeExample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<html>

<head>
<script type="text/javascript" src="../../build/dev/prebid.js" async></script>

<script>
var PREBID_TIMEOUT = 2000;

var adUnits = [
{
code: 'test-div',
mediaTypes: {
native: {
adTemplate: `
<div class="sponsored-post">
<div class="thumbnail" style="background-image: url('${encodeURI('##hb_native_asset_id_2##')}'); width=100%; height=100%; background-repeat: no-repeat;
background-size: cover; >
<div class="content">
<h1>
<a href="${encodeURI("https://" + '##hb_native_linkurl##')}" target="_blank" class="pb-click" hb_native_asset_id="4" >
${encodeURI('##hb_native_body##')}
</a>
</h1>
<p class="pb-click">${encodeURI('##hb_native_asset_id_4##')}</p>
<div class="attribution" class="pb-click hb_native_asset_id="1">${encodeURI('##hb_native_asset_id_1##')}</div>
</div>
</div>
</div>
`,
ortb: {
assets: [
{
id: 2,
required: 0,
img: {
type: 3,
w: 300,
h: 250
}
},
{
id: 1,
required: 1,
title: {
len: 800
}
},
{
id: 4,
required: 0,
data: {
type: 1
}
}
]
}
}
},

bids: [
{
bidder: 'preciso',
params: {
publisherId: 'PRECISO_TEST00001',
traffic: 'native',
// bidFloor: 0.12,
// currency: ['USD']
}
}
]
}
];

var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
</script>



<script>
pbjs.que.push(function () {
pbjs.setConfig({ enableSendAllBids: true });
pbjs.addAdUnits(adUnits);
pbjs.setConfig({
userSync: {
userIds: [{
name: 'sharedId',
storage: {
name: '_sharedid',
type: 'cookie',
expires: 365
}
}],
filterSettings: {
iframe: {
bidders: ['preciso'],
filter: 'include'
},
image: {
bidders: ['preciso'],
filter: 'include'
}
}
},
floors: {
currency: 'USD',
skipRate: 5,
modelVersion: 'Sports Ad Unit Floors',
schema: {
fields: ['mediaType']
},
values: {
'banner': 0.05,
'native': 0.1
}
},
ortb2: {
bcat: ['IAB1-1'],
badv: ['example.com'],
wlang: ['fr', 'en']
},
enableTIDs: true,
debug: true
});
});

function renderEverything() {
var winners = pbjs.getHighestCpmBids();
for (var i = 0; i < winners.length; i++) {
renderOne(winners[i]);
}
}

function renderOne(winningBid) {
if (winningBid && winningBid.adId) {
let options = winningBid.adm
console.log("Here 123");
var div = document.getElementById(winningBid.adUnitCode);
if (div) {
let iframe = document.createElement('iframe');
iframe.frameBorder = '0';
iframe.style.width = '300';
iframe.style.height = '600';
div.appendChild(iframe);
console.log("Here 1234");
if (winningBid.mediaType == "native") {
console.log("Here 1234 native");
var domdoc = iframe.contentWindow.document;
console.log("Here 12345 native");
pbjs.renderAd(domdoc, winningBid.adId);
console.log("Here 123456 native");
}
}
}
}

</script>
<script>
pbjs.que.push(function () {
pbjs.requestBids({
timeout: 2000,

bidsBackHandler: renderEverything
});
});
</script>
<script src="https://cdn.jsdelivr.net/npm/prebid-universal-creative@latest/dist/native.js"></script>
<script>
var pbNativeTagData = {};
pbNativeTagData.pubUrl = '##hb_adomain##'; // GAM specific
pbNativeTagData.adId = "##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>
</head>

<body>
<h2>Ad Serverless Test Page</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum
</p>
<div id="test-div"></div>
<p>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum
comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by
Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.
The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</p>
<div id="test-nat-div"></div>
</body>

</html>
104 changes: 104 additions & 0 deletions libraries/precisoUtils/bidNativeUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { deepAccess, logInfo } from '../../src/utils.js';
import { NATIVE } from '../../src/mediaTypes.js';
import { macroReplace } from './bidUtils.js';

const TTL = 55;
// Codes defined by OpenRTB Native Ads 1.1 specification
export const OPENRTB = {
NATIVE: {
IMAGE_TYPE: {
ICON: 1,
MAIN: 3,
},
ASSET_ID: {
TITLE: 1,
IMAGE: 2,
ICON: 3,
BODY: 4,
SPONSORED: 5,
CTA: 6
},
DATA_ASSET_TYPE: {
SPONSORED: 1,
DESC: 2,
CTA_TEXT: 12,
},
}
};

/**
* @param {object} serverBid Bid by OpenRTB 2.5 §4.2.3
* @returns {object} Prebid native bidObject
*/
export function interpretNativeBid(serverBid) {
return {
requestId: serverBid.impid,
mediaType: NATIVE,
cpm: serverBid.price,
creativeId: serverBid.adid || serverBid.crid,
width: 1,
height: 1,
ttl: TTL,
meta: {
advertiserDomains: serverBid.adomain
},
netRevenue: true,
currency: 'USD',
// native: interpretNativeAd(serverBid.adm)
native: interpretNativeAd(macroReplace(serverBid.adm, serverBid.price))
}
}

/**
* @param {string} adm JSON-encoded Request by OpenRTB Native Ads 1.1 §4.1
* @returns {object} Prebid bidObject.native
*/

export function interpretNativeAd(adm) {
try {
const native = JSON.parse(adm).native;
if (native) {
const result = {
clickUrl: encodeURI(native.link.url),
impressionTrackers: native.imptrackers || native.eventtrackers[0].url,
};
if (native.link.clicktrackers) {
result.clickTrackers = native.link.clicktrackers[0];
}

native.assets.forEach(asset => {
switch (asset.id) {
case OPENRTB.NATIVE.ASSET_ID.TITLE:
result.title = deepAccess(asset, 'title.text');
break;
case OPENRTB.NATIVE.ASSET_ID.IMAGE:
result.image = {
url: encodeURI(asset.img.url),
width: deepAccess(asset, 'img.w'),
height: deepAccess(asset, 'img.h')
};
break;
case OPENRTB.NATIVE.ASSET_ID.ICON:
result.icon = {
url: encodeURI(asset.img.url),
width: deepAccess(asset, 'img.w'),
height: deepAccess(asset, 'img.h')
};
break;
case OPENRTB.NATIVE.ASSET_ID.BODY:
result.body = deepAccess(asset, 'data.value');
break;
case OPENRTB.NATIVE.ASSET_ID.SPONSORED:
result.sponsoredBy = deepAccess(asset, 'data.value');
break;
case OPENRTB.NATIVE.ASSET_ID.CTA:
result.cta = deepAccess(asset, 'data.value');
break;
}
});
return result;
}
} catch (error) {
logInfo('Error in bidUtils interpretNativeAd' + error);
}
}
Loading

0 comments on commit 260a1b6

Please sign in to comment.