Skip to content

Commit

Permalink
NextMillennium Bid Adapter: removed the use of the events module (#11141
Browse files Browse the repository at this point in the history
)

* added support for gpp consent string

* changed test for nextMillenniumBidAdapter

* added some tests

* added site.pagecat, site.content.cat and site.content.language to request

* lint fix

* formated code

* formated code

* formated code

* pachage-lock with prebid

* pachage-lock with prebid

* formatted code

* added device.sua, user.eids

* formatted

* fixed tests

* fixed bug functio getSua

* deleted deprecated code wurl

* removed the use of the events module

* added parameters w and h for imp[].banner objecct
  • Loading branch information
mhlm authored Mar 6, 2024
1 parent 1a3a6ed commit 06bf1e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 142 deletions.
166 changes: 26 additions & 140 deletions modules/nextMillenniumBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
getWindowTop,
isArray,
isStr,
logMessage,
parseGPTSingleSizeArrayToRtbSize,
parseUrl,
triggerPixel,
Expand All @@ -18,7 +17,6 @@ import {getGlobal} from '../src/prebidGlobal.js';
import CONSTANTS from '../src/constants.json';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
import * as events from '../src/events.js';

import {registerBidder} from '../src/adapters/bidderFactory.js';
import {getRefererInfo} from '../src/refererDetection.js';
Expand Down Expand Up @@ -55,15 +53,6 @@ const ALLOWED_ORTB2_PARAMETERS = [
'user.keywords',
];

const sendingDataStatistic = initSendingDataStatistic();
events.on(CONSTANTS.EVENTS.AUCTION_INIT, auctionInitHandler);

const EXPIRENCE_WURL = 20 * 60000;
const wurlMap = {};
cleanWurl();

