-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
targeting.js
182 lines (163 loc) · 6.38 KB
/
targeting.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { uniques, isGptPubadsDefined, getHighestCpm, adUnitsFilter } from './utils';
import { NATIVE_TARGETING_KEYS } from './native';
const bidmanager = require('./bidmanager.js');
const utils = require('./utils.js');
var CONSTANTS = require('./constants.json');
var targeting = exports;
var pbTargetingKeys = [];
targeting.resetPresetTargeting = function(adUnitCode) {
if (isGptPubadsDefined()) {
const adUnitCodes = adUnitCode && adUnitCode.length ? [adUnitCode] : $$PREBID_GLOBAL$$._adUnitCodes;
window.googletag.pubads().getSlots().forEach(slot => {
pbTargetingKeys.forEach(function(key) {
// reset only registered adunits
adUnitCodes.find(function(unit) {
if (unit.code === slot.getAdUnitPath() ||
unit.code === slot.getSlotElementId()) {
slot.setTargeting(key, null);
}
});
});
});
}
};
targeting.getAllTargeting = function(adUnitCode) {
const adUnitCodes = adUnitCode && adUnitCode.length ? [adUnitCode] : $$PREBID_GLOBAL$$._adUnitCodes;
// Get targeting for the winning bid. Add targeting for any bids that have
// `alwaysUseBid=true`. If sending all bids is enabled, add targeting for losing bids.
var targeting = getWinningBidTargeting(adUnitCodes)
.concat(getAlwaysUseBidTargeting(adUnitCodes))
.concat($$PREBID_GLOBAL$$._sendAllBids ? getBidLandscapeTargeting(adUnitCodes) : []);
// store a reference of the targeting keys
targeting.map(adUnitCode => {
Object.keys(adUnitCode).map(key => {
adUnitCode[key].map(targetKey => {
if (pbTargetingKeys.indexOf(Object.keys(targetKey)[0]) === -1) {
pbTargetingKeys = Object.keys(targetKey).concat(pbTargetingKeys);
}
});
});
});
return targeting;
};
targeting.setTargeting = function(targetingConfig) {
window.googletag.pubads().getSlots().forEach(slot => {
targetingConfig.filter(targeting => Object.keys(targeting)[0] === slot.getAdUnitPath() ||
Object.keys(targeting)[0] === slot.getSlotElementId())
.forEach(targeting => targeting[Object.keys(targeting)[0]]
.forEach(key => {
key[Object.keys(key)[0]]
.map((value) => {
utils.logMessage(`Attempting to set key value for slot: ${slot.getSlotElementId()} key: ${Object.keys(key)[0]} value: ${value}`);
return value;
})
.forEach(value => {
slot.setTargeting(Object.keys(key)[0], value);
});
}));
});
};
targeting.getWinningBids = function(adUnitCode) {
// use the given adUnitCode as a filter if present or all adUnitCodes if not
const adUnitCodes = adUnitCode ? [adUnitCode] : $$PREBID_GLOBAL$$._adUnitCodes;
return $$PREBID_GLOBAL$$._bidsReceived
.filter(bid => adUnitCodes.includes(bid.adUnitCode))
.filter(bid => bid.cpm > 0)
.map(bid => bid.adUnitCode)
.filter(uniques)
.map(adUnitCode => $$PREBID_GLOBAL$$._bidsReceived
.filter(bid => bid.adUnitCode === adUnitCode ? bid : null)
.reduce(getHighestCpm,
{
adUnitCode: adUnitCode,
cpm: 0,
adserverTargeting: {},
timeToRespond: 0
}));
};
targeting.setTargetingForAst = function() {
let targeting = $$PREBID_GLOBAL$$.getAdserverTargeting();
Object.keys(targeting).forEach(targetId =>
Object.keys(targeting[targetId]).forEach(key => {
utils.logMessage(`Attempting to set targeting for targetId: ${targetId} key: ${key} value: ${targeting[targetId][key]}`);
// setKeywords supports string and array as value
if (utils.isStr(targeting[targetId][key]) || utils.isArray(targeting[targetId][key])) {
let keywordsObj = {};
let input = 'hb_adid';
let nKey = (key.substring(0, input.length) === input) ? key.toUpperCase() : key;
keywordsObj[nKey] = targeting[targetId][key];
window.apntag.setKeywords(targetId, keywordsObj);
}
})
);
};
function getWinningBidTargeting() {
let winners = targeting.getWinningBids();
let standardKeys = getStandardKeys();
winners = winners.map(winner => {
return {
[winner.adUnitCode]: Object.keys(winner.adserverTargeting)
.filter(key =>
typeof winner.sendStandardTargeting === 'undefined' ||
winner.sendStandardTargeting ||
standardKeys.indexOf(key) === -1)
.map(key => ({ [key.substring(0, 20)]: [winner.adserverTargeting[key]] }))
};
});
return winners;
}
function getStandardKeys() {
return bidmanager.getStandardBidderAdServerTargeting() // in case using a custom standard key set
.map(targeting => targeting.key)
.concat(CONSTANTS.TARGETING_KEYS).filter(uniques); // standard keys defined in the library.
}
/**
* Get custom targeting keys for bids that have `alwaysUseBid=true`.
*/
function getAlwaysUseBidTargeting(adUnitCodes) {
let standardKeys = getStandardKeys();
return $$PREBID_GLOBAL$$._bidsReceived
.filter(adUnitsFilter.bind(this, adUnitCodes))
.map(bid => {
if (bid.alwaysUseBid) {
return {
[bid.adUnitCode]: Object.keys(bid.adserverTargeting).map(key => {
// Get only the non-standard keys of the losing bids, since we
// don't want to override the standard keys of the winning bid.
if (standardKeys.indexOf(key) > -1) {
return;
}
return { [key.substring(0, 20)]: [bid.adserverTargeting[key]] };
}).filter(key => key) // remove empty elements
};
}
})
.filter(bid => bid); // removes empty elements in array;
}
function getBidLandscapeTargeting(adUnitCodes) {
const standardKeys = CONSTANTS.TARGETING_KEYS.concat(NATIVE_TARGETING_KEYS);
return $$PREBID_GLOBAL$$._bidsReceived
.filter(adUnitsFilter.bind(this, adUnitCodes))
.map(bid => {
if (bid.adserverTargeting) {
return {
[bid.adUnitCode]: getTargetingMap(bid, standardKeys.filter(
key => typeof bid.adserverTargeting[key] !== 'undefined') // mainly for possibly
// unset hb_deal
)
};
}
}).filter(bid => bid); // removes empty elements in array
}
function getTargetingMap(bid, keys) {
return keys.map(key => {
return {
[`${key}_${bid.bidderCode}`.substring(0, 20)]: [bid.adserverTargeting[key]]
};
});
}
targeting.isApntagDefined = function() {
if (window.apntag && utils.isFn(window.apntag.setKeywords)) {
return true;
}
};