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

Update Cox Bid Adapter For 1.0+ #2446

Merged
merged 4 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
156 changes: 156 additions & 0 deletions modules/coxBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
'use strict';

import * as utils from 'src/utils';
import { BANNER } from 'src/mediaTypes';
import { config } from 'src/config';
import { registerBidder } from 'src/adapters/bidderFactory';

const helper = (() => {
let srTestCapabilities = () => { // Legacy
let plugins = navigator.plugins;
let flashVer = -1;
let sf = 'Shockwave Flash';

if (plugins && plugins.length > 0) {
if (plugins[sf + ' 2.0'] || plugins[sf]) {
var swVer2 = plugins[sf + ' 2.0'] ? ' 2.0' : '';
var flashDescription = plugins[sf + swVer2].description;
flashVer = flashDescription.split(' ')[2].split('.')[0];
}
}
if (flashVer > 4) return 15; else return 7;
};

let getRand = () => {
return Math.round(Math.random() * 100000000);
};

// State variables
let env = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables are acting as global variables.

Bidder Requirements:
Support multiple instances: All adapters must support the creation of multiple concurrent instances. This means, for example, that adapters cannot rely on mutable global variables.

let tag = {};
let placementMap = {};

return {
ingest: function(rawBids = []) {
const adZoneAttributeKeys = ['id', 'size', 'thirdPartyClickUrl', 'dealId'];
const otherKeys = ['siteId', 'wrapper', 'referrerUrl'];

rawBids.forEach(oneBid => {
let params = oneBid.params || {};

tag.auctionId = oneBid.auctionId;
tag.responseJSON = true;

if (params.id && (/^\d+x\d+$/).test(params.size)) {
let adZoneKey = 'as' + params.id;
let zone = {};

zone.transactionId = oneBid.transactionId;
zone.bidId = oneBid.bidId;
tag.zones = tag.zones || {};
tag.zones[adZoneKey] = zone;

adZoneAttributeKeys.forEach(key => { if (params[key]) zone[key] = params[key]; });
otherKeys.forEach(key => { if (params[key]) tag[key] = params[key]; });

// Check for an environment setting
if (params.env) env = params.env;

// Update the placement map
let [x, y] = (params.size).split('x');
placementMap[adZoneKey] = {
'b': oneBid.bidId,
'w': x,
'h': y
};
}
});
},

transform: function(coxRawBids = {}) {
const pbjsBids = [];

for (let adZoneKey in placementMap) {
let responded = coxRawBids[adZoneKey]
let ingested = placementMap[adZoneKey];

utils.logInfo('coxBidAdapter.transform', adZoneKey, responded, ingested);

if (ingested && responded && responded['ad'] && responded['price'] > 0) {
pbjsBids.push({
requestId: ingested['b'],
cpm: responded['price'],
width: ingested['w'],
height: ingested['h'],
creativeId: responded['adid'],
dealId: responded['dealid'],
currency: 'USD',
netRevenue: true,
ttl: 300,
ad: responded['ad']
});
}
}
return pbjsBids;
},

getUrl: function() {
// Bounce if the tag is invalid
if (!tag.zones) return null;

let src = (document.location.protocol === 'https:' ? 'https://' : 'http://') + (!env || env === 'PRD' ? '' : env === 'PPE' ? 'ppe-' : env === 'STG' ? 'staging-' : '') + 'ad.afy11.net/ad' + '?mode=11' + '&ct=' + srTestCapabilities() + '&nif=0' + '&sf=0' + '&sfd=0' + '&ynw=0' + '&rand=' + getRand() + '&hb=1' + '&rk1=' + getRand() + '&rk2=' + new Date().valueOf() / 1000;

tag.pageUrl = config.getConfig('pageUrl') || utils.getTopWindowUrl();
tag.puTop = true;

// Attach the serialized tag to our string
src += '&ab=' + encodeURIComponent(JSON.stringify(tag));

return src;
},

resetState: function() {
env = '';
tag = {};
placementMap = {};
}
};
})();

export const spec = {
code: 'cox',
supportedMediaTypes: [BANNER],

isBidRequestValid: function(bid) {
return !!(bid.params && bid.params.id && bid.params.size);
},

buildRequests: function(validBidReqs) {
helper.resetState();
helper.ingest(validBidReqs);
let url = helper.getUrl();

return !url ? {} : {
method: 'GET',
url: url
};
},

interpretResponse: function({ body: { zones: coxRawBids } }) {
let bids = helper.transform(coxRawBids);

utils.logInfo('coxBidAdapter.interpretResponse', bids);
return bids;
},

getUserSyncs: function(syncOptions, [{ body: { tpCookieSync: urls = [] } }]) {
let syncs = [];
if (syncOptions.pixelEnabled && urls.length > 0) {
syncs = urls.map((url) => ({ type: 'image', url: url }))
}
utils.logInfo('coxBidAdapter.getuserSyncs', syncs);
return syncs;
}
};

registerBidder(spec);
41 changes: 41 additions & 0 deletions modules/coxBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Overview

```
Module Name: Cox/COMET Bid Adapter
Module Type: Bidder Adapter
Maintainer: [email protected]
```

# Description

Cox/COMET's adapter integration to the Prebid library.

# Test Parameters

```
var adUnits = [
{
code: 'test-leaderboard',
sizes: [[728, 90]],
bids: [{
bidder: 'cox',
params: {
size: '728x90',
id: 2000005991607,
siteId: 2000100948180,
}
}]
}, {
code: 'test-banner',
sizes: [[300, 250]],
bids: [{
bidder: 'cox',
params: {
size: '300x250',
id: 2000005991707,
siteId: 2000100948180,
}
}]
}
]
```
189 changes: 189 additions & 0 deletions test/spec/modules/coxBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { expect } from 'chai';
import { spec } from 'modules/coxBidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';
import { deepClone } from 'src/utils';

