Skip to content

Commit

Permalink
Conversant Bid Adapter checks pubcid directly (prebid#4430)
Browse files Browse the repository at this point in the history
  • Loading branch information
pycnvr authored and sa1omon committed Nov 28, 2019
1 parent 15c5f12 commit cd28151
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
40 changes: 39 additions & 1 deletion modules/conversantBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export const spec = {
let siteId = '';
let requestId = '';
let pubcid = null;
let pubcidName = '_pubcid';

const conversantImps = validBidRequests.map(function(bid) {
const bidfloor = utils.getBidIdParameter('bidfloor', bid.params);

siteId = utils.getBidIdParameter('site_id', bid.params);
siteId = utils.getBidIdParameter('site_id', bid.params) || siteId;
pubcidName = utils.getBidIdParameter('pubcid_name', bid.params) || pubcidName;

requestId = bid.auctionId;

const imp = {
Expand Down Expand Up @@ -132,6 +135,10 @@ export const spec = {
}
}

if (!pubcid) {
pubcid = readStoredValue(pubcidName);
}

// Add common id if available
if (pubcid) {
userExt.fpc = pubcid;
Expand Down Expand Up @@ -287,4 +294,35 @@ function copyOptProperty(src, dst, dstName) {
}
}

/**
* Look for a stored value from both cookie and local storage and return the first value found.
* @param key Key for the search
* @return {string} Stored value
*/
function readStoredValue(key) {
let storedValue;
try {
// check cookies first
storedValue = utils.getCookie(key);

if (!storedValue) {
// check expiration time before reading local storage
const storedValueExp = utils.getDataFromLocalStorage(`${key}_exp`);
if (storedValueExp === '' || (storedValueExp && (new Date(storedValueExp)).getTime() - Date.now() > 0)) {
storedValue = utils.getDataFromLocalStorage(key);
storedValue = storedValue ? decodeURIComponent(storedValue) : storedValue;
}
}

// deserialize JSON if needed
if (utils.isStr(storedValue) && storedValue.charAt(0) === '{') {
storedValue = JSON.parse(storedValue);
}
} catch (e) {
utils.logError(e);
}

return storedValue;
}

registerBidder(spec);
100 changes: 100 additions & 0 deletions test/spec/modules/conversantBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,104 @@ describe('Conversant adapter tests', function() {
expect(payload).to.have.deep.nested.property('user.ext.consent', '');
expect(payload).to.not.have.deep.nested.property('regs.ext.gdpr');
});

describe('direct reading pubcid', function() {
const ID_NAME = '_pubcid';
const CUSTOM_ID_NAME = 'myid';
const EXP = '_exp';
const TIMEOUT = 2000;

function cleanUp(key) {
window.document.cookie = key + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
localStorage.removeItem(key);
localStorage.removeItem(key + EXP);
}

function expStr(timeout) {
return (new Date(Date.now() + timeout * 60 * 60 * 24 * 1000)).toUTCString();
}

afterEach(() => {
cleanUp(ID_NAME);
cleanUp(CUSTOM_ID_NAME);
});

it('reading cookie', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);

// add a pubcid cookie
utils.setCookie(ID_NAME, '12345', expStr(TIMEOUT));

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.have.deep.nested.property('user.ext.fpc', '12345');
});

it('reading custom cookie', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);
requests[0].params.pubcid_name = CUSTOM_ID_NAME;

// add a pubcid cookie
utils.setCookie(CUSTOM_ID_NAME, '12345', expStr(TIMEOUT));

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.have.deep.nested.property('user.ext.fpc', '12345');
});

it('reading local storage with empty exp time', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);

// add a pubcid in local storage
utils.setDataInLocalStorage(ID_NAME + EXP, '');
utils.setDataInLocalStorage(ID_NAME, 'abcde');

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.have.deep.nested.property('user.ext.fpc', 'abcde');
});

it('reading local storage with valid exp time', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);

// add a pubcid in local storage
utils.setDataInLocalStorage(ID_NAME + EXP, expStr(TIMEOUT));
utils.setDataInLocalStorage(ID_NAME, 'fghijk');

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.have.deep.nested.property('user.ext.fpc', 'fghijk');
});

it('reading expired local storage', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);

// add a pubcid in local storage
utils.setDataInLocalStorage(ID_NAME + EXP, expStr(-TIMEOUT));
utils.setDataInLocalStorage(ID_NAME, 'lmnopq');

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.not.have.deep.nested.property('user.ext.fpc');
});

it('reading local storage with custom name', function() {
// clone bidRequests
const requests = utils.deepClone(bidRequests);
requests[0].params.pubcid_name = CUSTOM_ID_NAME;

// add a pubcid in local storage
utils.setDataInLocalStorage(CUSTOM_ID_NAME + EXP, expStr(TIMEOUT));
utils.setDataInLocalStorage(CUSTOM_ID_NAME, 'fghijk');

// construct http post payload
const payload = spec.buildRequests(requests).data;
expect(payload).to.have.deep.nested.property('user.ext.fpc', 'fghijk');
});
});
});

0 comments on commit cd28151

Please sign in to comment.