From 4faa3ec4b6f990ae793ae178e123b65ef6714e17 Mon Sep 17 00:00:00 2001 From: Marco Masotti Date: Tue, 16 Oct 2018 19:27:54 +0200 Subject: [PATCH] improves additional data passed to the handler in AuctionInit and AuctionEnd events (#3168) --- src/auction.js | 30 ++++++++++++++++++++++-------- test/spec/unit/pbjs_api_spec.js | 4 ++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/auction.js b/src/auction.js index b9f3574ed318..50b681d61a8f 100644 --- a/src/auction.js +++ b/src/auction.js @@ -96,6 +96,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels}) let _bidderRequests = []; let _bidsReceived = []; let _auctionStart; + let _auctionEnd; let _auctionId = utils.generateUUID(); let _auctionStatus; let _callback = callback; @@ -106,6 +107,22 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels}) function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests) }; function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); } + function getProperties() { + return { + auctionId: _auctionId, + auctionStart: _auctionStart, + auctionEnd: _auctionEnd, + auctionStatus: _auctionStatus, + adUnits: _adUnits, + adUnitCodes: _adUnitCodes, + labels: _labels, + bidderRequests: _bidderRequests, + bidsReceived: _bidsReceived, + winningBids: _winningBids, + timeout: _timeout + }; + } + function startAuctionTimer() { const timedOut = true; const timeoutCallback = executeCallback.bind(null, timedOut); @@ -129,10 +146,12 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels}) } } - events.emit(CONSTANTS.EVENTS.AUCTION_END, {auctionId: _auctionId}); - try { _auctionStatus = AUCTION_COMPLETED; + _auctionEnd = Date.now(); + + events.emit(CONSTANTS.EVENTS.AUCTION_END, getProperties()); + const adUnitCodes = _adUnitCodes; const bids = _bidsReceived .filter(adUnitsFilter.bind(this, adUnitCodes)) @@ -185,12 +204,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels}) _auctionStatus = AUCTION_IN_PROGRESS; - const auctionInit = { - timestamp: _auctionStart, - auctionId: _auctionId, - timeout: _timeout - }; - events.emit(CONSTANTS.EVENTS.AUCTION_INIT, auctionInit); + events.emit(CONSTANTS.EVENTS.AUCTION_INIT, getProperties()); let callbacks = auctionCallbacks(auctionDone, this); let boundObj = { diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index f113b8f0f450..1a791f8dd51b 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -1591,8 +1591,8 @@ describe('Unit: Prebid Module', function () { describe('emit', function () { it('should be able to emit event without arguments', function () { var spyEventsEmit = sinon.spy(events, 'emit'); - events.emit(CONSTANTS.EVENTS.AUCTION_END); - assert.ok(spyEventsEmit.calledWith('auctionEnd')); + events.emit(CONSTANTS.EVENTS.REQUEST_BIDS); + assert.ok(spyEventsEmit.calledWith('requestBids')); events.emit.restore(); }); });