Skip to content

Commit

Permalink
Merge pull request #1 from ahubertcriteo/prebid-1.0
Browse files Browse the repository at this point in the history
Implement fastbid in prebid 1.0 criteo adapter
  • Loading branch information
Spark-NF authored Dec 6, 2017
2 parents e5787dc + 8087eda commit 257ba69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
30 changes: 27 additions & 3 deletions modules/criteoBidAdapter.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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);
27 changes: 16 additions & 11 deletions test/spec/modules/criteoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 257ba69

Please sign in to comment.