From 8087edaf9c446fd39450be7a9dbb056ac08cf013 Mon Sep 17 00:00:00 2001 From: "a.hubert" Date: Fri, 1 Dec 2017 11:52:19 +0100 Subject: [PATCH] Implement fastbid in prebid 1.0 criteo adapter --- modules/criteoBidAdapter.js | 30 +++++++++++++++++++--- test/spec/modules/criteoBidAdapter_spec.js | 27 +++++++++++-------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/modules/criteoBidAdapter.js b/modules/criteoBidAdapter.js index 12d37be8a72..2b8c38275bf 100755 --- a/modules/criteoBidAdapter.js +++ b/modules/criteoBidAdapter.js @@ -1,14 +1,16 @@ +import { loadScript } from 'src/adloader'; import { registerBidder } from 'src/adapters/bidderFactory'; import { parse } from 'src/url'; import * as utils from 'src/utils'; +const ADAPTER_VERSION = 2; const BIDDER_CODE = 'criteo'; const CDB_ENDPOINT = '//bidder.criteo.com/cdb'; -const PROFILE_ID = 207; -const ADAPTER_VERSION = 2; const INTEGRATION_MODES = { 'amp': 1, }; +const PROFILE_ID = 207; +const PUBLISHER_TAG_URL = '//static.criteo.net/js/ld/publishertag.prebid.js'; /** @type {BidderSpec} */ export const spec = { @@ -31,6 +33,11 @@ export const spec = { let url; let data; + // If publisher tag not already loaded try to get it from fast bid else load it + if (typeof Criteo === 'undefined' && !tryGetCriteoFastBid()) { + loadScript(PUBLISHER_TAG_URL); + } + if (typeof Criteo !== 'undefined') { const adapter = new Criteo.PubTag.Adapters.Prebid(PROFILE_ID, ADAPTER_VERSION, bidRequests, bidderRequest); url = adapter.buildCdbUrl(); @@ -57,7 +64,7 @@ export const spec = { const bids = []; - if (response.body.slots && utils.isArray(response.body.slots)) { + if (response.body && response.body.slots && utils.isArray(response.body.slots)) { response.body.slots.forEach(slot => { const bid = { requestId: slot.impid, @@ -157,4 +164,21 @@ function buildCdbRequest(context, bidRequests) { return request; } +/** + * @return {boolean} + */ +function tryGetCriteoFastBid() { + let success = false; + try { + const fastBid = localStorage.getItem('criteo_fast_bid'); + if (fastBid !== null) { + eval(fastBid); // eslint-disable-line no-eval + success = true; + } + } catch (e) { + // Unable to get fast bid + } + return success; +} + registerBidder(spec); diff --git a/test/spec/modules/criteoBidAdapter_spec.js b/test/spec/modules/criteoBidAdapter_spec.js index d17efe9fe2b..6fce3f16be3 100755 --- a/test/spec/modules/criteoBidAdapter_spec.js +++ b/test/spec/modules/criteoBidAdapter_spec.js @@ -61,10 +61,11 @@ describe('The Criteo bidding adapter', () => { }, ]; const request = spec.buildRequests(bidRequests); - expect(request.url).to.equal('//bidder.criteo.com/cdb'); + expect(request.url).to.match(/^\/\/bidder\.criteo\.com\/cdb\?profileId=207&av=2&cb=\d/); expect(request.method).to.equal('POST'); + const ortbRequest = request.data; expect(ortbRequest.publisher.url).to.equal(utils.getTopWindowUrl()); - expect(ortbRequest.slots).to.have.lengthOf(2); + expect(ortbRequest.slots).to.have.lengthOf(1); expect(ortbRequest.slots[0].impid).to.equal('bid-123'); expect(ortbRequest.slots[0].transactionid).to.equal('transaction-123'); expect(ortbRequest.slots[0].sizes).to.have.lengthOf(1); @@ -85,8 +86,9 @@ describe('The Criteo bidding adapter', () => { }, ]; const request = spec.buildRequests(bidRequests); - expect(request.url).to.equal('//bidder.criteo.com/cdb'); + expect(request.url).to.match(/^\/\/bidder\.criteo\.com\/cdb\?profileId=207&av=2&cb=\d/); expect(request.method).to.equal('POST'); + const ortbRequest = request.data; expect(ortbRequest.publisher.url).to.equal(utils.getTopWindowUrl()); expect(ortbRequest.publisher.networkid).to.equal(456); expect(ortbRequest.slots).to.have.lengthOf(1); @@ -119,8 +121,9 @@ describe('The Criteo bidding adapter', () => { }, ]; const request = spec.buildRequests(bidRequests); - expect(request.url).to.equal('//bidder.criteo.com/cdb'); + expect(request.url).to.match(/^\/\/bidder\.criteo\.com\/cdb\?profileId=207&av=2&cb=\d/); expect(request.method).to.equal('POST'); + const ortbRequest = request.data; expect(ortbRequest.publisher.url).to.equal(utils.getTopWindowUrl()); expect(ortbRequest.publisher.networkid).to.equal(456); expect(ortbRequest.slots).to.have.lengthOf(2); @@ -145,13 +148,15 @@ describe('The Criteo bidding adapter', () => { it('should properly parse a bid response', () => { const response = { - slots: [{ - impid: 'test-requestId', - cpm: 1.23, - ad: 'test-ad', - width: 728, - height: 90, - }], + body: { + slots: [{ + impid: 'test-requestId', + cpm: 1.23, + creative: 'test-ad', + width: 728, + height: 90, + }], + }, }; const bids = spec.interpretResponse(response, null); expect(bids).to.have.lengthOf(1);