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

Prebid 9: extract DFP adpod logic into a separate dfpAdpod module #11550

Merged
merged 1 commit into from
Jun 3, 2024
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
13 changes: 13 additions & 0 deletions libraries/dfpUtils/dfpUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** Safe defaults which work on pretty much all video calls. */
export const DEFAULT_DFP_PARAMS = {
env: 'vp',
gdfp_req: 1,
output: 'vast',
unviewed_position_start: 1,
}

export const DFP_ENDPOINT = {
protocol: 'https',
host: 'securepubads.g.doubleclick.net',
pathname: '/gampad/ads'
}
2 changes: 1 addition & 1 deletion modules/.submodules.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
],
"adpod": [
"freeWheelAdserverVideo",
"dfpAdServerVideo"
"dfpAdpod"
],
"rtdModule": [
"1plusXRtdProvider",
Expand Down
119 changes: 6 additions & 113 deletions modules/dfpAdServerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
import {registerVideoSupport} from '../src/adServerManager.js';
import {targeting} from '../src/targeting.js';
import {
isNumber,
buildUrl,
deepAccess,
formatQS,
isEmpty,
isNumber,
logError,
parseSizesInput,
parseUrl,
uniques
} from '../src/utils.js';
import {config} from '../src/config.js';
import {getHook, submodule} from '../src/hook.js';
import {getHook} from '../src/hook.js';
import {auctionManager} from '../src/auctionManager.js';
import {gdprDataHandler} from '../src/adapterManager.js';
import * as events from '../src/events.js';
import { EVENTS } from '../src/constants.js';
import {EVENTS} from '../src/constants.js';
import {getPPID} from '../src/adserver.js';
import {getRefererInfo} from '../src/refererDetection.js';
import {CLIENT_SECTIONS} from '../src/fpd/oneClient.js';

import {DEFAULT_DFP_PARAMS, DFP_ENDPOINT} from '../libraries/dfpUtils/dfpUtils.js';
/**
* @typedef {Object} DfpVideoParams
*
Expand Down Expand Up @@ -54,16 +54,6 @@ import {CLIENT_SECTIONS} from '../src/fpd/oneClient.js';
* @param {string} [url] video adserver url
*/

/** Safe defaults which work on pretty much all video calls. */
const defaultParamConstants = {
env: 'vp',
gdfp_req: 1,
output: 'vast',
unviewed_position_start: 1,
};

export const adpodUtils = {};

export const dep = {
ri: getRefererInfo
}
Expand Down Expand Up @@ -115,7 +105,7 @@ export function buildDfpVideoUrl(options) {
let encodedCustomParams = getCustParams(bid, options, urlSearchComponent && urlSearchComponent.cust_params);

const queryParams = Object.assign({},
defaultParamConstants,
DEFAULT_DFP_PARAMS,
urlComponents.search,
derivedParams,
options.params,
Expand Down Expand Up @@ -202,11 +192,7 @@ export function buildDfpVideoUrl(options) {
}))
}

return buildUrl(Object.assign({
protocol: 'https',
host: 'securepubads.g.doubleclick.net',
pathname: '/gampad/ads'
}, urlComponents, { search: queryParams }));
return buildUrl(Object.assign({}, DFP_ENDPOINT, urlComponents, { search: queryParams }));
}

export function notifyTranslationModule(fn) {
Expand All @@ -215,95 +201,6 @@ export function notifyTranslationModule(fn) {

if (config.getConfig('brandCategoryTranslation.translationFile')) { getHook('registerAdserver').before(notifyTranslationModule); }

/**
* @typedef {Object} DfpAdpodOptions
*
* @param {string} code Ad Unit code
* @param {Object} params Query params which should be set on the DFP request.
* These will override this module's defaults whenever they conflict.
* @param {function} callback Callback function to execute when master tag is ready
*/

/**
* Creates master tag url for long-form
* @param {DfpAdpodOptions} options
* @returns {string} A URL which calls DFP with custom adpod targeting key values to compete with rest of the demand in DFP
*/
export function buildAdpodVideoUrl({code, params, callback} = {}) {
// TODO: the public API for this does not take in enough info to fill all DFP params (adUnit/bid),
// and is marked "alpha": https://docs.prebid.org/dev-docs/publisher-api-reference/adServers.dfp.buildAdpodVideoUrl.html
if (!params || !callback) {
logError(`A params object and a callback is required to use pbjs.adServers.dfp.buildAdpodVideoUrl`);
return;
}

const derivedParams = {
correlator: Date.now(),
sz: getSizeForAdUnit(code),
url: encodeURIComponent(location.href),
};

function getSizeForAdUnit(code) {
let adUnit = auctionManager.getAdUnits()
.filter((adUnit) => adUnit.code === code)
let sizes = deepAccess(adUnit[0], 'mediaTypes.video.playerSize');
return parseSizesInput(sizes).join('|');
}

adpodUtils.getTargeting({
'codes': [code],
'callback': createMasterTag
});

function createMasterTag(err, targeting) {
if (err) {
callback(err, null);
return;
}

let initialValue = {
[adpodUtils.TARGETING_KEY_PB_CAT_DUR]: undefined,
[adpodUtils.TARGETING_KEY_CACHE_ID]: undefined
};
let customParams = {};
if (targeting[code]) {
customParams = targeting[code].reduce((acc, curValue) => {
if (Object.keys(curValue)[0] === adpodUtils.TARGETING_KEY_PB_CAT_DUR) {
acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] = (typeof acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] !== 'undefined') ? acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] + ',' + curValue[adpodUtils.TARGETING_KEY_PB_CAT_DUR] : curValue[adpodUtils.TARGETING_KEY_PB_CAT_DUR];
} else if (Object.keys(curValue)[0] === adpodUtils.TARGETING_KEY_CACHE_ID) {
acc[adpodUtils.TARGETING_KEY_CACHE_ID] = curValue[adpodUtils.TARGETING_KEY_CACHE_ID]
}
return acc;
}, initialValue);
}

let encodedCustomParams = encodeURIComponent(formatQS(customParams));

const queryParams = Object.assign({},
defaultParamConstants,
derivedParams,
params,
{ cust_params: encodedCustomParams }
);

const gdprConsent = gdprDataHandler.getConsentData();
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') { queryParams.gdpr = Number(gdprConsent.gdprApplies); }
if (gdprConsent.consentString) { queryParams.gdpr_consent = gdprConsent.consentString; }
if (gdprConsent.addtlConsent) { queryParams.addtl_consent = gdprConsent.addtlConsent; }
}

