From 0b59d4d7fbd2224a9225bbdb0e9819aac0cdf970 Mon Sep 17 00:00:00 2001 From: redaguermas Date: Wed, 20 Dec 2023 04:35:29 -0800 Subject: [PATCH] NoBid Analytics Adapter: support for counting blocked requests for the Optimizer (#10842) * Enable supplyChain support * Added support for COPPA * rebuilt * Added support for Extended User IDs. * Added support for the "meta" attribute in bid response. * Delete nobidBidAdapter.js.orig * Delete a * Delete .jsdtscope * Delete org.eclipse.wst.jsdt.ui.superType.container * Delete org.eclipse.wst.jsdt.ui.superType.name * Delete .project * Added support for counting blocked requests for the Optimizer. * Added missing function for testing. * Added unit tests --------- Co-authored-by: Reda Guermas --- modules/nobidAnalyticsAdapter.js | 67 +++++++++++++++---- .../modules/nobidAnalyticsAdapter_spec.js | 8 ++- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/modules/nobidAnalyticsAdapter.js b/modules/nobidAnalyticsAdapter.js index 27ec1cd9451..3a7912c37e1 100644 --- a/modules/nobidAnalyticsAdapter.js +++ b/modules/nobidAnalyticsAdapter.js @@ -6,10 +6,10 @@ import CONSTANTS from '../src/constants.json'; import adapterManager from '../src/adapterManager.js'; import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js'; -const VERSION = '1.0.4'; +const VERSION = '1.1.0'; const MODULE_NAME = 'nobidAnalyticsAdapter'; -const ANALYTICS_DATA_NAME = 'analytics.nobid.io'; -const RETENTION_SECONDS = 7 * 24 * 3600; +const ANALYTICS_OPT_FLUSH_TIMEOUT_SECONDS = 5 * 1000; +const RETENTION_SECONDS = 1 * 24 * 3600; const TEST_ALLOCATION_PERCENTAGE = 5; // dont block 5% of the time; window.nobidAnalyticsVersion = VERSION; const analyticsType = 'endpoint'; @@ -148,7 +148,7 @@ nobidAnalytics = { return isExpired(data, this.retentionSeconds); }, isAnalyticsDisabled () { - let stored = storage.getDataFromLocalStorage(ANALYTICS_DATA_NAME); + let stored = storage.getDataFromLocalStorage(this.ANALYTICS_DATA_NAME); if (!isJson(stored)) return false; stored = JSON.parse(stored); if (this.isExpired(stored)) return false; @@ -157,8 +157,10 @@ nobidAnalytics = { processServerResponse (response) { if (!isJson(response)) return; const resp = JSON.parse(response); - storage.setDataInLocalStorage(ANALYTICS_DATA_NAME, JSON.stringify({ ...resp, ts: Date.now() })); - } + storage.setDataInLocalStorage(this.ANALYTICS_DATA_NAME, JSON.stringify({ ...resp, ts: Date.now() })); + }, + ANALYTICS_DATA_NAME: 'analytics.nobid.io', + ANALYTICS_OPT_NAME: 'analytics.nobid.io.optData' } adapterManager.registerAnalyticsAdapter({ @@ -166,33 +168,74 @@ adapterManager.registerAnalyticsAdapter({ code: 'nobidAnalytics', gvlid: GVLID }); +nobidAnalytics.originalAdUnits = {}; window.nobidCarbonizer = { getStoredLocalData: function () { - return storage.getDataFromLocalStorage(ANALYTICS_DATA_NAME); + const a = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_DATA_NAME); + const b = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_OPT_NAME); + const ret = {}; + if (a) ret[nobidAnalytics.ANALYTICS_DATA_NAME] = a; + if (b) ret[nobidAnalytics.ANALYTICS_OPT_NAME] = b + return ret; }, isActive: function () { - let stored = storage.getDataFromLocalStorage(ANALYTICS_DATA_NAME); + let stored = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_DATA_NAME); if (!isJson(stored)) return false; stored = JSON.parse(stored); if (isExpired(stored, nobidAnalytics.retentionSeconds)) return false; return stored.carbonizer_active || false; }, carbonizeAdunits: function (adunits, skipTestGroup) { + function processBlockedBidders (blockedBidders) { + function sendOptimizerData() { + let optData = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_OPT_NAME); + storage.removeDataFromLocalStorage(nobidAnalytics.ANALYTICS_OPT_NAME); + if (isJson(optData)) { + optData = JSON.parse(optData); + if (Object.getOwnPropertyNames(optData).length > 0) { + const event = { o_bidders: optData }; + if (nobidAnalytics.topLocation) event.topLocation = nobidAnalytics.topLocation; + sendEvent(event, 'optData'); + } + } + } + if (blockedBidders && blockedBidders.length > 0) { + let optData = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_OPT_NAME); + optData = isJson(optData) ? JSON.parse(optData) : {}; + const bidders = blockedBidders.map(rec => rec.bidder); + if (bidders && bidders.length > 0) { + bidders.forEach(bidder => { + if (!optData[bidder]) optData[bidder] = 1; + else optData[bidder] += 1; + }); + storage.setDataInLocalStorage(nobidAnalytics.ANALYTICS_OPT_NAME, JSON.stringify(optData)); + if (window.nobidAnalyticsOptTimer) return; + window.nobidAnalyticsOptTimer = setInterval(sendOptimizerData, ANALYTICS_OPT_FLUSH_TIMEOUT_SECONDS); + } + } + } function carbonizeAdunit (adunit) { - let stored = storage.getDataFromLocalStorage(ANALYTICS_DATA_NAME); + let stored = storage.getDataFromLocalStorage(nobidAnalytics.ANALYTICS_DATA_NAME); if (!isJson(stored)) return; stored = JSON.parse(stored); if (isExpired(stored, nobidAnalytics.retentionSeconds)) return; const carbonizerBidders = stored.bidders || []; - const allowedBidders = adunit.bids.filter(rec => carbonizerBidders.includes(rec.bidder)); + let originalAdUnit = null; + if (nobidAnalytics.originalAdUnits && nobidAnalytics.originalAdUnits[adunit.code]) originalAdUnit = nobidAnalytics.originalAdUnits[adunit.code]; + const allowedBidders = originalAdUnit.bids.filter(rec => carbonizerBidders.includes(rec.bidder)); + const blockedBidders = originalAdUnit.bids.filter(rec => !carbonizerBidders.includes(rec.bidder)); + processBlockedBidders(blockedBidders); adunit.bids = allowedBidders; } + for (const adunit of adunits) { + if (!nobidAnalytics.originalAdUnits[adunit.code]) nobidAnalytics.originalAdUnits[adunit.code] = JSON.parse(JSON.stringify(adunit)); + }; if (this.isActive()) { // 5% of the time do not block; if (!skipTestGroup && Math.floor(Math.random() * 101) <= TEST_ALLOCATION_PERCENTAGE) return; - adunits.forEach(adunit => { + for (const adunit of adunits) { carbonizeAdunit(adunit); - }); + }; } } }; diff --git a/test/spec/modules/nobidAnalyticsAdapter_spec.js b/test/spec/modules/nobidAnalyticsAdapter_spec.js index 742b4c16abb..06a39ffd020 100644 --- a/test/spec/modules/nobidAnalyticsAdapter_spec.js +++ b/test/spec/modules/nobidAnalyticsAdapter_spec.js @@ -466,8 +466,8 @@ describe('NoBid Prebid Analytic', function () { const previousRetention = nobidAnalytics.retentionSeconds; nobidAnalytics.retentionSeconds = 3; nobidAnalytics.processServerResponse(JSON.stringify({carbonizer_active: true})); - const stored = nobidCarbonizer.getStoredLocalData(); - expect(stored).to.contain(`{"carbonizer_active":true,"ts":`); + let stored = nobidCarbonizer.getStoredLocalData(); + expect(stored[nobidAnalytics.ANALYTICS_DATA_NAME]).to.contain(`{"carbonizer_active":true,"ts":`); clock.tick(5000); active = nobidCarbonizer.isActive(adunits, true); expect(active).to.equal(false); @@ -486,6 +486,10 @@ describe('NoBid Prebid Analytic', function () { } ] nobidCarbonizer.carbonizeAdunits(adunits, true); + stored = nobidCarbonizer.getStoredLocalData(); + expect(stored[nobidAnalytics.ANALYTICS_DATA_NAME]).to.contain('{"carbonizer_active":true,"ts":'); + expect(stored[nobidAnalytics.ANALYTICS_OPT_NAME]).to.contain('{"bidder1":1,"bidder2":1}'); + clock.tick(5000); expect(adunits[0].bids.length).to.equal(0); done();