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

Browi RTD provider: code improvements #7589

Merged
merged 55 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
6121b4e
real time data module,
omerDotan Aug 25, 2019
1a80b14
change timeout&primary ad server only to auctionDelay
omerDotan Sep 9, 2019
3b85815
support multiple providers
omerDotan Sep 18, 2019
0cb7b69
change promise to callbacks
omerDotan Oct 16, 2019
0908134
bug fixes
omerDotan Nov 3, 2019
cf4c5a9
use Prebid ajax
omerDotan Nov 6, 2019
7beeee3
tests fix
omerDotan Nov 6, 2019
60aaeaa
browsi real time data provider improvements
omerDotan Dec 8, 2019
0e06e6f
real time data module,
omerDotan Aug 25, 2019
e9312c7
change timeout&primary ad server only to auctionDelay
omerDotan Sep 9, 2019
c0901fe
support multiple providers
omerDotan Sep 18, 2019
398f922
change promise to callbacks
omerDotan Oct 16, 2019
b3d0bea
bug fixes
omerDotan Nov 3, 2019
a4f2de6
use Prebid ajax
omerDotan Nov 6, 2019
65ed991
tests fix
omerDotan Nov 6, 2019
15337d2
browsi real time data provider improvements
omerDotan Dec 8, 2019
76b3208
Merge remote-tracking branch 'origin/master'
omerDotan Dec 12, 2019
6a8c111
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 16, 2020
b9b05f0
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 20, 2020
89013d7
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 21, 2020
342484c
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 22, 2020
faa02bf
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 28, 2020
88430b1
Merge remote-tracking branch 'upstream/master'
omerDotan Feb 11, 2020
b73b6d7
Merge remote-tracking branch 'upstream/master'
omerDotan Feb 23, 2020
00c027c
Merge remote-tracking branch 'upstream/master'
omerDotan Apr 2, 2020
51555b6
Merge remote-tracking branch 'upstream/master'
omerDotan Apr 7, 2020
c959997
Merge remote-tracking branch 'upstream/master'
omerDotan Apr 21, 2020
a277d65
Merge remote-tracking branch 'upstream/master'
omerDotan May 4, 2020
ebf1fa8
Merge remote-tracking branch 'upstream/master'
omerDotan May 25, 2020
6ec752e
Merge remote-tracking branch 'upstream/master'
omerDotan May 28, 2020
6af3494
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 13, 2020
f0d9bb7
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 20, 2020
608eabf
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 21, 2020
6bdd935
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 26, 2020
ca43a02
Merge remote-tracking branch 'upstream/master'
omerDotan Aug 10, 2020
8346da4
Merge remote-tracking branch 'upstream/master'
omerDotan Aug 23, 2020
1f5a82e
Merge remote-tracking branch 'upstream/master'
omerDotan Aug 27, 2020
1c0d024
Merge remote-tracking branch 'upstream/master'
omerDotan Sep 14, 2020
6c7cac7
Merge remote-tracking branch 'upstream/master'
omerDotan Sep 21, 2020
1543516
Merge remote-tracking branch 'upstream/master'
omerDotan Sep 23, 2020
efe5ddf
Merge remote-tracking branch 'upstream/master'
omerDotan Oct 4, 2020
b404b69
Merge remote-tracking branch 'upstream/master'
omerDotan Nov 2, 2020
107f00c
Merge remote-tracking branch 'upstream/master'
omerDotan Nov 25, 2020
cea0004
Merge remote-tracking branch 'upstream/master'
omerDotan May 18, 2021
2dcd7af
Merge remote-tracking branch 'upstream/master'
omerDotan Jun 6, 2021
86d4cf7
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 5, 2021
191056f
Merge remote-tracking branch 'upstream/master'
omerDotan Jul 29, 2021
97c94b2
Merge remote-tracking branch 'upstream/master'
omerDotan Aug 8, 2021
ede3d7f
Merge remote-tracking branch 'upstream/master'
omerDotan Aug 10, 2021
e435be5
Merge remote-tracking branch 'upstream/master'
omerDotan Oct 11, 2021
d45789e
browsi rtd - refresh predictions & bid request data
omerDotan Oct 11, 2021
c2abb48
Browsi RTD provider improvements
omerDotan Oct 17, 2021
6a2293e
Merge remote-tracking branch 'upstream/master'
omerDotan Oct 17, 2021
88fc84f
Merge branch 'master' into browi-rtd-new-features
omerDotan Oct 17, 2021
303e2b8
bid request key fix
omerDotan Oct 26, 2021
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
124 changes: 106 additions & 18 deletions modules/browsiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
* @property {?string} keyName
*/

