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

Improvements for browsi RTD provider #4598

Merged
merged 30 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 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
c6e5e9e
remove eval
omerDotan Dec 18, 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
961e1c7
Merge branch 'master' into RTD-1.1
omerDotan Jan 23, 2020
faa02bf
Merge remote-tracking branch 'upstream/master'
omerDotan Jan 28, 2020
14f884a
Merge branch 'master' into RTD-1.1
omerDotan Jan 28, 2020
0556373
use external script, update macro function
omerDotan Jan 29, 2020
1c7dd3d
lint errors
omerDotan Jan 29, 2020
88430b1
Merge remote-tracking branch 'upstream/master'
omerDotan Feb 11, 2020
b8cfac1
Merge branch 'master' into RTD-1.1
omerDotan Feb 11, 2020
fd64c57
removed log
omerDotan Feb 11, 2020
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
94 changes: 79 additions & 15 deletions modules/browsiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
* @property {string} pubKey
* @property {string} url
* @property {?string} keyName
* @property {number} auctionDelay
* @property {?number} auctionDelay
* @property {?number} timeout
*/

import {config} from '../src/config.js';
import * as utils from '../src/utils';
import {submodule} from '../src/hook';
import {ajax} from '../src/ajax';
import {ajaxBuilder} from '../src/ajax';
import {loadExternalScript} from '../src/adloader';

/** @type {string} */
const MODULE_NAME = 'realTimeData';
/** @type {number} */
const DEF_TIMEOUT = 1000;
/** @type {ModuleParams} */
let _moduleParams = {};
/** @type {null|Object} */
Expand All @@ -33,14 +36,20 @@ let _dataReadyCallback = null;

