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

Conversant Bid Adapter checks pubcid directly #4430

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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');
});
});
});