Skip to content

Commit

Permalink
Multiple Bid/Analytics Adapters: import utils functions as needed and…
Browse files Browse the repository at this point in the history
… not the whole module (#7471)

* added support for pubcommon, digitrust, id5id

* added support for IdentityLink

* changed the source for id5

* added unit test cases

* changed source param for identityLink

* import utils as needed; dont import all

* import utils as needed; dont import all

* Import utils functions as needed; do not import whole module
  • Loading branch information
pm-harshad-mane authored Sep 27, 2021
1 parent cd10ea0 commit 2f7d3fd
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 268 deletions.
89 changes: 46 additions & 43 deletions modules/adkernelBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as utils from '../src/utils.js';
import {
isStr, isArray, isPlainObject, deepSetValue, isNumber, deepAccess, getAdUnitSizes, parseGPTSingleSizeArrayToRtbSize,
cleanObj, contains, getDNT, parseUrl, createTrackPixelHtml, _each, isArrayOfNums
} from '../src/utils.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import find from 'core-js-pure/features/array/find.js';
Expand Down Expand Up @@ -154,24 +157,24 @@ export const spec = {
prBid.mediaType = NATIVE;
prBid.native = buildNativeAd(JSON.parse(rtbBid.adm));
}
if (utils.isStr(rtbBid.dealid)) {
if (isStr(rtbBid.dealid)) {
prBid.dealId = rtbBid.dealid;
}
if (utils.isArray(rtbBid.adomain)) {
utils.deepSetValue(prBid, 'meta.advertiserDomains', rtbBid.adomain);
if (isArray(rtbBid.adomain)) {
deepSetValue(prBid, 'meta.advertiserDomains', rtbBid.adomain);
}
if (utils.isArray(rtbBid.cat)) {
utils.deepSetValue(prBid, 'meta.secondaryCatIds', rtbBid.cat);
if (isArray(rtbBid.cat)) {
deepSetValue(prBid, 'meta.secondaryCatIds', rtbBid.cat);
}
if (utils.isPlainObject(rtbBid.ext)) {
if (utils.isNumber(rtbBid.ext.advertiser_id)) {
utils.deepSetValue(prBid, 'meta.advertiserId', rtbBid.ext.advertiser_id);
if (isPlainObject(rtbBid.ext)) {
if (isNumber(rtbBid.ext.advertiser_id)) {
deepSetValue(prBid, 'meta.advertiserId', rtbBid.ext.advertiser_id);
}
if (utils.isStr(rtbBid.ext.advertiser_name)) {
utils.deepSetValue(prBid, 'meta.advertiserName', rtbBid.ext.advertiser_name);
if (isStr(rtbBid.ext.advertiser_name)) {
deepSetValue(prBid, 'meta.advertiserName', rtbBid.ext.advertiser_name);
}
if (utils.isStr(rtbBid.ext.agency_name)) {
utils.deepSetValue(prBid, 'meta.agencyName', rtbBid.ext.agency_name);
if (isStr(rtbBid.ext.agency_name)) {
deepSetValue(prBid, 'meta.agencyName', rtbBid.ext.agency_name);
}
}

Expand Down Expand Up @@ -241,27 +244,27 @@ function buildImp(bidRequest, secure) {
var mediaType;
var sizes = [];

if (utils.deepAccess(bidRequest, 'mediaTypes.banner')) {
sizes = utils.getAdUnitSizes(bidRequest);
if (deepAccess(bidRequest, 'mediaTypes.banner')) {
sizes = getAdUnitSizes(bidRequest);
imp.banner = {
format: sizes.map(wh => utils.parseGPTSingleSizeArrayToRtbSize(wh)),
format: sizes.map(wh => parseGPTSingleSizeArrayToRtbSize(wh)),
topframe: 0
};
mediaType = BANNER;
} else if (utils.deepAccess(bidRequest, 'mediaTypes.video')) {
let video = utils.deepAccess(bidRequest, 'mediaTypes.video');
} else if (deepAccess(bidRequest, 'mediaTypes.video')) {
let video = deepAccess(bidRequest, 'mediaTypes.video');
imp.video = {};
if (video.playerSize) {
sizes = video.playerSize[0];
imp.video = Object.assign(imp.video, utils.parseGPTSingleSizeArrayToRtbSize(sizes) || {});
imp.video = Object.assign(imp.video, parseGPTSingleSizeArrayToRtbSize(sizes) || {});
}
if (bidRequest.params.video) {
Object.keys(bidRequest.params.video)
.filter(key => includes(VIDEO_TARGETING, key))
.forEach(key => imp.video[key] = bidRequest.params.video[key]);
}
mediaType = VIDEO;
} else if (utils.deepAccess(bidRequest, 'mediaTypes.native')) {
} else if (deepAccess(bidRequest, 'mediaTypes.native')) {
let nativeRequest = buildNativeRequest(bidRequest.mediaTypes.native);
imp.native = {
ver: '1.1',
Expand Down Expand Up @@ -299,7 +302,7 @@ function buildNativeRequest(nativeReq) {
if (desc.assetType === 'img') {
assetRoot[desc.assetType] = buildImageAsset(desc, v);
} else if (desc.assetType === 'data') {
assetRoot.data = utils.cleanObj({type: desc.type, len: v.len});
assetRoot.data = cleanObj({type: desc.type, len: v.len});
} else if (desc.assetType === 'title') {
assetRoot.title = {len: v.len || 90};
} else {
Expand All @@ -323,7 +326,7 @@ function buildImageAsset(desc, val) {
img.wmin = val.aspect_ratios[0].min_width;
img.hmin = val.aspect_ratios[0].min_height;
}
return utils.cleanObj(img);
return cleanObj(img);
}

/**
Expand All @@ -336,9 +339,9 @@ function isSyncMethodAllowed(syncRule, bidderCode) {
if (!syncRule) {
return false;
}
let bidders = utils.isArray(syncRule.bidders) ? syncRule.bidders : [bidderCode];
let bidders = isArray(syncRule.bidders) ? syncRule.bidders : [bidderCode];
let rule = syncRule.filter === 'include';
return utils.contains(bidders, bidderCode) === rule;
return contains(bidders, bidderCode) === rule;
}

/**
Expand Down Expand Up @@ -382,33 +385,33 @@ function buildRtbRequest(imps, bidderRequest, schain) {
},
'tmax': parseInt(timeout)
};
if (utils.getDNT()) {
if (getDNT()) {
req.device.dnt = 1;
}
if (gdprConsent) {
if (gdprConsent.gdprApplies !== undefined) {
utils.deepSetValue(req, 'regs.ext.gdpr', ~~gdprConsent.gdprApplies);
deepSetValue(req, 'regs.ext.gdpr', ~~gdprConsent.gdprApplies);
}
if (gdprConsent.consentString !== undefined) {
utils.deepSetValue(req, 'user.ext.consent', gdprConsent.consentString);
deepSetValue(req, 'user.ext.consent', gdprConsent.consentString);
}
}
if (uspConsent) {
utils.deepSetValue(req, 'regs.ext.us_privacy', uspConsent);
deepSetValue(req, 'regs.ext.us_privacy', uspConsent);
}
if (coppa) {
utils.deepSetValue(req, 'regs.coppa', 1);
deepSetValue(req, 'regs.coppa', 1);
}
let syncMethod = getAllowedSyncMethod(bidderCode);
if (syncMethod) {
utils.deepSetValue(req, 'ext.adk_usersync', syncMethod);
deepSetValue(req, 'ext.adk_usersync', syncMethod);
}
if (schain) {
utils.deepSetValue(req, 'source.ext.schain', schain);
deepSetValue(req, 'source.ext.schain', schain);
}
let eids = getExtendedUserIds(bidderRequest);
if (eids) {
utils.deepSetValue(req, 'user.ext.eids', eids);
deepSetValue(req, 'user.ext.eids', eids);
}
return req;
}
Expand All @@ -426,7 +429,7 @@ function getLanguage() {
* Creates site description object
*/
function createSite(refInfo) {
let url = utils.parseUrl(refInfo.referer);
let url = parseUrl(refInfo.referer);
let site = {
'domain': url.hostname,
'page': `${url.protocol}://${url.hostname}${url.pathname}`
Expand All @@ -442,8 +445,8 @@ function createSite(refInfo) {
}

function getExtendedUserIds(bidderRequest) {
let eids = utils.deepAccess(bidderRequest, 'bids.0.userIdAsEids');
if (utils.isArray(eids)) {
let eids = deepAccess(bidderRequest, 'bids.0.userIdAsEids');
if (isArray(eids)) {
return eids;
}
}
Expand All @@ -455,7 +458,7 @@ function getExtendedUserIds(bidderRequest) {
function formatAdMarkup(bid) {
let adm = bid.adm;
if ('nurl' in bid) {
adm += utils.createTrackPixelHtml(`${bid.nurl}&px=1`);
adm += createTrackPixelHtml(`${bid.nurl}&px=1`);
}
return adm;
}
Expand All @@ -465,8 +468,8 @@ function formatAdMarkup(bid) {
*/
function validateNativeAdUnit(adUnit) {
return validateNativeImageSize(adUnit.image) && validateNativeImageSize(adUnit.icon) &&
!utils.deepAccess(adUnit, 'privacyLink.required') && // not supported yet
!utils.deepAccess(adUnit, 'privacyIcon.required'); // not supported yet
!deepAccess(adUnit, 'privacyLink.required') && // not supported yet
!deepAccess(adUnit, 'privacyIcon.required'); // not supported yet
}

/**
Expand All @@ -477,9 +480,9 @@ function validateNativeImageSize(img) {
return true;
}
if (img.sizes) {
return utils.isArrayOfNums(img.sizes, 2);
return isArrayOfNums(img.sizes, 2);
}
if (utils.isArray(img.aspect_ratios)) {
if (isArray(img.aspect_ratios)) {
return img.aspect_ratios.length > 0 && img.aspect_ratios[0].min_height && img.aspect_ratios[0].min_width;
}
return true;
Expand All @@ -496,14 +499,14 @@ function buildNativeAd(nativeResp) {
javascriptTrackers: jstracker ? [jstracker] : undefined,
privacyLink: privacy,
};
utils._each(assets, asset => {
_each(assets, asset => {
let assetName = NATIVE_MODEL[asset.id].name;
let assetType = NATIVE_MODEL[asset.id].assetType;
nativeAd[assetName] = asset[assetType].text || asset[assetType].value || utils.cleanObj({
nativeAd[assetName] = asset[assetType].text || asset[assetType].value || cleanObj({
url: asset[assetType].url,
width: asset[assetType].w,
height: asset[assetType].h
});
});
return utils.cleanObj(nativeAd);
return cleanObj(nativeAd);
}
52 changes: 26 additions & 26 deletions modules/adlooxAdServerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,48 @@ import { command as analyticsCommand, COMMAND } from './adlooxAnalyticsAdapter.j
import { ajax } from '../src/ajax.js';
import { EVENTS } from '../src/constants.json';
import { targeting } from '../src/targeting.js';
import * as utils from '../src/utils.js';
import { logInfo, isFn, logError, isPlainObject, isStr, isBoolean, deepSetValue, deepClone, timestamp, logWarn } from '../src/utils.js';

const MODULE = 'adlooxAdserverVideo';

const URL_VAST = 'https://j.adlooxtracking.com/ads/vast/tag.php';

export function buildVideoUrl(options, callback) {
utils.logInfo(MODULE, 'buildVideoUrl', options);
logInfo(MODULE, 'buildVideoUrl', options);

if (!utils.isFn(callback)) {
utils.logError(MODULE, 'invalid callback');
if (!isFn(callback)) {
logError(MODULE, 'invalid callback');
return false;
}
if (!utils.isPlainObject(options)) {
utils.logError(MODULE, 'missing options');
if (!isPlainObject(options)) {
logError(MODULE, 'missing options');
return false;
}
if (!(options.url_vast === undefined || utils.isStr(options.url_vast))) {
utils.logError(MODULE, 'invalid url_vast options value');
if (!(options.url_vast === undefined || isStr(options.url_vast))) {
logError(MODULE, 'invalid url_vast options value');
return false;
}
if (!(utils.isPlainObject(options.adUnit) || utils.isPlainObject(options.bid))) {
utils.logError(MODULE, "requires either 'adUnit' or 'bid' options value");
if (!(isPlainObject(options.adUnit) || isPlainObject(options.bid))) {
logError(MODULE, "requires either 'adUnit' or 'bid' options value");
return false;
}
if (!utils.isStr(options.url)) {
utils.logError(MODULE, 'invalid url options value');
if (!isStr(options.url)) {
logError(MODULE, 'invalid url options value');
return false;
}
if (!(options.wrap === undefined || utils.isBoolean(options.wrap))) {
utils.logError(MODULE, 'invalid wrap options value');
if (!(options.wrap === undefined || isBoolean(options.wrap))) {
logError(MODULE, 'invalid wrap options value');
return false;
}
if (!(options.blob === undefined || utils.isBoolean(options.blob))) {
utils.logError(MODULE, 'invalid blob options value');
if (!(options.blob === undefined || isBoolean(options.blob))) {
logError(MODULE, 'invalid blob options value');
return false;
}

// same logic used in modules/dfpAdServerVideo.js
options.bid = options.bid || targeting.getWinningBids(options.adUnit.code)[0];

utils.deepSetValue(options.bid, 'ext.adloox.video.adserver', true);
deepSetValue(options.bid, 'ext.adloox.video.adserver', true);

if (options.wrap !== false) {
VASTWrapper(options, callback);
Expand All @@ -70,7 +70,7 @@ registerVideoSupport('adloox', {
function track(options, callback) {
callback(options.url);

const bid = utils.deepClone(options.bid);
const bid = deepClone(options.bid);
bid.ext.adloox.video.adserver = false;

analyticsCommand(COMMAND.TRACK, {
Expand All @@ -85,13 +85,13 @@ function VASTWrapper(options, callback) {
function process(result) {
function getAd(xml) {
if (!xml || xml.documentElement.tagName != 'VAST') {
utils.logError(MODULE, 'not a VAST tag, using non-wrapped tracking');
logError(MODULE, 'not a VAST tag, using non-wrapped tracking');
return;
}

const ads = xml.querySelectorAll('Ad');
if (!ads.length) {
utils.logError(MODULE, 'no VAST ads, using non-wrapped tracking');
logError(MODULE, 'no VAST ads, using non-wrapped tracking');
return;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ function VASTWrapper(options, callback) {

options.url = toBlob(chain[0]);

const epoch = utils.timestamp() - new Date().getTimezoneOffset() * 60 * 1000;
const epoch = timestamp() - new Date().getTimezoneOffset() * 60 * 1000;
const expires0 = options.bid.ttl * 1000 - (epoch - options.bid.responseTimestamp);
const expires = Math.max(30 * 1000, expires0);
setTimeout(function() { urls.forEach(u => URL.revokeObjectURL(u)) }, expires);
Expand All @@ -154,7 +154,7 @@ function VASTWrapper(options, callback) {
const wrapper = getWrapper(ad);
if (wrapper) {
if (chain.length > 5) {
utils.logWarn(MODULE, `got wrapped tag at depth ${chain.length}, not continuing`);
logWarn(MODULE, `got wrapped tag at depth ${chain.length}, not continuing`);
blobify();
return track(options, callback);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ function VASTWrapper(options, callback) {
if (duration != 15) args.push([ 'duration', duration ]);
if (skip) args.push([ 'skip', skip ]);

utils.logInfo(MODULE, `processed VAST tag chain of depth ${chain.depth}, running callback`);
logInfo(MODULE, `processed VAST tag chain of depth ${chain.depth}, running callback`);

analyticsCommand(COMMAND.URL, {
url: (options.url_vast || URL_VAST) + '?',
Expand All @@ -202,18 +202,18 @@ function VASTWrapper(options, callback) {
}

function fetch(url, withoutcredentials) {
utils.logInfo(MODULE, `fetching VAST ${url}`);
logInfo(MODULE, `fetching VAST ${url}`);

ajax(url, {
success: function(responseText, q) {
process({ type: q.getResponseHeader('content-type'), xml: q.responseXML });
},
error: function(statusText, q) {
if (!withoutcredentials) {
utils.logWarn(MODULE, `unable to download (${statusText}), suspected CORS withCredentials problem, retrying without`);
logWarn(MODULE, `unable to download (${statusText}), suspected CORS withCredentials problem, retrying without`);
return fetch(url, true);
}
utils.logError(MODULE, `failed to fetch (${statusText}), using non-wrapped tracking`);
logError(MODULE, `failed to fetch (${statusText}), using non-wrapped tracking`);
process();
}
}, undefined, { withCredentials: !withoutcredentials });
Expand Down
Loading

0 comments on commit 2f7d3fd

Please sign in to comment.