describe('CoxBidAdapter', () => {
const adapter = newBidder(spec);

describe('isBidRequestValid', () => {
const CONFIG = {
'bidder': 'cox',
'params': {
'id': '8888',
'siteId': '1000',
'size': '300x250'
}
};

it('should return true when required params present', () => {
expect(spec.isBidRequestValid(CONFIG)).to.equal(true);
});

it('should return false when id param is missing', () => {
let config = deepClone(CONFIG);
config.params.id = null;

expect(spec.isBidRequestValid(config)).to.equal(false);
});

it('should return false when size param is missing', () => {
let config = deepClone(CONFIG);
config.params.size = null;

expect(spec.isBidRequestValid(config)).to.equal(false);
});
});

describe('buildRequests', () => {
const PROD_DOMAIN = 'ad.afy11.net';
const PPE_DOMAIN = 'ppe-ad.afy11.net';
const STG_DOMAIN = 'staging-ad.afy11.net';

const BID_INFO = [{
'bidder': 'cox',
'params': {
'id': '8888',
'siteId': '1000',
'size': '300x250'
},
'sizes': [[300, 250]],
'transactionId': 'tId-foo',
'bidId': 'bId-bar'
}];

it('should send bid request to PROD_DOMAIN via GET', () => {
let request = spec.buildRequests(BID_INFO);
expect(request.url).to.have.string(PROD_DOMAIN);
expect(request.method).to.equal('GET');
});

it('should send bid request to PPE_DOMAIN when configured', () => {
let clone = deepClone(BID_INFO);
clone[0].params.env = 'PPE';

let request = spec.buildRequests(clone);
expect(request.url).to.have.string(PPE_DOMAIN);
});

it('should send bid request to STG_DOMAIN when configured', () => {
let clone = deepClone(BID_INFO);
clone[0].params.env = 'STG';

let request = spec.buildRequests(clone);
expect(request.url).to.have.string(STG_DOMAIN);
});

it('should return empty when id is invalid', () => {
let clone = deepClone(BID_INFO);
clone[0].params.id = null;

let request = spec.buildRequests(clone);
expect(request).to.be.an('object').that.is.empty;
});

it('should return empty when size is invalid', () => {
let clone = deepClone(BID_INFO);
clone[0].params.size = 'FOO';

let request = spec.buildRequests(clone);
expect(request).to.be.an('object').that.is.empty;
});
})

describe('interpretResponse', () => {
const BID_INFO = [{
'bidder': 'cox',
'params': {
'id': '2000005657007',
'siteId': '2000101880180',
'size': '728x90'
},
'transactionId': 'tId-foo',
'bidId': 'bId-a-bar'
}];

const RESPONSE = { body: {
'zones': {
'as2000005657007': {
'price': 1.88,
'dealid': 'AA128460',
'ad': '<H1>2000005657007<br/>728x90</H1>',
'adid': '7007-728-90'
}}}};

it('should return correct pbjs bid', () => {
let expectedBid = {
'requestId': 'bId-a-bar',
'cpm': 1.88,
'width': '728',
'height': '90',
'creativeId': '7007-728-90',
'dealId': 'AA128460',
'currency': 'USD',
'netRevenue': true,
'ttl': 300,
'ad': '<H1>2000005657007<br/>728x90</H1>'
};
let hokey = spec.buildRequests(BID_INFO);

let result = spec.interpretResponse(RESPONSE);
expect(result[0]).to.eql(expectedBid);
});

it('should return empty when price is zero', () => {
let clone = deepClone(RESPONSE);
clone.body.zones.as2000005657007.price = 0;
let hokey = spec.buildRequests(BID_INFO);

let result = spec.interpretResponse(clone);
expect(result).to.be.an('array').that.is.empty;
});

it('should return empty when there is no ad', () => {
let clone = deepClone(RESPONSE);
clone.body.zones.as2000005657007.ad = null;
let hokey = spec.buildRequests(BID_INFO);

let result = spec.interpretResponse(clone);
expect(result).to.be.an('array').that.is.empty;
});

it('should return empty when there is no ad unit info', () => {
let clone = deepClone(RESPONSE);
delete (clone.body.zones.as2000005657007);
let hokey = spec.buildRequests(BID_INFO);

let result = spec.interpretResponse(clone);
expect(result).to.be.an('array').that.is.empty;
});
});

describe('getUserSyncs', () => {
const RESPONSE = [{ body: {
'zones': {},
'tpCookieSync': ['http://pixel.foo.com/', 'http://pixel.bar.com/']
}}];

it('should return correct pbjs syncs when pixels are enabled', () => {
let syncs = spec.getUserSyncs({ pixelEnabled: true }, RESPONSE);

expect(syncs.map(x => x.type)).to.eql(['image', 'image']);
expect(syncs.map(x => x.url)).to.have.members(['http://pixel.bar.com/', 'http://pixel.foo.com/']);
});

it('should return empty when pixels are not enabled', () => {
let syncs = spec.getUserSyncs({ pixelEnabled: false }, RESPONSE);

expect(syncs).to.be.an('array').that.is.empty;
});

it('should return empty when response has no sync data', () => {
let clone = deepClone(RESPONSE);
delete (clone[0].body.tpCookieSync);

let syncs = spec.getUserSyncs({ pixelEnabled: true }, clone);
expect(syncs).to.be.an('array').that.is.empty;
});
});
});