forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadserver.js
55 lines (47 loc) · 1.54 KB
/
adserver.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
import { formatQS } from './utils.js';
import { targeting } from './targeting.js';
// Adserver parent class
const AdServer = function(attr) {
this.name = attr.adserver;
this.code = attr.code;
this.getWinningBidByCode = function() {
return targeting.getWinningBids(this.code)[0];
};
};
// DFP ad server
export function dfpAdserver(options, urlComponents) {
var adserver = new AdServer(options);
adserver.urlComponents = urlComponents;
var dfpReqParams = {
'env': 'vp',
'gdfp_req': '1',
'impl': 's',
'unviewed_position_start': '1'
};
var dfpParamsWithVariableValue = ['output', 'iu', 'sz', 'url', 'correlator', 'description_url', 'hl'];
var getCustomParams = function(targeting) {
return encodeURIComponent(formatQS(targeting));
};
adserver.appendQueryParams = function() {
var bid = adserver.getWinningBidByCode();
if (bid) {
this.urlComponents.search.description_url = encodeURIComponent(bid.vastUrl);
this.urlComponents.search.cust_params = getCustomParams(bid.adserverTargeting);
this.urlComponents.search.correlator = Date.now();
}
};
adserver.verifyAdserverTag = function() {
for (var key in dfpReqParams) {
if (!this.urlComponents.search.hasOwnProperty(key) || this.urlComponents.search[key] !== dfpReqParams[key]) {
return false;
}
}
for (var i in dfpParamsWithVariableValue) {
if (!this.urlComponents.search.hasOwnProperty(dfpParamsWithVariableValue[i])) {
return false;
}
}
return true;
};
return adserver;
};