Skip to content

Commit

Permalink
contxtfulBidAdapter: revamp the sampling of events (prebid#12466)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhtml authored Nov 20, 2024
1 parent 24cf886 commit 09f9632
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
27 changes: 13 additions & 14 deletions modules/contxtfulBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ const getSamplingRate = (bidderConfig, eventType) => {
};

// Handles the logging of events
const logEvent = (eventType, data, options = {}) => {
const {
samplingEnabled = false,
} = options;

const logEvent = (eventType, data) => {
try {
// Log event
logInfo(BIDDER_CODE, `[${eventType}] ${JSON.stringify(data)}`);
Expand All @@ -165,12 +161,15 @@ const logEvent = (eventType, data, options = {}) => {
const {version, customer} = extractParameters(bidderConfig);

// Sampled monitoring
if (samplingEnabled) {
const shouldSampleDice = Math.random();
if (['onBidBillable', 'onAdRenderSucceeded'].includes(eventType)) {
const randomNumber = Math.random();
const samplingRate = getSamplingRate(bidderConfig, eventType);
if (shouldSampleDice >= samplingRate) {
if (randomNumber >= samplingRate) {
return; // Don't sample
}
} else if (!['onTimeout', 'onBidderError', 'onBidWon'].includes(eventType)) {
// Unsupported event type.
return;
}

const payload = { type: eventType, data };
Expand Down Expand Up @@ -206,12 +205,12 @@ export const spec = {
buildRequests,
interpretResponse,
getUserSyncs,
onBidWon: function(bid, options) { logEvent('onBidWon', bid, { samplingEnabled: false, ...options }); },
onBidBillable: function(bid, options) { logEvent('onBidBillable', bid, { samplingEnabled: false, ...options }); },
onAdRenderSucceeded: function(bid, options) { logEvent('onAdRenderSucceeded', bid, { samplingEnabled: false, ...options }); },
onSetTargeting: function(bid, options) { },
onTimeout: function(timeoutData, options) { logEvent('onTimeout', timeoutData, { samplingEnabled: true, ...options }); },
onBidderError: function(args, options) { logEvent('onBidderError', args, { samplingEnabled: true, ...options }); },
onBidWon: function(bid) { logEvent('onBidWon', bid); },
onBidBillable: function(bid) { logEvent('onBidBillable', bid); },
onAdRenderSucceeded: function(bid) { logEvent('onAdRenderSucceeded', bid); },
onSetTargeting: function(bid) { },
onTimeout: function(timeoutData) { logEvent('onTimeout', timeoutData); },
onBidderError: function({ error, bidderRequest }) { logEvent('onBidderError', { error, bidderRequest }); },
};

registerBidder(spec);
34 changes: 11 additions & 23 deletions test/spec/modules/contxtfulBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,10 @@ describe('contxtful bid adapter', function () {
expect(userSyncs2).to.have.lengthOf(0);
});

describe('on timeout callback', () => {
it('will never call server if sampling is 0 with sendBeacon available', () => {
describe('onTimeout callback', () => {
it('will always call server with sendBeacon available', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 0.0}},
});

const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(true);
const ajaxStub = sandbox.stub(ajax, 'ajax');
expect(spec.onTimeout({'customData': 'customvalue'})).to.not.throw;
expect(beaconStub.called).to.be.false;
expect(ajaxStub.called).to.be.false;
});

it('will always call server if sampling is 1 with sendBeacon available', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 1.0}},
contxtful: {customer: CUSTOMER, version: VERSION},
});

const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(true);
Expand All @@ -425,9 +413,9 @@ describe('contxtful bid adapter', function () {
expect(ajaxStub.called).to.be.false;
});

it('will always call server if sampling is 1 with sendBeacon not available', () => {
it('will always call server with sendBeacon not available', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onTimeout': 1.0}},
contxtful: {customer: CUSTOMER, version: VERSION},
});

const ajaxStub = sandbox.stub(ajax, 'ajax');
Expand All @@ -440,9 +428,9 @@ describe('contxtful bid adapter', function () {
});

describe('on onBidderError callback', () => {
it('will always call server if sampling is 1', () => {
it('will always call server', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION, 'sampling': {'onBidderError': 1.0}},
contxtful: {customer: CUSTOMER, version: VERSION},
});

const ajaxStub = sandbox.stub(ajax, 'ajax');
Expand All @@ -468,9 +456,9 @@ describe('contxtful bid adapter', function () {
});

describe('on onBidBillable callback', () => {
it('will always call server', () => {
it('will always call server when sampling rate is configured to be 1.0', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION},
contxtful: {customer: CUSTOMER, version: VERSION, sampling: {onBidBillable: 1.0}},
});
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
Expand All @@ -481,9 +469,9 @@ describe('contxtful bid adapter', function () {
});

describe('on onAdRenderSucceeded callback', () => {
it('will always call server', () => {
it('will always call server when sampling rate is configured to be 1.0', () => {
config.setConfig({
contxtful: {customer: CUSTOMER, version: VERSION},
contxtful: {customer: CUSTOMER, version: VERSION, sampling: {onAdRenderSucceeded: 1.0}},
});
const ajaxStub = sandbox.stub(ajax, 'ajax');
const beaconStub = sandbox.stub(ajax, 'sendBeacon').returns(false);
Expand Down

0 comments on commit 09f9632

Please sign in to comment.