Skip to content

Commit

Permalink
define split between exploratory and non exploratory sides of the det…
Browse files Browse the repository at this point in the history
…erministic sampling hash (#11104)
  • Loading branch information
jbogp authored Feb 19, 2024
1 parent cd328b6 commit aaf2951
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 17 additions & 3 deletions modules/greenbidsAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ export const BIDDER_STATUS = {

const analyticsOptions = {};

export const isSampled = function(greenbidsId, samplingRate) {
export const isSampled = function(greenbidsId, samplingRate, exploratorySamplingSplit) {
if (samplingRate < 0 || samplingRate > 1) {
logWarn('Sampling rate must be between 0 and 1');
return true;
}
const exploratorySamplingRate = samplingRate * exploratorySamplingSplit;
const throttledSamplingRate = samplingRate * (1.0 - exploratorySamplingSplit);
const hashInt = parseInt(greenbidsId.slice(-4), 16);
return hashInt < samplingRate * (0xFFFF + 1);
const isPrimarySampled = hashInt < exploratorySamplingRate * (0xFFFF + 1);
if (isPrimarySampled) return true;
const isExtraSampled = hashInt >= (1 - throttledSamplingRate) * (0xFFFF + 1);
return isExtraSampled;
}

export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER, analyticsType}), {

cachedAuctions: {},
exploratorySamplingSplit: 0.9,

initConfig(config) {
analyticsOptions.options = deepClone(config.options);
Expand Down Expand Up @@ -68,6 +74,14 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER
analyticsOptions.options.greenbidsSampling = 1;
}

/**
* Add optional debug parameter to override exploratorySamplingSplit
*/
if (typeof analyticsOptions.options.exploratorySamplingSplit === 'number') {
logInfo('Greenbids Analytics: Overriding "exploratorySamplingSplit".');
this.exploratorySamplingSplit = analyticsOptions.options.exploratorySamplingSplit;
}

analyticsOptions.pbuid = config.options.pbuid
analyticsOptions.server = ANALYTICS_SERVER;

Expand Down Expand Up @@ -172,7 +186,7 @@ export const greenbidsAnalyticsAdapter = Object.assign(adapter({ANALYTICS_SERVER
logInfo("Couldn't find Greenbids RTD info, assuming analytics only");
cachedAuction.greenbidsId = generateUUID();
}
cachedAuction.isSampled = isSampled(cachedAuction.greenbidsId, analyticsOptions.options.greenbidsSampling);
cachedAuction.isSampled = isSampled(cachedAuction.greenbidsId, analyticsOptions.options.greenbidsSampling, this.exploratorySamplingSplit);
},
handleAuctionEnd(auctionEndArgs) {
const cachedAuction = this.getCachedAuction(auctionEndArgs.auctionId);
Expand Down
16 changes: 12 additions & 4 deletions test/spec/modules/greenbidsAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,24 @@ describe('Greenbids Prebid AnalyticsAdapter Testing', function () {

describe('isSampled', function() {
it('should return true for invalid sampling rates', function() {
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', -1)).to.be.true;
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 1.2)).to.be.true;
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', -1, 0.0)).to.be.true;
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 1.2, 0.0)).to.be.true;
});

it('should return determinist falsevalue for valid sampling rate given the predifined id and rate', function() {
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.0001)).to.be.false;
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.0001, 0.0)).to.be.false;
});

it('should return determinist true value for valid sampling rate given the predifined id and rate', function() {
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.9999)).to.be.true;
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.9999, 0.0)).to.be.true;
});

it('should return determinist true value for valid sampling rate given the predifined id and rate when we split to non exploration first', function() {
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.9999, 0.0, 1.0)).to.be.true;
});

it('should return determinist false value for valid sampling rate given the predifined id and rate when we split to non exploration first', function() {
expect(isSampled('ce1f3692-632c-4cfd-9e40-0c2ad625ec56', 0.0001, 0.0, 1.0)).to.be.false;
});
});
});

0 comments on commit aaf2951

Please sign in to comment.