Skip to content

Commit

Permalink
NoBid Analytics Adapter: added support for flag to control bidWon and…
Browse files Browse the repository at this point in the history
… auctionEnd independently. (prebid#11087)

* 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

* Added support for Analytics adapter flag to control bidWon and auctionEnd independently.

---------

Co-authored-by: Reda Guermas <reda.guermas@nobid.io>
  • Loading branch information
redaguermas and Reda Guermas authored Feb 10, 2024
1 parent 8bda2ce commit f7163de
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 18 deletions.
26 changes: 20 additions & 6 deletions modules/nobidAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CONSTANTS from '../src/constants.json';
import adapterManager from '../src/adapterManager.js';
import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js';

const VERSION = '1.1.0';
const VERSION = '2.0.0';
const MODULE_NAME = 'nobidAnalyticsAdapter';
const ANALYTICS_OPT_FLUSH_TIMEOUT_SECONDS = 5 * 1000;
const RETENTION_SECONDS = 1 * 24 * 3600;
Expand Down Expand Up @@ -49,7 +49,7 @@ function sendEvent (event, eventType) {
return ret;
}
if (!nobidAnalytics.initOptions || !nobidAnalytics.initOptions.siteId || !event) return;
if (nobidAnalytics.isAnalyticsDisabled()) {
if (nobidAnalytics.isAnalyticsDisabled(eventType)) {
log('NoBid Analytics is Disabled');
return;
}
Expand Down Expand Up @@ -97,8 +97,16 @@ function sendAuctionEndEvent (event, eventType) {
if (data) cleanupObjectAttributes(data.bidderRequests, ['bidderCode', 'bidderRequestId', 'bids', 'refererInfo']);
if (data) cleanupObjectAttributes(data.bidsReceived, ['bidderCode', 'width', 'height', 'adUnitCode', 'statusMessage', 'requestId', 'mediaType', 'cpm']);
if (data) cleanupObjectAttributes(data.noBids, ['bidder', 'sizes', 'bidId']);
if (data.bidderRequests) cleanupObjectAttributes(data.bidderRequests.bids, ['mediaTypes', 'adUnitCode', 'sizes', 'bidId']);
if (data.bidderRequests) cleanupObjectAttributes(data.bidderRequests.refererInfo, ['topmostLocation']);
if (data.bidderRequests) {
data.bidderRequests.forEach(bidderRequest => {
cleanupObjectAttributes(bidderRequest.bids, ['mediaTypes', 'adUnitCode', 'sizes', 'bidId']);
});
}
if (data.bidderRequests) {
data.bidderRequests.forEach(bidderRequest => {
cleanupObjectAttributes(bidderRequest.refererInfo, ['topmostLocation']);
});
}
sendEvent(data, eventType);
}
function auctionInit (event) {
Expand Down Expand Up @@ -147,12 +155,18 @@ nobidAnalytics = {
isExpired (data) {
return isExpired(data, this.retentionSeconds);
},
isAnalyticsDisabled () {
isAnalyticsDisabled (eventType) {
let stored = storage.getDataFromLocalStorage(this.ANALYTICS_DATA_NAME);
if (!isJson(stored)) return false;
stored = JSON.parse(stored);
if (this.isExpired(stored)) return false;
return stored.disabled;
if (stored.disabled === 1) return true;
else if (stored.disabled === 0) return false;
if (eventType) {
if (stored[`disabled_${eventType}`] === 1) return true;
else if (stored[`disabled_${eventType}`] === 0) return false;
}
return false;
},
processServerResponse (response) {
if (!isJson(response)) return;
Expand Down
127 changes: 115 additions & 12 deletions test/spec/modules/nobidAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ describe('NoBid Prebid Analytic', function () {
bidderRequestId: '7c1940bb285731',
bids: [
{
bidder: 'nobid',
params: { siteId: SITE_ID },
mediaTypes: { banner: { sizes: [[728, 90]] } },
adUnitCode: 'leaderboard',
Expand Down Expand Up @@ -390,19 +389,28 @@ describe('NoBid Prebid Analytic', function () {
const auctionEndRequest = JSON.parse(server.requests[0].requestBody);
expect(auctionEndRequest).to.have.property('auctionId', requestOutgoing.auctionId);
expect(auctionEndRequest.bidderRequests).to.have.length(1);
expect(auctionEndRequest.bidderRequests[0]).to.have.property('bidderCode', requestOutgoing.bidderRequests[0].bidderCode);
expect(auctionEndRequest.bidderRequests[0].bidderCode).to.equal(requestOutgoing.bidderRequests[0].bidderCode);
expect(auctionEndRequest.bidderRequests[0].bids).to.have.length(1);
expect(auctionEndRequest.bidderRequests[0].bids[0]).to.have.property('bidder', requestOutgoing.bidderRequests[0].bids[0].bidder);
expect(auctionEndRequest.bidderRequests[0].bids[0]).to.have.property('adUnitCode', requestOutgoing.bidderRequests[0].bids[0].adUnitCode);
expect(auctionEndRequest.bidderRequests[0].bids[0].params).to.have.property('siteId', requestOutgoing.bidderRequests[0].bids[0].params.siteId);
expect(auctionEndRequest.bidderRequests[0].refererInfo).to.have.property('topmostLocation', requestOutgoing.bidderRequests[0].refererInfo.topmostLocation);
expect(typeof auctionEndRequest.bidderRequests[0].bids[0].bidder).to.equal('undefined');
expect(auctionEndRequest.bidderRequests[0].bids[0].adUnitCode).to.equal(requestOutgoing.bidderRequests[0].bids[0].adUnitCode);
expect(typeof auctionEndRequest.bidderRequests[0].bids[0].params).to.equal('undefined');
expect(typeof auctionEndRequest.bidderRequests[0].bids[0].src).to.equal('undefined');
expect(auctionEndRequest.bidderRequests[0].refererInfo.topmostLocation).to.equal(requestOutgoing.bidderRequests[0].refererInfo.topmostLocation);
expect(auctionEndRequest.bidsReceived).to.have.length(1);
expect(auctionEndRequest.bidsReceived[0].bidderCode).to.equal(requestOutgoing.bidsReceived[0].bidderCode);
expect(auctionEndRequest.bidsReceived[0].width).to.equal(requestOutgoing.bidsReceived[0].width);
expect(auctionEndRequest.bidsReceived[0].height).to.equal(requestOutgoing.bidsReceived[0].height);
expect(auctionEndRequest.bidsReceived[0].mediaType).to.equal(requestOutgoing.bidsReceived[0].mediaType);
expect(auctionEndRequest.bidsReceived[0].cpm).to.equal(requestOutgoing.bidsReceived[0].cpm);
expect(auctionEndRequest.bidsReceived[0].adUnitCode).to.equal(requestOutgoing.bidsReceived[0].adUnitCode);
expect(typeof auctionEndRequest.bidsReceived[0].source).to.equal('undefined');

done();
});

it('Analytics disabled test', function (done) {
let disabled;
nobidAnalytics.processServerResponse(JSON.stringify({disabled: false}));
nobidAnalytics.processServerResponse(JSON.stringify({disabled: 0}));
disabled = nobidAnalytics.isAnalyticsDisabled();
expect(disabled).to.equal(false);
events.emit(constants.EVENTS.AUCTION_END, {auctionId: '1234567890'});
Expand All @@ -417,15 +425,15 @@ describe('NoBid Prebid Analytic', function () {
clock.tick(1000);
expect(server.requests).to.have.length(3);

nobidAnalytics.processServerResponse(JSON.stringify({disabled: true}));
nobidAnalytics.processServerResponse(JSON.stringify({disabled: 1}));
disabled = nobidAnalytics.isAnalyticsDisabled();
expect(disabled).to.equal(true);
events.emit(constants.EVENTS.AUCTION_END, {auctionId: '12345678902'});
clock.tick(5000);
expect(server.requests).to.have.length(3);

nobidAnalytics.retentionSeconds = 5;
nobidAnalytics.processServerResponse(JSON.stringify({disabled: true}));
nobidAnalytics.processServerResponse(JSON.stringify({disabled: 1}));
clock.tick(1000);
disabled = nobidAnalytics.isAnalyticsDisabled();
expect(disabled).to.equal(true);
Expand All @@ -437,6 +445,100 @@ describe('NoBid Prebid Analytic', function () {
});
});

describe('Analytics disabled event type test', function () {
beforeEach(function () {
sinon.stub(events, 'getEvents').returns([]);
clock = sinon.useFakeTimers(Date.now());
});

afterEach(function () {
events.getEvents.restore();
clock.restore();
});

after(function () {
nobidAnalytics.disableAnalytics();
});

it('Analytics disabled event type test', function (done) {
// Initialize adapter
const initOptions = { options: { siteId: SITE_ID } };
nobidAnalytics.enableAnalytics(initOptions);
adapterManager.enableAnalytics({ provider: 'nobid', options: initOptions });

let eventType = constants.EVENTS.AUCTION_END;
let disabled;
nobidAnalytics.processServerResponse(JSON.stringify({disabled: 0}));
disabled = nobidAnalytics.isAnalyticsDisabled();
expect(disabled).to.equal(false);
events.emit(eventType, {auctionId: '1234567890'});
clock.tick(1000);
expect(server.requests).to.have.length(1);
events.emit(eventType, {auctionId: '12345678901'});
clock.tick(1000);
expect(server.requests).to.have.length(2);

server.requests.length = 0;
expect(server.requests).to.have.length(0);

nobidAnalytics.processServerResponse(JSON.stringify({disabled_auctionEnd: 1}));
disabled = nobidAnalytics.isAnalyticsDisabled(eventType);
expect(disabled).to.equal(true);
events.emit(eventType, {auctionId: '1234567890'});
clock.tick(1000);
expect(server.requests).to.have.length(0);

server.requests.length = 0;

nobidAnalytics.processServerResponse(JSON.stringify({disabled_auctionEnd: 0}));
disabled = nobidAnalytics.isAnalyticsDisabled(eventType);
expect(disabled).to.equal(false);
events.emit(constants.EVENTS.AUCTION_END, {auctionId: '1234567890'});
clock.tick(1000);
expect(server.requests).to.have.length(1);

server.requests.length = 0;
expect(server.requests).to.have.length(0);

eventType = constants.EVENTS.BID_WON;
nobidAnalytics.processServerResponse(JSON.stringify({disabled_bidWon: 1}));
disabled = nobidAnalytics.isAnalyticsDisabled(eventType);
expect(disabled).to.equal(true);
events.emit(eventType, {bidderCode: 'nobid'});
clock.tick(1000);
expect(server.requests).to.have.length(0);

server.requests.length = 0;
expect(server.requests).to.have.length(0);

eventType = constants.EVENTS.AUCTION_END;
nobidAnalytics.processServerResponse(JSON.stringify({disabled: 1}));
disabled = nobidAnalytics.isAnalyticsDisabled(eventType);
expect(disabled).to.equal(true);
events.emit(eventType, {auctionId: '1234567890'});
clock.tick(1000);
expect(server.requests).to.have.length(0);

server.requests.length = 0;
expect(server.requests).to.have.length(0);

eventType = constants.EVENTS.AUCTION_END;
nobidAnalytics.processServerResponse(JSON.stringify({disabled_auctionEnd: 1, disabled_bidWon: 0}));
disabled = nobidAnalytics.isAnalyticsDisabled(eventType);
expect(disabled).to.equal(true);
events.emit(eventType, {auctionId: '1234567890'});
clock.tick(1000);
expect(server.requests).to.have.length(0);
disabled = nobidAnalytics.isAnalyticsDisabled(constants.EVENTS.BID_WON);
expect(disabled).to.equal(false);
events.emit(constants.EVENTS.BID_WON, {bidderCode: 'nobid'});
clock.tick(1000);
expect(server.requests).to.have.length(1);

done();
});
});

describe('NoBid Carbonizer', function () {
beforeEach(function () {
sinon.stub(events, 'getEvents').returns([]);
Expand All @@ -456,7 +558,8 @@ describe('NoBid Prebid Analytic', function () {
let active = nobidCarbonizer.isActive();
expect(active).to.equal(false);

active = nobidCarbonizer.isActive(JSON.stringify({carbonizer_active: false}));
nobidAnalytics.processServerResponse(JSON.stringify({carbonizer_active: false}));
active = nobidCarbonizer.isActive();
expect(active).to.equal(false);

nobidAnalytics.processServerResponse(JSON.stringify({carbonizer_active: true}));
Expand All @@ -469,12 +572,12 @@ describe('NoBid Prebid Analytic', function () {
let stored = nobidCarbonizer.getStoredLocalData();
expect(stored[nobidAnalytics.ANALYTICS_DATA_NAME]).to.contain(`{"carbonizer_active":true,"ts":`);
clock.tick(5000);
active = nobidCarbonizer.isActive(adunits, true);
active = nobidCarbonizer.isActive();
expect(active).to.equal(false);

nobidAnalytics.retentionSeconds = previousRetention;
nobidAnalytics.processServerResponse(JSON.stringify({carbonizer_active: true}));
active = nobidCarbonizer.isActive(adunits, true);
active = nobidCarbonizer.isActive();
expect(active).to.equal(true);

let adunits = [
Expand Down

0 comments on commit f7163de

Please sign in to comment.