import { deepClone, logError, isGptPubadsDefined } from '../src/utils.js';
import { deepClone, logError, isGptPubadsDefined, isNumber, isFn, deepSetValue } from '../src/utils.js';
import {submodule} from '../src/hook.js';
import {ajaxBuilder} from '../src/ajax.js';
import {loadExternalScript} from '../src/adloader.js';
import {getStorageManager} from '../src/storageManager.js';
import find from 'core-js-pure/features/array/find.js';
import {getGlobal} from '../src/prebidGlobal.js';
import includes from 'core-js-pure/features/array/includes.js';

const storage = getStorageManager();

/** @type {ModuleParams} */
let _moduleParams = {};
/** @type {null|Object} */
let _predictionsData = null;
let _browsiData = null;
/** @type {string} */
const DEF_KEYNAME = 'browsiViewability';
/** @type {null | function} */
let _dataReadyCallback = null;
/** @type {null|Object} */
let _ic = {};

/**
* add browsi script to page
Expand Down Expand Up @@ -78,29 +84,49 @@ export function collectData() {
getPredictionsFromServer(`//${_moduleParams.url}/prebid?${toUrlParams(predictorData)}`);
}

/**
* wait for data from server
* call callback when data is ready
* @param {function} callback
*/
function waitForData(callback) {
if (_browsiData) {
_dataReadyCallback = null;
callback(_browsiData);
} else {
_dataReadyCallback = callback;
}
}

export function setData(data) {
_predictionsData = data;
_browsiData = data;
if (isFn(_dataReadyCallback)) {
_dataReadyCallback(_browsiData);
_dataReadyCallback = null;
}
}