const masterTag = buildUrl({
protocol: 'https',
host: 'securepubads.g.doubleclick.net',
pathname: '/gampad/ads',
search: queryParams
});

callback(null, masterTag);
}
}

/**
* Builds a video url from a base dfp video url and a winning bid, appending
* Prebid-specific key-values.
Expand Down Expand Up @@ -375,8 +272,4 @@ function getCustParams(bid, options, urlCustParams) {

registerVideoSupport('dfp', {
buildVideoUrl: buildDfpVideoUrl,
buildAdpodVideoUrl: buildAdpodVideoUrl,
getAdpodTargeting: (args) => adpodUtils.getTargeting(args)
});

submodule('adpod', adpodUtils);
102 changes: 102 additions & 0 deletions modules/dfpAdpod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {submodule} from '../src/hook.js';
import {buildUrl, deepAccess, formatQS, logError, parseSizesInput} from '../src/utils.js';
import {auctionManager} from '../src/auctionManager.js';
import {DEFAULT_DFP_PARAMS, DFP_ENDPOINT} from '../libraries/dfpUtils/dfpUtils.js';
import {gdprDataHandler} from '../src/consentHandler.js';
import {registerVideoSupport} from '../src/adServerManager.js';

export const adpodUtils = {};

/**
* @typedef {Object} DfpAdpodOptions
*
* @param {string} code Ad Unit code
* @param {Object} params Query params which should be set on the DFP request.
* These will override this module's defaults whenever they conflict.
* @param {function} callback Callback function to execute when master tag is ready
*/

/**
* Creates master tag url for long-form
* @param {DfpAdpodOptions} options
* @returns {string} A URL which calls DFP with custom adpod targeting key values to compete with rest of the demand in DFP
*/
export function buildAdpodVideoUrl({code, params, callback} = {}) {
// TODO: the public API for this does not take in enough info to fill all DFP params (adUnit/bid),
// and is marked "alpha": https://docs.prebid.org/dev-docs/publisher-api-reference/adServers.dfp.buildAdpodVideoUrl.html
if (!params || !callback) {
logError(`A params object and a callback is required to use pbjs.adServers.dfp.buildAdpodVideoUrl`);
return;
}

const derivedParams = {
correlator: Date.now(),
sz: getSizeForAdUnit(code),
url: encodeURIComponent(location.href),
};

function getSizeForAdUnit(code) {
let adUnit = auctionManager.getAdUnits()
.filter((adUnit) => adUnit.code === code)
let sizes = deepAccess(adUnit[0], 'mediaTypes.video.playerSize');
return parseSizesInput(sizes).join('|');
}

adpodUtils.getTargeting({
'codes': [code],
'callback': createMasterTag
});

function createMasterTag(err, targeting) {
if (err) {
callback(err, null);
return;
}

let initialValue = {
[adpodUtils.TARGETING_KEY_PB_CAT_DUR]: undefined,
[adpodUtils.TARGETING_KEY_CACHE_ID]: undefined
};
let customParams = {};
if (targeting[code]) {
customParams = targeting[code].reduce((acc, curValue) => {
if (Object.keys(curValue)[0] === adpodUtils.TARGETING_KEY_PB_CAT_DUR) {
acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] = (typeof acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] !== 'undefined') ? acc[adpodUtils.TARGETING_KEY_PB_CAT_DUR] + ',' + curValue[adpodUtils.TARGETING_KEY_PB_CAT_DUR] : curValue[adpodUtils.TARGETING_KEY_PB_CAT_DUR];
} else if (Object.keys(curValue)[0] === adpodUtils.TARGETING_KEY_CACHE_ID) {
acc[adpodUtils.TARGETING_KEY_CACHE_ID] = curValue[adpodUtils.TARGETING_KEY_CACHE_ID]
}
return acc;
}, initialValue);
}

let encodedCustomParams = encodeURIComponent(formatQS(customParams));

const queryParams = Object.assign({},
DEFAULT_DFP_PARAMS,
derivedParams,
params,
{ cust_params: encodedCustomParams }
);

const gdprConsent = gdprDataHandler.getConsentData();
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') { queryParams.gdpr = Number(gdprConsent.gdprApplies); }
if (gdprConsent.consentString) { queryParams.gdpr_consent = gdprConsent.consentString; }
if (gdprConsent.addtlConsent) { queryParams.addtl_consent = gdprConsent.addtlConsent; }
}

const masterTag = buildUrl({
...DFP_ENDPOINT,
search: queryParams
});

callback(null, masterTag);
}
}

registerVideoSupport('dfp', {
buildAdpodVideoUrl: buildAdpodVideoUrl,
getAdpodTargeting: (args) => adpodUtils.getTargeting(args)
});

submodule('adpod', adpodUtils);
Loading