events.on(CONSTANTS.EVENTS.BID_WON, bidWonHandler);

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
Expand All @@ -79,7 +68,7 @@ export const spec = {
const requests = [];
window.nmmRefreshCounts = window.nmmRefreshCounts || {};

_each(validBidRequests, function(bid) {
_each(validBidRequests, (bid) => {
window.nmmRefreshCounts[bid.adUnitCode] = window.nmmRefreshCounts[bid.adUnitCode] || 0;
const id = getPlacementId(bid);
const auctionId = bid.auctionId;
Expand Down Expand Up @@ -135,6 +124,8 @@ export const spec = {
params,
auctionId,
});

this.getUrlPixelMetric(CONSTANTS.EVENTS.BID_REQUESTED, bid);
});

return requests;
Expand All @@ -148,11 +139,6 @@ export const spec = {
_each(resp.bid, (bid) => {
const requestId = bidRequest.bidId;
const params = bidRequest.params;
const auctionId = bidRequest.auctionId;
const wurl = deepAccess(bid, 'ext.prebid.events.win');

// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
addWurl({auctionId, requestId, wurl});

const {ad, adUrl, vastUrl, vastXml} = getAd(bid);

Expand Down Expand Up @@ -182,6 +168,8 @@ export const spec = {
};

bidResponses.push(bidResponse);

this.getUrlPixelMetric(CONSTANTS.EVENTS.BID_RESPONSE, bid);
});
});

Expand Down Expand Up @@ -215,6 +203,16 @@ export const spec = {
},

getUrlPixelMetric(eventName, bid) {
const disabledSending = !!config.getBidderConfig()?.nextMillennium?.disabledSendingStatisticData;
if (disabledSending) return;

const url = this._getUrlPixelMetric(eventName, bid);
if (!url) return;

triggerPixel(url);
},

_getUrlPixelMetric(eventName, bid) {
const bidder = bid.bidder || bid.bidderCode;
if (bidder != BIDDER_CODE) return;

Expand Down Expand Up @@ -248,6 +246,12 @@ export const spec = {

return url;
},

onTimeout(bids) {
for (const bid of bids) {
this.getUrlPixelMetric(CONSTANTS.EVENTS.BID_TIMEOUT, bid);
};
},
};

export function getImp(bid, id, mediaTypes) {
Expand All @@ -267,8 +271,12 @@ export function getImp(bid, id, mediaTypes) {
if (banner.bidfloorcur) imp.bidfloorcur = banner.bidfloorcur;
if (banner.bidfloor) imp.bidfloor = banner.bidfloor;

const format = (banner.data?.sizes || []).map(s => { return {w: s[0], h: s[1]} })
const {w, h} = (format[0] || {})
imp.banner = {
format: (banner.data?.sizes || []).map(s => { return {w: s[0], h: s[1]} }),
w,
h,
format,
};
};

Expand Down Expand Up @@ -487,126 +495,4 @@ function getSua() {
};
}

function getKeyWurl({auctionId, requestId}) {
return `${auctionId}-${requestId}`;
}

function addWurl({wurl, requestId, auctionId}) {
if (!wurl) return;

const expirence = Date.now() + EXPIRENCE_WURL;
const key = getKeyWurl({auctionId, requestId});
wurlMap[key] = {wurl, expirence};
}

function removeWurl({auctionId, requestId}) {
const key = getKeyWurl({auctionId, requestId});
delete wurlMap[key];
}

function getWurl({auctionId, requestId}) {
const key = getKeyWurl({auctionId, requestId});
return wurlMap[key] && wurlMap[key].wurl;
}

function bidWonHandler(bid) {
const {auctionId, requestId} = bid;
const wurl = getWurl({auctionId, requestId});
if (wurl) {
logMessage(`(nextmillennium) Invoking image pixel for wurl on BID_WIN: "${wurl}"`);
triggerPixel(wurl);
removeWurl({auctionId, requestId});
};
}

function auctionInitHandler() {
sendingDataStatistic.initEvents();
}

function cleanWurl() {
const dateNow = Date.now();
Object.keys(wurlMap).forEach(key => {
if (dateNow >= wurlMap[key].expirence) {
delete wurlMap[key];
};
});

setTimeout(cleanWurl, 60000);
}

function initSendingDataStatistic() {
class SendingDataStatistic {
eventNames = [
CONSTANTS.EVENTS.BID_TIMEOUT,
CONSTANTS.EVENTS.BID_RESPONSE,
CONSTANTS.EVENTS.BID_REQUESTED,
CONSTANTS.EVENTS.NO_BID,
];

disabledSending = false;
enabledSending = false;
eventHendlers = {};

initEvents() {
this.disabledSending = !!config.getBidderConfig()?.nextMillennium?.disabledSendingStatisticData;
if (this.disabledSending) {
this.removeEvents();
} else {
this.createEvents();
};
}

createEvents() {
if (this.enabledSending) return;

this.enabledSending = true;
for (let eventName of this.eventNames) {
if (!this.eventHendlers[eventName]) {
this.eventHendlers[eventName] = this.eventHandler(eventName);
};

events.on(eventName, this.eventHendlers[eventName]);
};
}

removeEvents() {
if (!this.enabledSending) return;

this.enabledSending = false;
for (let eventName of this.eventNames) {
if (!this.eventHendlers[eventName]) continue;

events.off(eventName, this.eventHendlers[eventName]);
};
}

eventHandler(eventName) {
const eventHandlerFunc = this.getEventHandler(eventName);
if (eventName == CONSTANTS.EVENTS.BID_TIMEOUT) {
return bids => {
if (this.disabledSending || !Array.isArray(bids)) return;

for (let bid of bids) {
eventHandlerFunc(bid);
};
}
};

return eventHandlerFunc;
}

getEventHandler(eventName) {
return bid => {
if (this.disabledSending) return;

const url = spec.getUrlPixelMetric(eventName, bid);
if (!url) return;
triggerPixel(url);
};
}
};

return new SendingDataStatistic();
}

registerBidder(spec);
4 changes: 2 additions & 2 deletions test/spec/modules/nextMillenniumBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('nextMillenniumBidAdapterTests', () => {
bidfloorcur: 'EUR',
bidfloor: 1.11,
ext: {prebid: {storedrequest: {id: '123'}}},
banner: {format: [{w: 300, h: 250}, {w: 320, h: 250}]},
banner: {w: 300, h: 250, format: [{w: 300, h: 250}, {w: 320, h: 250}]},
},
},

Expand Down Expand Up @@ -902,7 +902,7 @@ describe('nextMillenniumBidAdapterTests', () => {
];

for (let {eventName, bid, expected} of dataForTests) {
const url = spec.getUrlPixelMetric(eventName, bid);
const url = spec._getUrlPixelMetric(eventName, bid);
expect(url).to.equal(expected);
};
})
Expand Down

0 comments on commit 06bf1e4

Please sign in to comment.