function sendDataToModule(adUnitsCodes) {
function getRTD(auc) {
try {
const _predictions = (_predictionsData && _predictionsData.p) || {};
return adUnitsCodes.reduce((rp, adUnitCode) => {
if (!adUnitCode) {
const _bp = (_browsiData && _browsiData.p) || {};
return auc.reduce((rp, uc) => {
_ic[uc] = _ic[uc] || 0;
const _c = _ic[uc];
if (!uc) {
return rp
}
const adSlot = getSlotByCode(adUnitCode);
const identifier = adSlot ? getMacroId(_predictionsData['pmd'], adSlot) : adUnitCode;
const predictionData = _predictions[identifier];
rp[adUnitCode] = getKVObject(-1, _predictionsData['kn']);
if (!predictionData) {
const adSlot = getSlotByCode(uc);
const identifier = adSlot ? getMacroId(_browsiData['pmd'], adSlot) : uc;
const _pd = _bp[identifier];
rp[uc] = getKVObject(-1);
if (!_pd) {
return rp
}
if (predictionData.p) {
if (!isIdMatchingAdUnit(adSlot, predictionData.w)) {
if (_pd.ps) {
if (!isIdMatchingAdUnit(adSlot, _pd.w)) {
return rp;
}
rp[adUnitCode] = getKVObject(predictionData.p, _predictionsData.kn);
rp[uc] = getKVObject(getCurrentData(_pd.ps, _c));
}
return rp;
}, {});
Expand All @@ -109,6 +135,31 @@ function sendDataToModule(adUnitsCodes) {
}
}

/**
* get prediction
* return -1 if prediction not found
* @param {object} predictionObject
* @param {number} _c
* @return {number}
*/
export function getCurrentData(predictionObject, _c) {
if (!predictionObject || !isNumber(_c)) {
return -1;
}
if (isNumber(predictionObject[_c])) {
return predictionObject[_c];
}
if (Object.keys(predictionObject).length > 1) {
while (_c > 0) {
_c--;
if (isNumber(predictionObject[_c])) {
return predictionObject[_c];
}
}
}
return -1;
}

/**
* get all slots on page
* @return {Object[]} slot GoogleTag slots
Expand All @@ -122,12 +173,16 @@ function getAllSlots() {
* @param {string?} keyName
* @return {Object} key:value
*/
function getKVObject(p, keyName) {
function getKVObject(p) {
const prValue = p < 0 ? 'NA' : (Math.floor(p * 10) / 10).toFixed(2);
let prObject = {};
prObject[((_moduleParams['keyName'] || keyName || DEF_KEYNAME).toString())] = prValue.toString();
prObject[getKey()] = prValue.toString();
return prObject;
}

function getKey() {
return ((_moduleParams['keyName'] || (_browsiData && _browsiData['kn']) || DEF_KEYNAME).toString())
}
/**
* check if placement id matches one of given ad units
* @param {Object} slot google slot
Expand Down Expand Up @@ -238,6 +293,28 @@ function toUrlParams(data) {
.join('&');
}

function setBidRequestsData(bidObj, callback) {
let adUnitCodes = bidObj.adUnitCodes;
let adUnits = bidObj.adUnits || getGlobal().adUnits || [];
if (adUnitCodes) {
adUnits = adUnits.filter(au => includes(adUnitCodes, au.code));
} else {
adUnitCodes = adUnits.map(au => au.code);
}
waitForData(() => {
const data = getRTD(adUnitCodes);
if (data) {
adUnits.forEach(adUnit => {
const adUnitCode = adUnit.code;
if (data[adUnitCode]) {
deepSetValue(adUnit, 'ortb2Imp.ext.data.browsi', {[getKey()]: data[adUnitCode][getKey()]});
}
});
}
callback();
})
}

/** @type {RtdSubmodule} */
export const browsiSubmodule = {
/**
Expand All @@ -250,10 +327,21 @@ export const browsiSubmodule = {
* @function
* @param {string[]} adUnitsCodes
*/
getTargetingData: sendDataToModule,
getTargetingData: getTargetingData,
init: init,
getBidRequestData: setBidRequestsData
};

function getTargetingData(uc) {
const targetingData = getRTD(uc);
uc.forEach(auc => {
if (isNumber(_ic[auc])) {
_ic[auc] = _ic[auc] + 1;
}
});
return targetingData;
}

function init(moduleConfig) {
_moduleParams = moduleConfig.params;
if (_moduleParams && _moduleParams.siteKey && _moduleParams.pubKey && _moduleParams.url) {
Expand Down
4 changes: 2 additions & 2 deletions modules/rtdModule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
* @function?
* @summary modify bid request data
* @name RtdSubmodule#getBidRequestData
* @param {SubmoduleConfig} config
* @param {UserConsentData} userConsent
* @param {Object} reqBidsConfigObj
* @param {function} callback
* @param {SubmoduleConfig} config
* @param {UserConsentData} userConsent
*/

/**
Expand Down
69 changes: 62 additions & 7 deletions test/spec/modules/browsiRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as browsiRTD from '../../../modules/browsiRtdProvider.js';
import {makeSlot} from '../integration/faker/googletag.js';
import * as utils from '../../../src/utils'

describe('browsi Real time data sub module', function () {
const conf = {
Expand Down Expand Up @@ -29,11 +30,11 @@ describe('browsi Real time data sub module', function () {
});

it('should match placement with ad unit', function () {
const slot = makeSlot({code: '/57778053/Browsi_Demo_300x250', divId: 'browsiAd_1'});
const slot = makeSlot({code: '/123/abc', divId: 'browsiAd_1'});

const test1 = browsiRTD.isIdMatchingAdUnit(slot, ['/57778053/Browsi_Demo_300x250']); // true
const test2 = browsiRTD.isIdMatchingAdUnit(slot, ['/57778053/Browsi_Demo_300x250', '/57778053/Browsi']); // true
const test3 = browsiRTD.isIdMatchingAdUnit(slot, ['/57778053/Browsi_Demo_Low']); // false
const test1 = browsiRTD.isIdMatchingAdUnit(slot, ['/123/abc']); // true
const test2 = browsiRTD.isIdMatchingAdUnit(slot, ['/123/abc', '/456/def']); // true
const test3 = browsiRTD.isIdMatchingAdUnit(slot, ['/123/def']); // false
const test4 = browsiRTD.isIdMatchingAdUnit(slot, []); // true

expect(test1).to.equal(true);
Expand All @@ -43,12 +44,12 @@ describe('browsi Real time data sub module', function () {
});

it('should return correct macro values', function () {
const slot = makeSlot({code: '/57778053/Browsi_Demo_300x250', divId: 'browsiAd_1'});
const slot = makeSlot({code: '/123/abc', divId: 'browsiAd_1'});

slot.setTargeting('test', ['test', 'value']);
// slot getTargeting doesn't act like GPT so we can't expect real value
const macroResult = browsiRTD.getMacroId({p: '<AD_UNIT>/<KEY_test>'}, slot);
expect(macroResult).to.equal('/57778053/Browsi_Demo_300x250/NA');
expect(macroResult).to.equal('/123/abc/NA');

const macroResultB = browsiRTD.getMacroId({}, slot);
expect(macroResultB).to.equal('browsiAd_1');
Expand All @@ -72,12 +73,66 @@ describe('browsi Real time data sub module', function () {
it('should return prediction from server', function () {
makeSlot({code: 'hasPrediction', divId: 'hasPrediction'});
const data = {
p: {'hasPrediction': {p: 0.234}},
p: {'hasPrediction': {ps: {0: 0.234}}},
kn: 'bv',
pmd: undefined
};
browsiRTD.setData(data);
expect(browsiRTD.browsiSubmodule.getTargetingData(['hasPrediction'])).to.eql({hasPrediction: {bv: '0.20'}});
})
})

describe('should return matching prediction', function () {
const predictions = {
0: 0.123,
1: 0.254,
3: 0,
4: 0.8
}
const singlePrediction = {
0: 0.123
}
it('should return raw value if valid', function () {
expect(browsiRTD.getCurrentData(predictions, 0)).to.equal(0.123);
expect(browsiRTD.getCurrentData(predictions, 1)).to.equal(0.254);
})
it('should return 0 for prediction = 0', function () {
expect(browsiRTD.getCurrentData(predictions, 3)).to.equal(0);
})
it('should return -1 for invalid params', function () {
expect(browsiRTD.getCurrentData(null, 3)).to.equal(-1);
expect(browsiRTD.getCurrentData(predictions, null)).to.equal(-1);
})
it('should return prediction according to object keys length ', function () {
expect(browsiRTD.getCurrentData(singlePrediction, 0)).to.equal(0.123);
expect(browsiRTD.getCurrentData(singlePrediction, 1)).to.equal(-1);
expect(browsiRTD.getCurrentData(singlePrediction, 2)).to.equal(-1);
expect(browsiRTD.getCurrentData(predictions, 4)).to.equal(0.8);
expect(browsiRTD.getCurrentData(predictions, 5)).to.equal(0.8);
expect(browsiRTD.getCurrentData(predictions, 8)).to.equal(0.8);
})
})
describe('should set bid request data', function () {
const data = {
p: {
'adUnit1': {ps: {0: 0.234}},
'adUnit2': {ps: {0: 0.134}}},
kn: 'bv',
pmd: undefined
};
browsiRTD.setData(data);
const fakeAdUnits = [
{
code: 'adUnit1'
},
{
code: 'adUnit2'
}
]
browsiRTD.browsiSubmodule.getBidRequestData({adUnits: fakeAdUnits}, () => {}, {}, null);
it('should set ad unit params with prediction values', function () {
expect(utils.deepAccess(fakeAdUnits[0], 'ortb2Imp.ext.data.browsi')).to.eql({bv: '0.20'});
expect(utils.deepAccess(fakeAdUnits[1], 'ortb2Imp.ext.data.browsi')).to.eql({bv: '0.10'});
})
})
});