Skip to content

Commit

Permalink
Real Time Data Module - Phase3 (prebid#5783)
Browse files Browse the repository at this point in the history
* real time data module,
browsi sub module for real time data,
new hook bidsBackCallback,
fix for config unsubscribe

* change timeout&primary ad server only to auctionDelay
update docs

* support multiple providers

* change promise to callbacks
configure submodule on submodules.json

* bug fixes

* use Prebid ajax

* tests fix

* browsi real time data provider improvements

* real time data module,
browsi sub module for real time data,
new hook bidsBackCallback,
fix for config unsubscribe

* change timeout&primary ad server only to auctionDelay
update docs

* support multiple providers

* change promise to callbacks
configure submodule on submodules.json

* bug fixes

* use Prebid ajax

* tests fix

* browsi real time data provider improvements

* RTD module extend prebid#4610

* add hook for submodule init
variables naming

* RTD bug fix

* remove auction delay and related hooks

* RTD phase 3

* design changes

* fix loop continuation

* proper fix this time

* linter

* reduce loops

Co-authored-by: bretg <bgorsline@gmail.com>
  • Loading branch information
2 people authored and stsepelin committed May 28, 2021
1 parent e566c42 commit 4f55c07
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 516 deletions.
122 changes: 36 additions & 86 deletions modules/browsiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,21 @@
* @property {string} pubKey
* @property {string} url
* @property {?string} keyName
* @property {?number} auctionDelay
* @property {?number} timeout
*/

import {config} from '../src/config.js';
import * as utils 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 {getStorageManager} from '../src/storageManager.js';
import find from 'core-js-pure/features/array/find.js';

const storage = getStorageManager();

/** @type {string} */
const MODULE_NAME = 'realTimeData';
/** @type {number} */
const DEF_TIMEOUT = 1000;
/** @type {ModuleParams} */
let _moduleParams = {};
/** @type {null|Object} */
let _data = null;
/** @type {null | function} */
let _dataReadyCallback = null;
let _predictionsData = null;
/** @type {string} */
const DEF_KEYNAME = 'browsiViewability';

Expand All @@ -63,7 +54,7 @@ export function addBrowsiTag(data) {
* collect required data from page
* send data to browsi server to get predictions
*/
function collectData() {
export function collectData() {
const win = window.top;
const doc = win.document;
let browsiData = null;
Expand All @@ -88,59 +79,33 @@ function collectData() {
}

export function setData(data) {
_data = data;

if (typeof _dataReadyCallback === 'function') {
_dataReadyCallback(_data);
_dataReadyCallback = null;
}
}

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

/**
* filter server data according to adUnits received
* call callback (onDone) when data is ready
* @param {adUnit[]} adUnits
* @param {function} onDone callback function
*/
function sendDataToModule(adUnits, onDone) {
function sendDataToModule(adUnitsCodes) {
try {
waitForData(_predictionsData => {
const _predictions = _predictionsData.p || {};
let dataToReturn = adUnits.reduce((rp, cau) => {
const adUnitCode = cau && cau.code;
if (!adUnitCode) { 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) { return rp }

if (predictionData.p) {
if (!isIdMatchingAdUnit(adSlot, predictionData.w)) {
return rp;
}
rp[adUnitCode] = getKVObject(predictionData.p, _predictionsData.kn);
const _predictions = (_predictionsData && _predictionsData.p) || {};
return adUnitsCodes.reduce((rp, adUnitCode) => {
if (!adUnitCode) {
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) {
return rp
}
if (predictionData.p) {
if (!isIdMatchingAdUnit(adSlot, predictionData.w)) {
return rp;
}
return rp;
}, {});
return onDone(dataToReturn);
});
rp[adUnitCode] = getKVObject(predictionData.p, _predictionsData.kn);
}
return rp;
}, {});
} catch (e) {
onDone({});
return {};
}
}

Expand Down Expand Up @@ -231,7 +196,7 @@ function evaluate(macro, divId, adUnit, replacer) {
* @param {string} url server url with query params
*/
function getPredictionsFromServer(url) {
let ajax = ajaxBuilder(_moduleParams.auctionDelay || _moduleParams.timeout);
let ajax = ajaxBuilder();

ajax(url,
{
Expand Down Expand Up @@ -283,38 +248,23 @@ export const browsiSubmodule = {
/**
* get data and send back to realTimeData module
* @function
* @param {adUnit[]} adUnits
* @param {function} onDone
* @param {string[]} adUnitsCodes
*/
getData: sendDataToModule,
init: init
getTargetingData: sendDataToModule,
init: init,
};

function init(config, gdpr, usp) {
function init(moduleConfig) {
_moduleParams = moduleConfig.params;
if (_moduleParams && _moduleParams.siteKey && _moduleParams.pubKey && _moduleParams.url) {
collectData();
} else {
utils.logError('missing params for Browsi provider');
}
return true;
}

export function beforeInit(config) {
const confListener = config.getConfig(MODULE_NAME, ({realTimeData}) => {
try {
_moduleParams = realTimeData.dataProviders && realTimeData.dataProviders.filter(
pr => pr.name && pr.name.toLowerCase() === 'browsi')[0].params;
confListener();
_moduleParams.auctionDelay = realTimeData.auctionDelay;
_moduleParams.timeout = realTimeData.timeout || DEF_TIMEOUT;
} catch (e) {
_moduleParams = {};
}
if (_moduleParams.siteKey && _moduleParams.pubKey && _moduleParams.url) {
collectData();
} else {
utils.logError('missing params for Browsi provider');
}
});
}

function registerSubModule() {
submodule('realTimeData', browsiSubmodule);
}
registerSubModule();
beforeInit(config);
Loading

0 comments on commit 4f55c07

Please sign in to comment.