From e87ac61711b1b07614b3061f4a42d17a1ee2a5dd Mon Sep 17 00:00:00 2001 From: Rocco Barbini <46724608+rbarbini-ias@users.noreply.github.com> Date: Thu, 31 Mar 2022 17:18:23 +0200 Subject: [PATCH] IAS RTD Module: Custom key values (#8214) * Mapping table initialization A constant initialises a mapping table where each possible key used by IAS is mapped to itself. The dataProviders config for the IAS RTD module accepts an optional new property: keyMappings. The init function reads this property and uses it to overwrite any key that is already in the pre-initialised mapping table with the new value provided. In a future addition, the mapping table will be used to populate the key-values actually sent to the adserver. * Rename Key Values After merging the key-values for each ad unit, a renaming function is called to ensure that any custom key chosen by the client is used. * Bugfix Call the callback() function in getBidRequestData. * Unit tests changed to include one renamed parameter. * Unit tests corrected and adapted to cover the current features. --- modules/iasRtdProvider.js | 43 +++++++++++- test/spec/modules/iasRtdProvider_spec.js | 86 +++++++++++++++--------- 2 files changed, 98 insertions(+), 31 deletions(-) diff --git a/modules/iasRtdProvider.js b/modules/iasRtdProvider.js index 6f7b2d5215d..58899d7a8c0 100644 --- a/modules/iasRtdProvider.js +++ b/modules/iasRtdProvider.js @@ -13,6 +13,28 @@ const FRAUD_FIELD_NAME = 'fr'; const SLOTS_OBJECT_FIELD_NAME = 'slots'; const CUSTOM_FIELD_NAME = 'custom'; const IAS_KW = 'ias-kw'; +const IAS_KEY_MAPPINGS = { + adt: 'adt', + alc: 'alc', + dlm: 'dlm', + hat: 'hat', + off: 'off', + vio: 'vio', + drg: 'drg', + 'ias-kw': 'ias-kw', + fr: 'fr', + vw: 'vw', + grm: 'grm', + pub: 'pub', + vw05: 'vw05', + vw10: 'vw10', + vw15: 'vw15', + vw30: 'vw30', + vw_vv: 'vw_vv', + grm_vv: 'grm_vv', + pub_vv: 'pub_vv', + id: 'id' +}; /** * Module init @@ -26,6 +48,14 @@ export function init(config, userConsent) { utils.logError('missing pubId param for IAS provider'); return false; } + if (params.hasOwnProperty('keyMappings')) { + const keyMappings = params.keyMappings; + for (let prop in keyMappings) { + if (IAS_KEY_MAPPINGS.hasOwnProperty(prop)) { + IAS_KEY_MAPPINGS[prop] = keyMappings[prop] + } + } + } return true; } @@ -62,6 +92,16 @@ function stringifyScreenSize() { return [(window.screen && window.screen.width) || -1, (window.screen && window.screen.height) || -1].join('.'); } +function renameKeyValues(source) { + let result = {}; + for (let prop in IAS_KEY_MAPPINGS) { + if (source.hasOwnProperty(prop)) { + result[IAS_KEY_MAPPINGS[prop]] = source[prop]; + } + } + return result; +} + function formatTargetingData(adUnit) { let result = {}; if (iasTargeting[BRAND_SAFETY_OBJECT_FIELD_NAME]) { @@ -76,7 +116,7 @@ function formatTargetingData(adUnit) { if (iasTargeting[SLOTS_OBJECT_FIELD_NAME] && adUnit in iasTargeting[SLOTS_OBJECT_FIELD_NAME]) { utils.mergeDeep(result, iasTargeting[SLOTS_OBJECT_FIELD_NAME][adUnit]); } - return result; + return renameKeyValues(result); } function constructQueryString(anId, adUnits) { @@ -147,6 +187,7 @@ function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) { undefined, { method: 'GET' } ); + callback() } /** @type {RtdSubmodule} */ diff --git a/test/spec/modules/iasRtdProvider_spec.js b/test/spec/modules/iasRtdProvider_spec.js index 192b2c6e3c3..0d52c594fb5 100644 --- a/test/spec/modules/iasRtdProvider_spec.js +++ b/test/spec/modules/iasRtdProvider_spec.js @@ -29,7 +29,7 @@ describe('iasRtdProvider is a RTD provider that', function () { const value = iasSubModule.init(config); expect(value).to.equal(false); }); - it('returns false missing pubId param', function () { + it('returns true with only the pubId param', function () { const config = { name: 'ias', waitForIt: true, @@ -40,6 +40,20 @@ describe('iasRtdProvider is a RTD provider that', function () { const value = iasSubModule.init(config); expect(value).to.equal(true); }); + it('returns true with the pubId and keyMappings params', function () { + const config = { + name: 'ias', + waitForIt: true, + params: { + pubId: '123456', + keyMappings: { + 'id': 'ias_id' + } + } + }; + const value = iasSubModule.init(config); + expect(value).to.equal(true); + }); }); describe('has a method `getBidRequestData` that', function () { it('exists', function () { @@ -75,34 +89,43 @@ describe('iasRtdProvider is a RTD provider that', function () { it('exists', function () { expect(iasSubModule.getTargetingData).to.be.a('function'); }); - it('invoke method', function () { - const targeting = iasSubModule.getTargetingData(adUnitsCode, config); - expect(adUnitsCode).to.length(2); - expect(targeting).to.be.not.null; - expect(targeting).to.be.not.empty; - expect(targeting['one-div-id']).to.be.not.null; - const targetingKeys = Object.keys(targeting['one-div-id']); - expect(targetingKeys.length).to.equal(10); - expect(targetingKeys['adt']).to.be.not.null; - expect(targetingKeys['alc']).to.be.not.null; - expect(targetingKeys['dlm']).to.be.not.null; - expect(targetingKeys['drg']).to.be.not.null; - expect(targetingKeys['hat']).to.be.not.null; - expect(targetingKeys['off']).to.be.not.null; - expect(targetingKeys['vio']).to.be.not.null; - expect(targetingKeys['fr']).to.be.not.null; - expect(targetingKeys['ias-kw']).to.be.not.null; - expect(targetingKeys['id']).to.be.not.null; - expect(targeting['one-div-id']['adt']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['alc']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['dlm']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['drg']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['hat']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['off']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['vio']).to.be.eq('veryLow'); - expect(targeting['one-div-id']['fr']).to.be.eq('false'); - expect(targeting['one-div-id']['id']).to.be.eq('4813f7a2-1f22-11ec-9bfd-0a1107f94461'); - }); + describe('invoke method', function () { + it('returns a targeting object with the right shape', function () { + const targeting = iasSubModule.getTargetingData(adUnitsCode, config); + expect(adUnitsCode).to.length(2); + expect(targeting).to.be.not.null; + expect(targeting).to.be.not.empty; + expect(targeting['one-div-id']).to.be.not.null; + }); + it('returns the right keys', function () { + const targeting = iasSubModule.getTargetingData(adUnitsCode, config); + const targetingKeys = Object.keys(targeting['one-div-id']); + expect(targetingKeys.length).to.equal(10); + expect(targetingKeys).to.include('adt', 'adt key missing from the targeting object'); + expect(targetingKeys).to.include('alc', 'alc key missing from the targeting object'); + expect(targetingKeys).to.include('dlm', 'dlm key missing from the targeting object'); + expect(targetingKeys).to.include('drg', 'drg key missing from the targeting object'); + expect(targetingKeys).to.include('hat', 'hat key missing from the targeting object'); + expect(targetingKeys).to.include('off', 'off key missing from the targeting object'); + expect(targetingKeys).to.include('vio', 'vio key missing from the targeting object'); + expect(targetingKeys).to.include('fr', 'fr key missing from the targeting object'); + expect(targetingKeys).to.include('ias-kw', 'ias-kw key missing from the targeting object'); + expect(targetingKeys).to.not.include('id', 'id key present in the targeting object, should have been renamed to ias_id'); + expect(targetingKeys).to.include('ias_id', 'ias_id key missing from the targeting object'); + }); + it('returns the right values', function () { + const targeting = iasSubModule.getTargetingData(adUnitsCode, config); + expect(targeting['one-div-id']['adt']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['alc']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['dlm']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['drg']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['hat']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['off']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['vio']).to.be.eq('veryLow'); + expect(targeting['one-div-id']['fr']).to.be.eq('false'); + expect(targeting['one-div-id']['ias_id']).to.be.eq('4813f7a2-1f22-11ec-9bfd-0a1107f94461'); + }); + }) }); }); @@ -110,7 +133,10 @@ const config = { name: 'ias', waitForIt: true, params: { - pubId: 1234 + pubId: 1234, + keyMappings: { + 'id': 'ias_id' + } } };