Skip to content

Commit

Permalink
cwire Bid Adapter: Add new optional parameters (#8143)
Browse files Browse the repository at this point in the history
* add new global config params

* move params from global config to bidder params

* fix lint

* fix wording, remove unused param
  • Loading branch information
kodi authored Mar 9, 2022
1 parent ca333b5 commit 25f4e4c
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 30 deletions.
45 changes: 36 additions & 9 deletions modules/cwireBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,27 @@ export const mapSlotsData = function(validBidRequests) {
const slots = [];
validBidRequests.forEach(bid => {
const bidObj = {};
// get testing / debug params
let cwcreative = getValue(bid.params, 'cwcreative');
let refgroups = getValue(bid.params, 'refgroups');
let cwapikey = getValue(bid.params, 'cwapikey');

// get the pacement and page ids
let placementId = getValue(bid.params, 'placementId');
let pageId = getValue(bid.params, 'pageId');
let adUnitElementId = getValue(bid.params, 'adUnitElementId');
// get the rest of the auction/bid/transaction info
bidObj.auctionId = getBidIdParameter('auctionId', bid);
bidObj.adUnitCode = getBidIdParameter('adUnitCode', bid);
bidObj.adUnitElementId = adUnitElementId;
bidObj.bidId = getBidIdParameter('bidId', bid);
bidObj.bidderRequestId = getBidIdParameter('bidderRequestId', bid);
bidObj.placementId = placementId;
bidObj.pageId = pageId;
bidObj.mediaTypes = getBidIdParameter('mediaTypes', bid);
bidObj.transactionId = getBidIdParameter('transactionId', bid);
bidObj.sizes = getSlotSizes(bid);
bidObj.cwcreative = cwcreative;
bidObj.refgroups = refgroups;
bidObj.cwapikey = cwapikey;
slots.push(bidObj);
});

Expand All @@ -124,11 +130,6 @@ export const spec = {
isBidRequestValid: function(bid) {
bid.params = bid.params || {};

// if ad unit elemt id not provided - use adUnitCode by default
if (!bid.params.adUnitElementId) {
bid.params.adUnitElementId = bid.code;
}

if (!bid.params.placementId || !isNumber(bid.params.placementId)) {
logError('placementId not provided or invalid');
return false;
Expand All @@ -142,6 +143,21 @@ export const spec = {
return true;
},

/**
* ------------------------------------
* itterate trough slots array and try
* to extract first occurence of a given
* key, if not found - return null
* ------------------------------------
*/
getFirstValueOrNull: function(slots, key) {
const found = slots.find((item) => {
return (typeof item[key] !== 'undefined');
});

return (found) ? found[key] : null;
},

/**
* ------------------------------------
* Make a server request from the
Expand All @@ -162,9 +178,19 @@ export const spec = {

let refgroups = [];

const cwCreativeId = getQueryVariable(CW_CREATIVE_QUERY);
const cwCreativeId = parseInt(getQueryVariable(CW_CREATIVE_QUERY), 10) || null;
const cwCreativeIdFromConfig = this.getFirstValueOrNull(slots, 'cwcreative');
const refGroupsFromConfig = this.getFirstValueOrNull(slots, 'refgroups');
const cwApiKeyFromConfig = this.getFirstValueOrNull(slots, 'cwapikey');
const rgQuery = getQueryVariable(CW_GROUPS_QUERY);

if (refGroupsFromConfig !== null) {
refgroups = refGroupsFromConfig.split(',');
}

if (rgQuery !== null) {
// override if query param is present
refgroups = [];
refgroups = rgQuery.split(',');
}

Expand All @@ -173,8 +199,9 @@ export const spec = {
const payload = {
cwid: localStorageCWID,
refgroups,
cwcreative: cwCreativeId,
cwcreative: cwCreativeId || cwCreativeIdFromConfig,
slots: slots,
cwapikey: cwApiKeyFromConfig,
httpRef: referer || '',
pageViewId: CW_PAGE_VIEW_ID,
};
Expand Down
13 changes: 9 additions & 4 deletions modules/cwireBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Module Name: C-WIRE Bid Adapter
Module Type: Adagio Adapter
Maintainer: dragan@cwire.ch
Maintainer: publishers@cwire.ch

## Description

Expand All @@ -17,7 +17,10 @@ Below, the list of C-WIRE params and where they can be set.
| ---------- | ------------- | ------------- | ---- | ---------|
| pageId | | x | number | YES |
| placementId | | x | number | YES |
| adUnitElementId | | x | string | NO |
| refgroups | | x | string | NO |
| cwcreative | | x | integer | NO |
| cwapikey | | x | string | NO |


### adUnit configuration

Expand All @@ -35,9 +38,11 @@ var adUnits = [
params: {
pageId: 1422, // required - number
placementId: 2211521, // required - number
adUnitElementId: 'other_div', // optional, div id to write to, if not set it will default to ad unit code
cwcreative: 42, // optional - id of creative to force
refgroups: 'test-user', // optional - name of group or coma separated list of groups to force
cwapikey: 'api_key_xyz', // optional - api key for integration testing
}
}]
}
];
```
```
155 changes: 138 additions & 17 deletions test/spec/modules/cwireBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as utils from '../../../src/utils.js';
import { config } from '../../../src/config.js';
import {
spec,
CW_PAGE_VIEW_ID,
Expand All @@ -24,7 +25,6 @@ const BID_DEFAULTS = {
params: {
placementId: 123456,
pageId: 777,
adUnitElementId: 'target-div'
},
sizes: [[300, 250], [1, 1]],
};
Expand Down Expand Up @@ -85,6 +85,7 @@ describe('C-WIRE bid adapter', () => {

afterEach(() => {
sandbox.restore();
config.resetConfig();
});

// START TESTING
Expand Down Expand Up @@ -119,31 +120,149 @@ describe('C-WIRE bid adapter', () => {
bid01.params.pageId = '3320';
expect(spec.isBidRequestValid(bid01)).to.equal(false);
});
});

describe('C-WIRE - buildRequests()', function () {
it('creates a valid request', function () {
const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
sizes: [[1, 1]],
}
}
}).withParams({
cwcreative: 54321,
cwapikey: 'xxx-xxx-yyy-zzz-uuid',
refgroups: 'group_1',
}).build();

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01], bidderRequest01);

it('should use params.adUnitElementId if provided', function () {
expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal(54321);
expect(requests.data.cwapikey).to.equal('xxx-xxx-yyy-zzz-uuid');
expect(requests.data.refgroups[0]).to.equal('group_1');
});

it('creates a valid request - read debug params from second bid', function () {
const bid01 = new BidRequestBuilder().withParams().build();

expect(spec.isBidRequestValid(bid01)).to.equal(true);
expect(bid01.params.adUnitElementId).to.exist;
expect(bid01.params.adUnitElementId).to.equal('target-div');
const bid02 = new BidRequestBuilder({
mediaTypes: {
banner: {
sizes: [[1, 1]],
}
}
}).withParams({
cwcreative: 1234,
cwapikey: 'api_key_5',
refgroups: 'group_5',
}).build();

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01, bid02], bidderRequest01);

expect(requests.data.slots.length).to.equal(2);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwcreative).to.equal(1234);
expect(requests.data.cwapikey).to.equal('api_key_5');
expect(requests.data.refgroups[0]).to.equal('group_5');
});

it('should use default adUnitCode if no adUnitElementId provided', function () {
const bid01 = new BidRequestBuilder().withParams({}, ['adUnitElementId']).build();
expect(spec.isBidRequestValid(bid01)).to.equal(true);
expect(bid01.params.adUnitElementId).to.exist;
expect(bid01.params.adUnitElementId).to.equal('original-div');
it('creates a valid request - read debug params from first bid, ignore second', function () {
const bid01 = new BidRequestBuilder()
.withParams({
cwcreative: 33,
cwapikey: 'api_key_33',
refgroups: 'group_33',
}).build();

const bid02 = new BidRequestBuilder()
.withParams({
cwcreative: 1234,
cwapikey: 'api_key_5',
refgroups: 'group_5',
}).build();

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01, bid02], bidderRequest01);

expect(requests.data.slots.length).to.equal(2);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwcreative).to.equal(33);
expect(requests.data.cwapikey).to.equal('api_key_33');
expect(requests.data.refgroups[0]).to.equal('group_33');
});
});

describe('C-WIRE - buildRequests()', function () {
it('creates a valid request', function () {
it('creates a valid request - read debug params from 3 different slots', function () {
const bid01 = new BidRequestBuilder()
.withParams({
cwcreative: 33,
}).build();

const bid02 = new BidRequestBuilder()
.withParams({
cwapikey: 'api_key_5',
}).build();

const bid03 = new BidRequestBuilder()
.withParams({
refgroups: 'group_5',
}).build();
const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01, bid02, bid03], bidderRequest01);

expect(requests.data.slots.length).to.equal(3);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwcreative).to.equal(33);
expect(requests.data.cwapikey).to.equal('api_key_5');
expect(requests.data.refgroups[0]).to.equal('group_5');
});

it('creates a valid request - config is overriden by URL params', function () {
// for whatever reason stub for getWindowLocation does not work
// so this was the closest way to test for get params
const params = sandbox.stub(utils, 'getParameterByName');
params.withArgs('cwgroups').returns('group_1');
params.withArgs('cwcreative').returns('54321');
params.withArgs('cwgroups').returns('group_2');
params.withArgs('cwcreative').returns('654321');

const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
sizes: [[1, 1]],
}
}
}).withParams({
cwcreative: 54321,
cwapikey: 'xxx-xxx-yyy-zzz',
refgroups: 'group_1',
}).build();

const bidderRequest01 = new BidderRequestBuilder().build();

const requests = spec.buildRequests([bid01], bidderRequest01);

expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal(654321);
expect(requests.data.cwapikey).to.equal('xxx-xxx-yyy-zzz');
expect(requests.data.refgroups[0]).to.equal('group_2');
});

it('creates a valid request - if params are not set, null or empty array are sent to the API', function () {
const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
Expand All @@ -158,9 +277,11 @@ describe('C-WIRE bid adapter', () => {

expect(requests.data.slots.length).to.equal(1);
expect(requests.data.cwid).to.be.null;
expect(requests.data.cwid).to.be.null;
expect(requests.data.slots[0].sizes[0]).to.equal('1x1');
expect(requests.data.cwcreative).to.equal('54321');
expect(requests.data.refgroups[0]).to.equal('group_1');
expect(requests.data.cwcreative).to.equal(null);
expect(requests.data.cwapikey).to.equal(null);
expect(requests.data.refgroups.length).to.equal(0);
});
});

Expand Down

0 comments on commit 25f4e4c

Please sign in to comment.