/**
* add browsi script to page
* @param {string} bptUrl
* @param {Object} data
*/
export function addBrowsiTag(bptUrl) {
let script = loadExternalScript(bptUrl, 'browsi');
export function addBrowsiTag(data) {
let script = loadExternalScript(data.u, 'browsi');
script.async = true;
script.setAttribute('data-sitekey', _moduleParams.siteKey);
script.setAttribute('data-pubkey', _moduleParams.pubKey);
script.setAttribute('prebidbpt', 'true');
script.setAttribute('id', 'browsi-tag');
script.setAttribute('src', data.u);
script.prebidData = utils.deepClone(data);
if (_moduleParams.keyName) {
script.prebidData.kn = _moduleParams.keyName;
}
omerDotan marked this conversation as resolved.
Show resolved Hide resolved
return script;
}

Expand Down Expand Up @@ -109,17 +118,20 @@ function sendDataToModule(adUnits, onDone) {
return onDone({});
}
const slots = getAllSlots();
if (!slots) {
if (!slots || !slots.length) {
return onDone({});
}
let dataToReturn = adUnits.reduce((rp, cau) => {
const adUnitCode = cau && cau.code;
if (!adUnitCode) { return rp }
const predictionData = _predictions[adUnitCode];
const adSlot = getSlotById(adUnitCode);
if (!adSlot) { return rp }
const macroId = getMacroId(_predictionsData.pmd, adUnitCode, adSlot);
const predictionData = _predictions[macroId];
if (!predictionData) { return rp }

if (predictionData.p) {
if (!isIdMatchingAdUnit(adUnitCode, slots, predictionData.w)) {
if (!isIdMatchingAdUnit(adUnitCode, adSlot, predictionData.w)) {
return rp;
}
rp[adUnitCode] = getKVObject(predictionData.p, _predictionsData.kn);
Expand Down Expand Up @@ -155,36 +167,87 @@ function getKVObject(p, keyName) {
/**
* check if placement id matches one of given ad units
* @param {number} id placement id
* @param {Object[]} allSlots google slots on page
* @param {Object} slot google slot
* @param {string[]} whitelist ad units
* @return {boolean}
*/
export function isIdMatchingAdUnit(id, allSlots, whitelist) {
export function isIdMatchingAdUnit(id, slot, whitelist) {
if (!whitelist || !whitelist.length) {
return true;
}
const slot = allSlots.filter(s => s.getSlotElementId() === id);
const slotAdUnits = slot.map(s => s.getAdUnitPath());
return slotAdUnits.some(a => whitelist.indexOf(a) !== -1);
const slotAdUnits = slot.getAdUnitPath();
return whitelist.indexOf(slotAdUnits) !== -1;
}

/**
* get GPT slot by placement id
* @param {string} id placement id
* @return {?Object}
*/
function getSlotById(id) {
const slots = getAllSlots();
if (!slots || !slots.length) {
return null;
}
return slots.filter(s => s.getSlotElementId() === id)[0] || null;
}

/**
* generate id according to macro script
* @param {string} macro replacement macro
* @param {string} id placement id
* @param {Object} slot google slot
* @return {?Object}
*/
function getMacroId(macro, id, slot) {
if (macro) {
try {
const macroResult = evaluate(macro, slot.getSlotElementId(), slot.getAdUnitPath(), (match, p1) => {
return (p1 && slot.getTargeting(p1).join('_')) || 'NA';
});
return macroResult;
} catch (e) {
utils.logError(`failed to evaluate: ${macro}`);
}
}
return id;
}

function evaluate(macro, divId, adUnit, replacer) {
let macroResult = macro.p
.replace(/['"]+/g, '')
.replace(/<DIV_ID>/g, divId);

if (adUnit) {
macroResult = macroResult.replace(/<AD_UNIT>/g, adUnit);
}
if (replacer) {
macroResult = macroResult.replace(/<KEY_(\w+)>/g, replacer);
}
if (macro.s) {
macroResult = macroResult.substring(macro.s.s, macro.s.e);
}
return macroResult;
}
/**
* XMLHttpRequest to get data form browsi server
* @param {string} url server url with query params
*/
function getPredictionsFromServer(url) {
let ajax = ajaxBuilder(_moduleParams.auctionDelay || _moduleParams.timeout || DEF_TIMEOUT);
omerDotan marked this conversation as resolved.
Show resolved Hide resolved

ajax(url,
{
success: function (response, req) {
if (req.status === 200) {
try {
const data = JSON.parse(response);
if (data && data.p && data.kn) {
setData({p: data.p, kn: data.kn});
setData({p: data.p, kn: data.kn, pmd: data.pmd});
} else {
setData({});
}
addBrowsiTag(data.u);
addBrowsiTag(data);
} catch (err) {
utils.logError('unable to parse data');
setData({})
Expand Down Expand Up @@ -235,6 +298,7 @@ export function init(config) {
_moduleParams = realTimeData.dataProviders && realTimeData.dataProviders.filter(
pr => pr.name && pr.name.toLowerCase() === 'browsi')[0].params;
_moduleParams.auctionDelay = realTimeData.auctionDelay;
_moduleParams.timeout = realTimeData.timeout;
} catch (e) {
_moduleParams = {};
}
Expand Down
11 changes: 10 additions & 1 deletion modules/rtdModule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
* @type {Object}
*/

/**
* @property
* @summary timeout (if no auction dealy)
* @name ModuleConfig#timeout
* @type {number}
*/

import {getGlobal} from '../../src/prebidGlobal';
import {config} from '../../src/config.js';
import {targeting} from '../../src/targeting';
Expand All @@ -55,6 +62,8 @@ import * as utils from '../../src/utils';

/** @type {string} */
const MODULE_NAME = 'realTimeData';
/** @type {number} */
const DEF_TIMEOUT = 1000;
/** @type {RtdSubmodule[]} */
let subModules = [];
/** @type {ModuleConfig} */
Expand Down Expand Up @@ -100,7 +109,7 @@ function getProviderData(adUnits, callback) {
const dataWaitTimeout = setTimeout(() => {
processDone = true;
callback(dataReceived);
}, _moduleConfig.auctionDelay);
}, _moduleConfig.auctionDelay || _moduleConfig.timeout || DEF_TIMEOUT);

subModules.forEach(sm => {
sm.getData(adUnits, onDataReceived);
Expand Down
2 changes: 1 addition & 1 deletion src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
_auctionEnd = Date.now();

events.emit(CONSTANTS.EVENTS.AUCTION_END, getProperties());
bidsBackCallback(_adUnitCodes, function () {
bidsBackCallback(_adUnits, function () {
omerDotan marked this conversation as resolved.
Show resolved Hide resolved
try {
if (_callback != null) {
const adUnitCodes = _adUnitCodes;
Expand Down
8 changes: 4 additions & 4 deletions test/spec/modules/realTimeModule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ describe('Real time module', function() {
expect(script.async).to.equal(true);

const slots = createSlots();
const test1 = isIdMatchingAdUnit('browsiAd_1', slots, ['/57778053/Browsi_Demo_300x250']); // true
const test2 = isIdMatchingAdUnit('browsiAd_1', slots, ['/57778053/Browsi_Demo_300x250', '/57778053/Browsi']); // true
const test3 = isIdMatchingAdUnit('browsiAd_1', slots, ['/57778053/Browsi_Demo_Low']); // false
const test4 = isIdMatchingAdUnit('browsiAd_1', slots, []); // true
const test1 = isIdMatchingAdUnit('browsiAd_1', slots[0], ['/57778053/Browsi_Demo_300x250']); // true
const test2 = isIdMatchingAdUnit('browsiAd_1', slots[0], ['/57778053/Browsi_Demo_300x250', '/57778053/Browsi']); // true
const test3 = isIdMatchingAdUnit('browsiAd_1', slots[0], ['/57778053/Browsi_Demo_Low']); // false
const test4 = isIdMatchingAdUnit('browsiAd_1', slots[0], []); // true

expect(test1).to.equal(true);
expect(test2).to.equal(true);
Expand Down