Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native 1.0 support to the Criteo adapter #2

Merged
merged 3 commits into from
Dec 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions modules/criteoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const spec = {
data = buildCdbRequest(context, bidRequests);
}

return { method: 'POST', url, data };
return { method: 'POST', url, data, bidRequests };
},

/**
Expand All @@ -58,8 +58,8 @@ export const spec = {
*/
interpretResponse: (response, request) => {
if (typeof Criteo !== 'undefined') {
const adapter = Criteo.PubTag.Adapters.Prebid.GetAdapter(request.data);
return adapter.interpretResponse(response.body);
const adapter = Criteo.PubTag.Adapters.Prebid.GetAdapter(request);
return adapter.interpretResponse(response.body, request);
}

const bids = [];
Expand All @@ -71,10 +71,17 @@ export const spec = {
cpm: slot.cpm,
currency: slot.currency,
netRevenue: true,
ad: slot.creative,
ttl: slot.ttl || 60,
creativeId: slot.impid,
width: slot.width,
height: slot.height,
}
if (slot.native) {
const bidRequest = request.bidRequests.find(b => b.bidId === slot.impid);
bid.ad = createNativeAd(slot.impid, slot.native, bidRequest.params.nativeCallback);
} else {
bid.ad = slot.creative;
}
bids.push(bid);
});
}
Expand Down Expand Up @@ -155,6 +162,9 @@ function buildCdbRequest(context, bidRequests) {
if (bidRequest.params.publisherSubId) {
slot.publishersubid = bidRequest.params.publisherSubId;
}
if (bidRequest.params.nativeCallback) {
slot.native = true;
}
return slot;
}),
};
Expand All @@ -164,6 +174,32 @@ function buildCdbRequest(context, bidRequests) {
return request;
}

/**
* @param {string} id
* @param {*} payload
* @param {*} callback
* @return {string}
*/
function createNativeAd(id, payload, callback) {
// Store the callback and payload in a global object to be later accessed from the creative
window.criteo_prebid_native_slots = window.criteo_prebid_native_slots || {};
window.criteo_prebid_native_slots[id] = { callback, payload };

// The creative is in an iframe so we have to get the callback and payload
// from the parent window (doesn't work with safeframes)
return `<script type="text/javascript">
var win = window;
for (const i = 0; i < 10; ++i) {
win = win.parent;
if (win.criteo_prebid_native_slots) {
var responseSlot = win.criteo_prebid_native_slots["${id}"];
responseSlot.callback(responseSlot.payload);
break;
}
}
</script>`;
}

/**
* @return {boolean}
*/
Expand Down