diff --git a/modules/browsiRtdProvider.js b/modules/browsiRtdProvider.js index d527223964e..a1943afda8d 100644 --- a/modules/browsiRtdProvider.js +++ b/modules/browsiRtdProvider.js @@ -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 @@ -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; }, {}); @@ -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 @@ -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 @@ -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 = { /** @@ -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) { diff --git a/modules/rtdModule/index.js b/modules/rtdModule/index.js index 7dce09f0d1d..c5242c71946 100644 --- a/modules/rtdModule/index.js +++ b/modules/rtdModule/index.js @@ -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 */ /** diff --git a/test/spec/modules/browsiRtdProvider_spec.js b/test/spec/modules/browsiRtdProvider_spec.js index ee37d16905b..32e1c7fe795 100644 --- a/test/spec/modules/browsiRtdProvider_spec.js +++ b/test/spec/modules/browsiRtdProvider_spec.js @@ -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 = { @@ -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); @@ -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: '/'}, 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'); @@ -72,7 +73,7 @@ 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 }; @@ -80,4 +81,58 @@ describe('browsi Real time data sub module', function () { 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'}); + }) + }) });