From 6ed55167c1e8fdc39f9bb29030f106e456f3e14f Mon Sep 17 00:00:00 2001 From: Catalin Ciocov Date: Wed, 24 Mar 2021 22:09:54 +0200 Subject: [PATCH 1/5] Add support for multiple custom configurations on the MASS module --- integrationExamples/mass/index.html | 24 +++- modules/mass.js | 173 ++++++++++++++++++---------- test/spec/modules/mass_spec.js | 20 +--- 3 files changed, 139 insertions(+), 78 deletions(-) diff --git a/integrationExamples/mass/index.html b/integrationExamples/mass/index.html index 80fe4cfb934..3b034957d13 100644 --- a/integrationExamples/mass/index.html +++ b/integrationExamples/mass/index.html @@ -36,8 +36,30 @@ pbjs.setConfig({ mass: { enabled: true, + + // official MASS-supported config: + dealIdPattern: /^MASS/i, renderUrl: 'https://cdn.massplatform.net/bootloader.js', - dealIdPattern: /^MASS/i + + // custom configs: + custom: [ + // simple: + { + dealIdPattern: /^abc/i, + renderUrl: 'https://my.domain.com/script.js' + }, + + // flexible: + { + match: function(bid) { + // return true or false, based on given bid + }, + + render: function(payload) { + // render the ad + } + } + ] } }); }); diff --git a/modules/mass.js b/modules/mass.js index 14fe556a466..67b3cc9e599 100644 --- a/modules/mass.js +++ b/modules/mass.js @@ -6,15 +6,16 @@ import { config } from '../src/config.js'; import { getHook } from '../src/hook.js'; import find from 'core-js-pure/features/array/find.js'; -export let listenerAdded = false; -export let massEnabled = false; - const defaultCfg = { dealIdPattern: /^MASS/i }; let cfg; -const massBids = {}; +export let listenerAdded = false; +export let isEnabled = false; + +const matchedBids = {}; +let renderers; init(); config.getConfig('mass', config => init(config.mass)); @@ -22,68 +23,111 @@ config.getConfig('mass', config => init(config.mass)); /** * Module init. */ -export function init(customCfg) { - cfg = Object.assign({}, defaultCfg, customCfg); +export function init(userCfg) { + cfg = Object.assign({}, defaultCfg, userCfg); if (cfg.enabled === false) { - if (massEnabled) { - massEnabled = false; + if (isEnabled) { getHook('addBidResponse').getHooks({hook: addBidResponseHook}).remove(); + isEnabled = false; } } else { - if (!massEnabled) { + if (!isEnabled) { getHook('addBidResponse').before(addBidResponseHook); - massEnabled = true; + isEnabled = true; } } + + if (isEnabled) { + updateRenderers(); + } } /** - * Before hook for 'addBidResponse'. + * Update the list of renderers based on current config. */ -export function addBidResponseHook(next, adUnitCode, bid) { - if (!isMassBid(bid) || !cfg.renderUrl) { - return next(adUnitCode, bid); +function updateRenderers() { + renderers = []; + + // official MASS renderer: + if (cfg.dealIdPattern && cfg.renderUrl) { + renderers.push({ + match: isMassBid, + render: useDefaultRender(cfg.renderUrl, 'mass') + }); } - const bidRequest = find(this.bidderRequest.bids, bidRequest => - bidRequest.bidId === bid.requestId - ); - - massBids[bid.requestId] = { - bidRequest, - bid, - adm: bid.ad - }; - - bid.ad = '