Skip to content
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

Reset Digital Bid Adapter: refactoring usersync method #10673

Merged
merged 2 commits into from
Nov 2, 2023
Merged
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
122 changes: 69 additions & 53 deletions modules/resetdigitalBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { timestamp, deepAccess, isStr, deepClone } from '../src/utils.js';
import { getOrigin } from '../libraries/getOrigin/index.js';
import { config } from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER} from '../src/mediaTypes.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';

const BIDDER_CODE = 'resetdigital';
const CURRENCY = 'USD';

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [ 'banner', 'video' ],
isBidRequestValid: function(bid) {
return (!!(bid.params.pubId || bid.params.zoneId));
supportedMediaTypes: ['banner', 'video'],
isBidRequestValid: function (bid) {
return !!(bid.params.pubId || bid.params.zoneId);
},
buildRequests: function(validBidRequests, bidderRequest) {
let stack = (bidderRequest.refererInfo &&
bidderRequest.refererInfo.stack ? bidderRequest.refererInfo.stack
: [])

let spb = (config.getConfig('userSync') && config.getConfig('userSync').syncsPerBidder)
? config.getConfig('userSync').syncsPerBidder : 5
buildRequests: function (validBidRequests, bidderRequest) {
let stack =
bidderRequest.refererInfo && bidderRequest.refererInfo.stack
? bidderRequest.refererInfo.stack
: [];

let spb =
config.getConfig('userSync') &&
config.getConfig('userSync').syncsPerBidder
? config.getConfig('userSync').syncsPerBidder
: 5;

const payload = {
start_time: timestamp(),
Expand All @@ -29,19 +33,19 @@ export const spec = {
iframe: !bidderRequest.refererInfo.reachedTop,
// TODO: the last element in refererInfo.stack is window.location.href, that's unlikely to have been the intent here
url: stack && stack.length > 0 ? [stack.length - 1] : null,
https: (window.location.protocol === 'https:'),
https: window.location.protocol === 'https:',
// TODO: is 'page' the right value here?
referrer: bidderRequest.refererInfo.page
referrer: bidderRequest.refererInfo.page,
},
imps: [],
user_ids: validBidRequests[0].userId,
sync_limit: spb
sync_limit: spb,
};

if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdpr = {
applies: bidderRequest.gdprConsent.gdprApplies,
consent: bidderRequest.gdprConsent.consentString
consent: bidderRequest.gdprConsent.consentString,
};
}

Expand All @@ -50,10 +54,16 @@ export const spec = {
}

function getOrtb2Keywords(ortb2Obj) {
const fields = ['site.keywords', 'site.content.keywords', 'user.keywords', 'app.keywords', 'app.content.keywords'];
const fields = [
'site.keywords',
'site.content.keywords',
'user.keywords',
'app.keywords',
'app.content.keywords',
];
let result = [];

fields.forEach(path => {
fields.forEach((path) => {
let keyStr = deepAccess(ortb2Obj, path);
if (isStr(keyStr)) result.push(keyStr);
});
Expand All @@ -79,18 +89,26 @@ export const spec = {
const floorInfo = req.getFloor({
currency: CURRENCY,
mediaType: BANNER,
size: '*'
size: '*',
});
if (typeof floorInfo === 'object' && floorInfo.currency === CURRENCY && !isNaN(parseFloat(floorInfo.floor))) {
if (
typeof floorInfo === 'object' &&
floorInfo.currency === CURRENCY &&
!isNaN(parseFloat(floorInfo.floor))
) {
bidFloor = parseFloat(floorInfo.floor);
bidFloorCur = CURRENCY;
}
}

// get param kewords (if it exists)
let paramsKeywords = req.params.keywords ? req.params.keywords.split(',') : [];
let paramsKeywords = req.params.keywords
? req.params.keywords.split(',')
: [];
// merge all keywords
let keywords = ortb2KeywordsList.concat(paramsKeywords).concat(metaKeywords);
let keywords = ortb2KeywordsList
.concat(paramsKeywords)
.concat(metaKeywords);

payload.imps.push({
pub_id: req.params.pubId,
Expand All @@ -110,32 +128,32 @@ export const spec = {
sizes: req.sizes,
force_bid: req.params.forceBid,
coppa: config.getConfig('coppa') === true ? 1 : 0,
media_types: deepAccess(req, 'mediaTypes')
media_types: deepAccess(req, 'mediaTypes'),
});
}

let params = validBidRequests[0].params
let url = params.endpoint ? params.endpoint : '//ads.resetsrv.com'
let params = validBidRequests[0].params;
let url = params.endpoint ? params.endpoint : '//ads.resetsrv.com';
return {
method: 'POST',
url: url,
data: JSON.stringify(payload),
bids: validBidRequests
bids: validBidRequests,
};
},
interpretResponse: function(serverResponse, bidRequest) {
interpretResponse: function (serverResponse, bidRequest) {
const bidResponses = [];
if (!serverResponse || !serverResponse.body) {
return bidResponses
return bidResponses;
}

let res = serverResponse.body;
if (!res.bids || !res.bids.length) {
return []
return [];
}

for (let x = 0; x < serverResponse.body.bids.length; x++) {
let bid = serverResponse.body.bids[x]
let bid = serverResponse.body.bids[x];

bidResponses.push({
requestId: bid.bid_id,
Expand All @@ -152,47 +170,45 @@ export const spec = {
netRevenue: true,
currency: 'USD',
meta: {
advertiserDomains: bid.adomain
}
})
advertiserDomains: bid.adomain,
},
});
}

return bidResponses;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent) {
const syncs = []
getUserSyncs: function (syncOptions, serverResponses, gdprConsent) {
const syncs = [];

if (!serverResponses.length || !serverResponses[0].body) {
return syncs
return syncs;
}

let pixels = serverResponses[0].body.pixels
let pixels = serverResponses[0].body.pixels;
if (!pixels || !pixels.length) {
return syncs
return syncs;
}

let gdprParams = null
let gdprParams = '';
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
gdprParams = `gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`
gdprParams = `gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${
gdprConsent.consentString
}`;
} else {
gdprParams = `gdpr_consent=${gdprConsent.consentString}`
gdprParams = `gdpr_consent=${gdprConsent.consentString}`;
}
}

for (let x = 0; x < pixels.length; x++) {
let pixel = pixels[x]

if ((pixel.type === 'iframe' && syncOptions.iframeEnabled) ||
(pixel.type === 'image' && syncOptions.pixelEnabled)) {
if (gdprParams && gdprParams.length) {
pixel = (pixel.indexOf('?') === -1 ? '?' : '&') + gdprParams
}
syncs.push(pixel)
}
if ((syncOptions.iframeEnabled || syncOptions.pixelEnabled)) {
return [
{
type: 'iframe',
url: 'https://media.reset-digital.com/prebid/async_usersync.html?' + gdprParams.length ? gdprParams : '',
},
];
}
return syncs;
}
},
};

registerBidder(spec);