-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
dfpAdpod.js
102 lines (87 loc) · 3.66 KB
/
dfpAdpod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);