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

cwire Bid Adapter: Add new optional parameters #8143

Merged
merged 4 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 36 additions & 2 deletions modules/cwireBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ 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');
Expand All @@ -106,6 +111,9 @@ export const mapSlotsData = function(validBidRequests) {
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 Down Expand Up @@ -142,6 +150,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 +185,19 @@ export const spec = {

let refgroups = [];

const cwCreativeId = getQueryVariable(CW_CREATIVE_QUERY);
const cwCreativeId = parseInt(getQueryVariable(CW_CREATIVE_QUERY), 10) || null;
kodi marked this conversation as resolved.
Show resolved Hide resolved
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 = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

This has no effect? it's immediately re-assigned on the next line.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I still think this should be removed as it's useless, but you'll be the maintainer, so your choice :)

refgroups = rgQuery.split(',');
}

Expand All @@ -173,8 +206,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: 10 additions & 3 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,8 +17,12 @@ Below, the list of C-WIRE params and where they can be set.
| ---------- | ------------- | ------------- | ---- | ---------|
| pageId | | x | number | YES |
| placementId | | x | number | YES |
| refgroups | | x | string | NO |
| cwcreative | | x | integer | NO |
| cwapikey | | x | string | NO |
| adUnitElementId | | x | string | NO |


### adUnit configuration

```javascript
Expand All @@ -35,9 +39,12 @@ 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
adUnitElementId: 'other_div', // optional, div id to write to, if not set it will default to ad unit code
kodi marked this conversation as resolved.
Show resolved Hide resolved
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
}
}]
}
];
```
```
145 changes: 141 additions & 4 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 Down Expand Up @@ -85,6 +86,7 @@ describe('C-WIRE bid adapter', () => {

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

// START TESTING
Expand Down Expand Up @@ -138,12 +140,145 @@ describe('C-WIRE bid adapter', () => {

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);

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();

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('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');
});

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 config not set null or empty array is sent', function () {
kodi marked this conversation as resolved.
Show resolved Hide resolved
const bid01 = new BidRequestBuilder({
mediaTypes: {
banner: {
Expand All @@ -158,9 +293,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