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

NoBid Analytics Adapter: support for counting blocked requests for the Optimizer #10842

Merged
merged 41 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
0eba11e
Enable supplyChain support
redaguermas Mar 4, 2020
c0fb8ee
Added support for COPPA
redaguermas Mar 5, 2020
36fab76
Merge branch 'master' of https://github.com/prebid/Prebid.js
redaguermas Mar 13, 2020
51525d7
Merge branch 'master' of https://github.com/prebid/Prebid.js
redaguermas Apr 13, 2020
c473780
Merge remote-tracking branch 'upstream/master'
Jun 1, 2020
33757b8
Merge branch 'master' of https://github.com/prebid/Prebid.js
Jun 4, 2020
9e1758b
Merge branch 'master' of https://github.com/prebid/Prebid.js
Jun 19, 2020
1d7db5d
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 18, 2020
8fca715
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 26, 2020
c11a5b3
rebuilt
Sep 27, 2020
c44bd3f
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 27, 2020
9c8440c
Added support for Extended User IDs.
Sep 28, 2020
ec5cde7
Merge branch 'master' of https://github.com/prebid/Prebid.js
May 28, 2021
ff10bbe
Added support for the "meta" attribute in bid response.
May 28, 2021
4a4c253
Merge branch 'master' of https://github.com/prebid/Prebid.js
Jul 13, 2021
01a2d94
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 27, 2021
61583a2
Merge branch 'master' of https://github.com/prebid/Prebid.js
Mar 7, 2023
00142cc
Merge branch 'master' of https://github.com/prebid/Prebid.js
Mar 31, 2023
d6160fa
Merge branch 'master' of https://github.com/prebid/Prebid.js
Jul 4, 2023
388d3e0
Delete nobidBidAdapter.js.orig
redaguermas Jul 28, 2023
1c3f853
Delete a
redaguermas Jul 28, 2023
38dc58a
Delete .jsdtscope
redaguermas Jul 28, 2023
2818cbe
Delete org.eclipse.wst.jsdt.ui.superType.container
redaguermas Jul 28, 2023
ad8662d
Delete org.eclipse.wst.jsdt.ui.superType.name
redaguermas Jul 28, 2023
8ddb9a9
Delete .project
redaguermas Jul 28, 2023
a164194
Merge branch 'master' of https://github.com/redaguermas/Prebid.js
Aug 7, 2023
0a5fbc6
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 10, 2023
c2a5fc4
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 10, 2023
a144f4a
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 21, 2023
19caf0e
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 21, 2023
e92612e
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 24, 2023
b4cdf09
Merge branch 'master' of https://github.com/prebid/Prebid.js
Aug 29, 2023
d2b8b26
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 19, 2023
b5e4f5c
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 21, 2023
ca24cfb
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 22, 2023
33d02ab
Merge branch 'master' of https://github.com/prebid/Prebid.js
Sep 25, 2023
04ebf45
Merge branch 'master' of https://github.com/prebid/Prebid.js
Oct 3, 2023
0e9b6ac
Merge branch 'master' of https://github.com/prebid/Prebid.js
Dec 14, 2023
d54a60d
Added support for counting blocked requests for the Optimizer.
Dec 14, 2023
565c688
Added missing function for testing.
Dec 15, 2023
2c814ff
Added unit tests
Dec 15, 2023
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
67 changes: 55 additions & 12 deletions modules/nobidAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -157,42 +157,85 @@ 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({
adapter: nobidAnalytics,
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);
});
};
}
}
};
Expand Down
8 changes: 6 additions & 2 deletions test/spec/modules/nobidAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down