Skip to content

Commit

Permalink
PE-110: Add user sync logic to the Prebid Adapter (#3)
Browse files Browse the repository at this point in the history
* PE-110: add user sync logic

* PE-110: update userSync url

* PE-110: check if iframe is enabled before setting params
  • Loading branch information
PavloMalashnyak authored Oct 26, 2023
1 parent 79a0a94 commit 610b6c2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
41 changes: 41 additions & 0 deletions modules/BTBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ortbConverter } from '../libraries/ortbConverter/converter.js';
const BIDDER_CODE = 'blockthrough';
const GVLID = 815;
const ENDPOINT_URL = 'https://pbs.btloader.com/openrtb2/auction';
const SYNC_URL = 'https://cdn.btloader.com/user_sync.html';

const CONVERTER = ortbConverter({
context: {
Expand Down Expand Up @@ -121,13 +122,53 @@ function interpretResponse(serverResponse, request) {
}).bids;
}

/**
* Generates user synchronization data based on provided options and consents.
*
* @param {Object} syncOptions - Synchronization options.
* @param {Object[]} serverResponses - An array of server responses.
* @param {Object} gdprConsent - GDPR consent information.
* @param {string} uspConsent - US Privacy consent string.
* @param {Object} gppConsent - Google Publisher Policies (GPP) consent information.
* @returns {Object[]} An array of user synchronization objects.
*/
function getUserSyncs(
syncOptions,
serverResponses,
gdprConsent,
uspConsent,
gppConsent
) {
let syncs = [];
const syncUrl = new URL(SYNC_URL);

if (syncOptions.iframeEnabled) {
if (gdprConsent) {
syncUrl.searchParams.set('gdpr', Number(gdprConsent.gdprApplies));
syncUrl.searchParams.set('gdpr_consent', gdprConsent.consentString);
}
if (gppConsent) {
syncUrl.searchParams.set('gpp', gppConsent.gppString);
syncUrl.searchParams.set('gpp_sid', gppConsent.applicableSections);
}
if (uspConsent) {
syncUrl.searchParams.set('us_privacy', uspConsent);
}

syncs.push({ type: 'iframe', url: syncUrl.href });
}

return syncs;
}

export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
};

registerBidder(spec);
45 changes: 45 additions & 0 deletions test/spec/modules/BTBidAdapte_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,49 @@ describe('BT Bid Adapter', () => {
expect(bids).to.deep.equal(expectedBids);
});
});

describe('getUserSyncs', () => {
const SYNC_URL = 'https://cdn.btloader.com/user_sync.html';

it('should return an empty array if no sync options are provided', () => {
const syncs = spec.getUserSyncs({}, [], null, null, null);

expect(syncs).to.deep.equal([]);
});

it('should include consent parameters in sync URL if they are provided', () => {
const gdprConsent = {
gdprApplies: true,
consentString: 'GDPRConsentString123',
};
const gppConsent = {
gppString: 'GPPString123',
applicableSections: ['sectionA'],
};
const us_privacy = '1YNY';
const expectedSyncUrl = `${SYNC_URL}?gdpr=1&gdpr_consent=${gdprConsent.consentString}&gpp=${gppConsent.gppString}&gpp_sid=sectionA&us_privacy=${us_privacy}`;

const syncs = spec.getUserSyncs(
{ iframeEnabled: true },
[],
gdprConsent,
us_privacy,
gppConsent
);

expect(syncs).to.deep.equal([{ type: 'iframe', url: expectedSyncUrl }]);
});

it('should not include any consent parameters if no consents are provided', () => {
const syncs = spec.getUserSyncs(
{ iframeEnabled: true },
[],
null,
null,
null
);

expect(syncs).to.deep.equal([{ type: 'iframe', url: SYNC_URL }]);
});
});
});

0 comments on commit 610b6c2

Please sign in to comment.