From f9b2f5fe3a99717c3d7775296376a0890f878ba9 Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Wed, 12 Oct 2022 15:02:02 -0700 Subject: [PATCH] UserID: call gpt `setPublisherProvidedId` only when PPID is available (#9099) --- modules/userId/index.js | 35 +++++++++++++++----------- test/spec/modules/userId_spec.js | 43 +++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/modules/userId/index.js b/modules/userId/index.js index ca4a67019ec..73295011089 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -425,6 +425,7 @@ function processSubmoduleCallbacks(submodules, cb) { } // cache decoded value (this is copied to every adUnit bid) submodule.idObj = submodule.submodule.decode(idObj, submodule.config); + updatePPID(submodule.idObj); } else { logInfo(`${MODULE_NAME}: ${submodule.submodule.name} - request id responded with an empty value`); } @@ -613,9 +614,9 @@ function idSystemInitializer({delay = GreedyPromise.timeout} = {}) { let initIdSystem; -function getPPID() { +function getPPID(eids = getUserIdsAsEids() || []) { // userSync.ppid should be one of the 'source' values in getUserIdsAsEids() eg pubcid.org or id5-sync.com - const matchingUserId = ppidSource && (getUserIdsAsEids() || []).find(userID => userID.source === ppidSource); + const matchingUserId = ppidSource && eids.find(userID => userID.source === ppidSource); if (matchingUserId && typeof deepAccess(matchingUserId, 'uids.0.id') === 'string') { const ppidValue = matchingUserId.uids[0].id.replace(/[\W_]/g, ''); if (ppidValue.length >= 32 && ppidValue.length <= 150) { @@ -642,18 +643,6 @@ export const requestBidsHook = timedAuctionHook('userId', function requestBidsHo ]).then(() => { // pass available user id data to bid adapters addIdDataToAdUnitBids(reqBidsConfigObj.adUnits || getGlobal().adUnits, initializedSubmodules); - const ppid = getPPID(); - if (ppid) { - if (isGptPubadsDefined()) { - window.googletag.pubads().setPublisherProvidedId(ppid); - } else { - window.googletag = window.googletag || {}; - window.googletag.cmd = window.googletag.cmd || []; - window.googletag.cmd.push(function() { - window.googletag.pubads().setPublisherProvidedId(ppid); - }); - } - } uidMetrics().join(useMetrics(reqBidsConfigObj.metrics), {propagate: false, includeGroups: true}); // calling fn allows prebid to continue processing fn.call(this, reqBidsConfigObj); @@ -853,6 +842,24 @@ function populateSubmoduleId(submodule, consentData, storedConsentData, forceRef if (response.id) { submodule.idObj = submodule.submodule.decode(response.id, submodule.config); } } } + updatePPID(submodule.idObj); +} + +function updatePPID(userIds = getUserIds()) { + if (userIds && ppidSource) { + const ppid = getPPID(createEidsArray(userIds)); + if (ppid) { + if (isGptPubadsDefined()) { + window.googletag.pubads().setPublisherProvidedId(ppid); + } else { + window.googletag = window.googletag || {}; + window.googletag.cmd = window.googletag.cmd || []; + window.googletag.cmd.push(function() { + window.googletag.pubads().setPublisherProvidedId(ppid); + }); + } + } + } } function initSubmodules(dest, submodules, consentData, forceRefresh = false) { diff --git a/test/spec/modules/userId_spec.js b/test/spec/modules/userId_spec.js index e6058673d41..4e9be5417e2 100644 --- a/test/spec/modules/userId_spec.js +++ b/test/spec/modules/userId_spec.js @@ -405,6 +405,9 @@ describe('User ID', function () { init(config); setSubmoduleRegistry([amxIdSubmodule, sharedIdSystemSubmodule, identityLinkSubmodule, imuIdSubmodule]); + // before ppid should not be set + expect(window.googletag._ppid).to.equal(undefined); + config.setConfig({ userSync: { ppid: 'pubcid.org', @@ -416,19 +419,52 @@ describe('User ID', function () { ] } }); - // before ppid should not be set - expect(window.googletag._ppid).to.equal(undefined); return expectImmediateBidHook(() => {}, {adUnits}).then(() => { // ppid should have been set without dashes and stuff expect(window.googletag._ppid).to.equal('pubCommonidvaluepubCommonidvalue'); }); }); + it('should set PPID when the source needs to call out to the network', () => { + let adUnits = [getAdUnitMock()]; + init(config); + const callback = sinon.stub(); + setSubmoduleRegistry([{ + name: 'sharedId', + getId: function () { + return {callback} + }, + decode(d) { + return d + } + }]); + config.setConfig({ + userSync: { + ppid: 'pubcid.org', + auctionDelay: 10, + userIds: [ + { + name: 'sharedId', + } + ] + } + }); + return expectImmediateBidHook(() => {}, {adUnits}).then(() => { + expect(window.googletag._ppid).to.be.undefined; + const uid = 'thismustbelongerthan32characters' + callback.yield({pubcid: uid}); + expect(window.googletag._ppid).to.equal(uid); + }); + }); + it('should set googletag ppid correctly for imuIdSubmodule', function () { let adUnits = [getAdUnitMock()]; init(config); setSubmoduleRegistry([imuIdSubmodule]); + // before ppid should not be set + expect(window.googletag._ppid).to.equal(undefined); + config.setConfig({ userSync: { ppid: 'ppid.intimatemerger.com', @@ -437,8 +473,7 @@ describe('User ID', function () { ] } }); - // before ppid should not be set - expect(window.googletag._ppid).to.equal(undefined); + return expectImmediateBidHook(() => {}, {adUnits}).then(() => { // ppid should have been set without dashes and stuff expect(window.googletag._ppid).to.equal('imppidvalueimppidvalueimppidvalue');