Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 6 additions & 9 deletions modules/livewrappedAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const ADRENDERFAILEDSENT = 16;
let initOptions;
const prebidGlobal = getGlobal();
export const BID_WON_TIMEOUT = 500;
const CACHE_CLEANUP_DELAY = BID_WON_TIMEOUT * 3;

const cache = {
auctions: {}
Expand Down Expand Up @@ -159,6 +158,7 @@ const livewrappedAnalyticsAdapter = Object.assign(adapter({ EMPTYURL, ANALYTICST
// save the base class function
livewrappedAnalyticsAdapter.originEnableAnalytics = livewrappedAnalyticsAdapter.enableAnalytics;
livewrappedAnalyticsAdapter.allRequestEvents = [];
const baseClearAllAuctions = prebidGlobal.clearAllAuctions;

// override enableAnalytics so we can get access to the config passed in from the page
livewrappedAnalyticsAdapter.enableAnalytics = function (config) {
Expand Down Expand Up @@ -190,12 +190,6 @@ livewrappedAnalyticsAdapter.sendEvents = function() {
}

ajax(initOptions.endpoint || URL, undefined, JSON.stringify(events), { method: 'POST' });

setTimeout(() => {
sentRequests.auctionIds.forEach(id => {
delete cache.auctions[id];
});
}, CACHE_CLEANUP_DELAY);
};

function getMediaTypeEnum(mediaType) {
Expand Down Expand Up @@ -429,6 +423,11 @@ function getbidAdUnits() {
return bidAdUnits;
}

prebidGlobal.clearAllAuctions = function() {
cache.auctions = {};
baseClearAllAuctions();
Comment on lines +426 to +428
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore automatic eviction of sent auction cache

This change makes cache.auctions cleanup depend entirely on pbjs.clearAllAuctions(), but that API is optional and typically not called in normal auction flows. After removing the timed cleanup in sendEvents, every completed auction remains in memory indefinitely, so sendEvents keeps iterating an ever-growing cache on subsequent auctions and can degrade long-lived pages (memory and CPU) even when no new data from old auctions is sent.

Useful? React with 👍 / 👎.

}

adapterManager.registerAnalyticsAdapter({
adapter: livewrappedAnalyticsAdapter,
code: 'livewrapped'
Expand All @@ -438,6 +437,4 @@ export function getAuctionCache() {
return cache.auctions;
}

export { CACHE_CLEANUP_DELAY };

export default livewrappedAnalyticsAdapter;
27 changes: 25 additions & 2 deletions test/spec/modules/livewrappedAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AD_RENDER_FAILED_REASON, EVENTS, STATUS } from 'src/constants.js';
import { config } from 'src/config.js';
import { server } from 'test/mocks/xhr.js';
import { setConfig } from 'modules/currency.js';
import { getGlobal } from '../../../src/prebidGlobal.js';

const events = require('src/events');
const utils = require('src/utils');
Expand Down Expand Up @@ -377,12 +378,34 @@ describe('Livewrapped analytics adapter', function () {
expect(message).to.deep.equal(ANALYTICS_MESSAGE);
});

it('should clear auction cache after sending events', function () {
it('should clear auction cache when pbjs.clearAllAuctions is called', function () {
performStandardAuction();
performSecondAuction();

clock.tick(BID_WON_TIMEOUT + CACHE_CLEANUP_DELAY + 100);
expect(Object.keys(getAuctionCache()).length).to.equal(2);

getGlobal().clearAllAuctions();

expect(Object.keys(getAuctionCache()).length).to.equal(0);

function performSecondAuction() {
events.emit(AUCTION_INIT, {
'auctionId': '35c6d7f5-699a-4bfc-87c9-996f915341fa',
});
events.emit(BID_REQUESTED, {
'bidder': 'livewrapped',
'auctionId': '35c6d7f5-699a-4bfc-87c9-996f915341fa',
'bidderRequestId': '2be65d7958826a',
'bids': [
{
'bidder': 'livewrapped',
'adUnitCode': 'panorama_d_1',
'bidId': '3ecff0db240757',
}
],
'start': 1519149562216
});
}
});

it('should send batched message without BID_WON AND AD_RENDER_FAILED if necessary and further BID_WON and AD_RENDER_FAILED events individually', function () {
Expand Down
Loading