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

InvibesBidAdapter - kw support #4488

Merged
merged 3 commits into from
Dec 3, 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
111 changes: 95 additions & 16 deletions modules/invibesBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CONSTANTS = {
SYNC_ENDPOINT: '//k.r66net.com/GetUserSync',
TIME_TO_LIVE: 300,
DEFAULT_CURRENCY: 'EUR',
PREBID_VERSION: 1,
PREBID_VERSION: 2,
METHOD: 'GET',
INVIBES_VENDOR_ID: 436
};
Expand Down Expand Up @@ -114,7 +114,9 @@ function buildRequest(bidRequests, bidderRequest) {
height: topWin.innerHeight,

noc: !cookieDomain,
oi: invibes.optIn
oi: invibes.optIn,

kw: keywords
};

if (invibes.dom.id) {
Expand Down Expand Up @@ -190,7 +192,7 @@ function handleResponse(responseObj, bidRequests) {
for (let i = 0; i < bidRequests.length; i++) {
let bidRequest = bidRequests[i];

if (bidModel.PlacementId === bidRequest.params.placementId) {
if (bidModel.PlacementId == bidRequest.params.placementId) {
let size = getBiggerSize(bidRequest.sizes);

bidResponses.push({
Expand Down Expand Up @@ -444,7 +446,7 @@ let initDomainId = function (options) {

let persistence = options.persistence || cookiePersistence;
let state;
let minHC = 7;
let minHC = 2;

let validGradTime = function (state) {
if (!state.cr) { return false; }
Expand All @@ -468,23 +470,31 @@ let initDomainId = function (options) {
state.id = invibes.Uid.generate();
}

let graduate;

let setId = function () {
invibes.dom = {
id: (!state.cr && invibes.optIn > 0) ? state.id : undefined,
tempId: (invibes.optIn > 0) ? state.id : undefined,
graduate: graduate
};
};

graduate = function () {
let graduate = function () {
if (!state.cr) { return; }
delete state.cr;
delete state.hc;
persistence.save(state);
setId();
}
};

let regenerateId = function () {
state.id = invibes.Uid.generate();
persistence.save(state);
};

let setId = function () {
invibes.dom = {
get id() {
return (!state.cr && invibes.optIn > 0) ? state.id : undefined;
},
get tempId() {
return (invibes.optIn > 0) ? state.id : undefined;
},
graduate: graduate,
regen: regenerateId
};
};

if (state.cr && !options.noVisit) {
if (state.hc < minHC) {
Expand All @@ -498,6 +508,75 @@ let initDomainId = function (options) {
setId();
ivLogger.info('Did=' + invibes.dom.id);
};

let keywords = (function () {
const cap = 300;
let headTag = document.getElementsByTagName('head')[0];
let metaTag = headTag ? headTag.getElementsByTagName('meta') : [];

function parse(str, cap) {
let parsedStr = str.replace(/[<>~|\\"`!@#$%^&*()=+?]/g, '');

function onlyUnique(value, index, self) {
return value !== '' && self.indexOf(value) === index;
}

let words = parsedStr.split(/[\s,;.:]+/);
let uniqueWords = words.filter(onlyUnique);
parsedStr = '';

for (let i = 0; i < uniqueWords.length; i++) {
parsedStr += uniqueWords[i];
if (parsedStr.length >= cap) {
return parsedStr;
}
if (i < uniqueWords.length - 1) {
parsedStr += ',';
}
}

return parsedStr;
}

function gt(cap, prefix) {
cap = cap || 300;
prefix = prefix || '';
let title = document.title || headTag
? headTag.getElementsByTagName('title')[0]
? headTag.getElementsByTagName('title')[0].innerHTML
: ''
: '';

return parse(prefix + ',' + title, cap);
}

function gmeta(metaName, cap, prefix) {
metaName = metaName || 'keywords';
cap = cap || 100;
prefix = prefix || '';
let fallbackKw = prefix;

for (let i = 0; i < metaTag.length; i++) {
if (metaTag[i].name && metaTag[i].name.toLowerCase() === metaName.toLowerCase()) {
let kw = prefix + ',' + metaTag[i].content || '';
return parse(kw, cap);
} else if (metaTag[i].name && metaTag[i].name.toLowerCase().indexOf(metaName.toLowerCase()) > -1) {
fallbackKw = prefix + ',' + metaTag[i].content || '';
}
}

return parse(fallbackKw, cap);
}

let kw = gmeta('keywords', cap);
if (!kw || kw.length < cap - 8) {
kw = gmeta('description', cap, kw);
if (!kw || kw.length < cap - 8) {
kw = gt(cap, kw);
}
}
return kw;
}());
// =====================

export function resetInvibes() {
Expand Down
53 changes: 52 additions & 1 deletion test/spec/modules/invibesBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ describe('invibesBidAdapter:', function () {
expect(parsedData.height).to.exist;
});

it('has capped ids if local storage variable is correctly formatted', function () {
localStorage.ivvcap = '{"9731":[1,1768600800000]}';
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.capCounts).to.equal('9731=1');
});

it('does not have capped ids if local storage variable is incorrectly formatted', function () {
localStorage.ivvcap = ':[1,1574334216992]}';
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.capCounts).to.equal('');
});

it('does not have capped ids if local storage variable is expired', function () {
localStorage.ivvcap = '{"9731":[1,1574330064104]}';
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.capCounts).to.equal('');
});

it('sends query string params from localstorage 1', function () {
localStorage.ivbs = JSON.stringify({ bvci: 1 });
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.bvci).to.equal(1);
});

it('sends query string params from localstorage 2', function () {
localStorage.ivbs = JSON.stringify({ invibbvlog: true });
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.invibbvlog).to.equal(true);
});

it('does not send query string params from localstorage if unknwon', function () {
localStorage.ivbs = JSON.stringify({ someparam: true });
const request = spec.buildRequests(bidRequests, { auctionStart: Date.now() });
expect(request.data.someparam).to.be.undefined;
});

it('sends all Placement Ids', function () {
const request = spec.buildRequests(bidRequests);
expect(JSON.parse(request.data.bidParamsJson).placementIds).to.contain(bidRequests[0].params.placementId);
Expand All @@ -154,7 +190,7 @@ describe('invibesBidAdapter:', function () {
});

it('try to graduate but not enough count - doesnt send the domain id', function () {
global.document.cookie = 'ivbsdid={"id":"dvdjkams6nkq","cr":1521818537626,"hc":5}';
global.document.cookie = 'ivbsdid={"id":"dvdjkams6nkq","cr":1521818537626,"hc":0}';
let bidderRequest = { gdprConsent: { vendorData: { vendorConsents: { 436: true } } } };
let request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.not.exist;
Expand All @@ -179,27 +215,40 @@ describe('invibesBidAdapter:', function () {
stubDomainOptions(new StubbedPersistence('{"id":"f8zoh044p9oi"}'));
let request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.exist;
expect(top.window.invibes.dom.tempId).to.exist;
});

it('send the domain id after replacing it with new format', function () {
let bidderRequest = { gdprConsent: { vendorData: { vendorConsents: { 436: true } } } };
stubDomainOptions(new StubbedPersistence('{"id":"f8zoh044p9oi.8537626"}'));
let request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.exist;
expect(top.window.invibes.dom.tempId).to.exist;
});

it('dont send the domain id if consent declined', function () {
let bidderRequest = { gdprConsent: { vendorData: { vendorConsents: { 436: false } } } };
stubDomainOptions(new StubbedPersistence('{"id":"f8zoh044p9oi.8537626"}'));
let request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.not.exist;
expect(top.window.invibes.dom.tempId).to.not.exist;
});

it('dont send the domain id if no consent', function () {
let bidderRequest = { };
stubDomainOptions(new StubbedPersistence('{"id":"f8zoh044p9oi.8537626"}'));
let request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.not.exist;
expect(top.window.invibes.dom.tempId).to.not.exist;
});

it('try to init id but was already loaded on page - does not increment the id again', function () {
let bidderRequest = { gdprConsent: { vendorData: { vendorConsents: { 436: true } } } };
global.document.cookie = 'ivbsdid={"id":"dvdjkams6nkq","cr":1521818537626,"hc":0}';
let request = spec.buildRequests(bidRequests, bidderRequest);
request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.data.lId).to.not.exist;
expect(top.window.invibes.dom.tempId).to.exist;
});
});

Expand Down Expand Up @@ -273,6 +322,8 @@ describe('invibesBidAdapter:', function () {

context('when the response is valid', function () {
it('responds with a valid bid', function () {
top.window.invibes.setCookie('a', 'b', 370);
top.window.invibes.setCookie('c', 'd', 0);
let result = spec.interpretResponse({ body: response }, { bidRequests });
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});
Expand Down