Skip to content

PBS Bid Adapter : add BEFORE_PBS_HTTP event #12889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2025
Merged
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
19 changes: 11 additions & 8 deletions modules/prebidServerBidAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,17 @@ export const processPBSRequest = hook('sync', function (s2sBidRequest, bidReques
.filter(uniques);

const request = s2sBidRequest.metrics.measureTime('buildRequests', () => buildPBSRequest(s2sBidRequest, bidRequests, adUnits, requestedBidders));
const requestJson = request && JSON.stringify(request);
logInfo('BidRequest: ' + requestJson);
const endpointUrl = getMatchingConsentUrl(s2sBidRequest.s2sConfig.endpoint, gdprConsent);
const customHeaders = s2sBidRequest?.s2sConfig?.customHeaders ?? {};
if (request && requestJson && endpointUrl) {
const requestData = {
endpointUrl: getMatchingConsentUrl(s2sBidRequest.s2sConfig.endpoint, gdprConsent),
requestJson: request && JSON.stringify(request),
customHeaders: s2sBidRequest?.s2sConfig?.customHeaders ?? {},
};
events.emit(EVENTS.BEFORE_PBS_HTTP, requestData)
logInfo('BidRequest: ' + requestData);
if (request && requestData.requestJson && requestData.endpointUrl) {
const networkDone = s2sBidRequest.metrics.startTiming('net');
ajax(
endpointUrl,
requestData.endpointUrl,
{
success: function (response) {
networkDone();
Expand All @@ -537,12 +540,12 @@ export const processPBSRequest = hook('sync', function (s2sBidRequest, bidReques
onError.apply(this, arguments);
}
},
requestJson,
requestData.requestJson,
{
contentType: 'text/plain',
withCredentials: true,
browsingTopics: isActivityAllowed(ACTIVITY_TRANSMIT_UFPD, s2sActivityParams(s2sBidRequest.s2sConfig)),
customHeaders
customHeaders: requestData.customHeaders
}
);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const EVENTS = {
PAAPI_BID: 'paapiBid',
PAAPI_NO_BID: 'paapiNoBid',
PAAPI_ERROR: 'paapiError',
BEFORE_PBS_HTTP: 'beforePBSHttp',
BROWSI_INIT: 'browsiInit',
BROWSI_DATA: 'browsiData',
};
Expand Down
26 changes: 20 additions & 6 deletions test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3270,8 +3270,8 @@ describe('S2S Adapter', function () {
adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
server.requests[0].respond(200, {}, JSON.stringify(RESPONSE_OPENRTB));

sinon.assert.calledOnce(events.emit);
const event = events.emit.firstCall.args;
sinon.assert.calledTwice(events.emit);
const event = events.emit.secondCall.args;
expect(event[0]).to.equal(EVENTS.BIDDER_DONE);
expect(event[1].bids[0]).to.have.property('serverResponseTimeMs', 8);

Expand Down Expand Up @@ -3302,7 +3302,7 @@ describe('S2S Adapter', function () {
const responding = deepClone(nonbidResponse);
Object.assign(responding.ext.seatnonbid, [{auctionId: 2}])
server.requests[0].respond(200, {}, JSON.stringify(responding));
const event = events.emit.secondCall.args;
const event = events.emit.thirdCall.args;
expect(event[0]).to.equal(EVENTS.SEAT_NON_BID);
expect(event[1].seatnonbid[0]).to.have.property('auctionId', 2);
expect(event[1].requestedBidders).to.deep.equal(['appnexus']);
Expand All @@ -3319,7 +3319,7 @@ describe('S2S Adapter', function () {
const responding = deepClone(nonbidResponse);
Object.assign(responding.ext.seatnonbid, [{auctionId: 2}])
server.requests[0].respond(200, {}, JSON.stringify(responding));
const event = events.emit.thirdCall.args;
const event = events.emit.getCall(3).args;
expect(event[0]).to.equal(EVENTS.PBS_ANALYTICS);
expect(event[1].seatnonbid[0]).to.have.property('auctionId', 2);
expect(event[1].requestedBidders).to.deep.equal(['appnexus']);
Expand All @@ -3336,12 +3336,26 @@ describe('S2S Adapter', function () {
const responding = deepClone(atagResponse);
Object.assign(responding.ext.prebid.analytics.tags, ['stuff'])
server.requests[0].respond(200, {}, JSON.stringify(responding));
const event = events.emit.secondCall.args;
const event = events.emit.thirdCall.args;
expect(event[0]).to.equal(EVENTS.PBS_ANALYTICS);
expect(event[1].atag[0]).to.deep.equal('stuff');
expect(event[1].response).to.deep.equal(responding);
});

it('emits the BEFORE_PBS_HTTP event and captures responses', function () {
config.setConfig({ CONFIG });

adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax);
server.requests[0].respond(200, {}, JSON.stringify(RESPONSE_OPENRTB));

sinon.assert.calledTwice(events.emit);
const event = events.emit.firstCall.args;
expect(event[0]).to.equal(EVENTS.BEFORE_PBS_HTTP);
expect(event[1]).to.have.property('requestJson', server.requests[0].requestBody);
expect(event[1]).to.have.property('endpointUrl', CONFIG.endpoint.p1Consent);
expect(event[1].customHeaders).to.deep.equal({});
});

it('respects defaultTtl', function () {
const s2sConfig = Object.assign({}, CONFIG, {
defaultTtl: 30
Expand All @@ -3353,7 +3367,7 @@ describe('S2S Adapter', function () {
adapter.callBids(s2sBidRequest, BID_REQUESTS, addBidResponse, done, ajax);
server.requests[0].respond(200, {}, JSON.stringify(RESPONSE_OPENRTB));

sinon.assert.calledOnce(events.emit);
sinon.assert.calledTwice(events.emit);
const event = events.emit.firstCall.args;
sinon.assert.calledOnce(addBidResponse);
const response = addBidResponse.firstCall.args[1];
Expand Down