forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublirBidAdapter.js
138 lines (130 loc) · 4.14 KB
/
publirBidAdapter.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
import {
logWarn,
logInfo,
isArray,
deepAccess,
timestamp,
triggerPixel,
} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { getStorageManager } from '../src/storageManager.js';
import { ajax } from '../src/ajax.js';
import {
generateBidsParams,
generateGeneralParams,
} from '../libraries/riseUtils/index.js';
const SUPPORTED_AD_TYPES = [BANNER, VIDEO];
const BIDDER_CODE = 'publir';
const ADAPTER_VERSION = '1.0.0';
const TTL = 360;
const CURRENCY = 'USD';
const BASE_URL = 'https://prebid.publir.com/publirPrebidEndPoint';
const DEFAULT_IMPS_ENDPOINT = 'https://prebidimpst.publir.com/publirPrebidImpressionTracker';
export const storage = getStorageManager({ bidderCode: BIDDER_CODE });
export const spec = {
code: BIDDER_CODE,
version: ADAPTER_VERSION,
aliases: ['plr'],
supportedMediaTypes: SUPPORTED_AD_TYPES,
isBidRequestValid: function (bidRequest) {
if (!bidRequest.params.pubId) {
logWarn('pubId is a mandatory param for Publir adapter');
return false;
}
return true;
},
buildRequests: function (validBidRequests, bidderRequest) {
const combinedRequestsObject = {};
const generalObject = validBidRequests[0];
combinedRequestsObject.params = generateGeneralParams(generalObject, bidderRequest, ADAPTER_VERSION);
combinedRequestsObject.bids = generateBidsParams(validBidRequests, bidderRequest);
combinedRequestsObject.bids.timestamp = timestamp();
let options = {
withCredentials: false
};
return {
method: 'POST',
url: BASE_URL,
data: combinedRequestsObject,
options
};
},
interpretResponse: function ({ body }) {
const bidResponses = [];
if (body.bids) {
body.bids.forEach(adUnit => {
const bidResponse = {
requestId: adUnit.requestId,
cpm: adUnit.cpm,
currency: adUnit.currency || CURRENCY,
width: adUnit.width,
height: adUnit.height,
ttl: adUnit.ttl || TTL,
creativeId: adUnit.creativeId,
netRevenue: adUnit.netRevenue || true,
nurl: adUnit.nurl,
mediaType: adUnit.mediaType,
meta: {
mediaType: adUnit.mediaType
},
};
if (adUnit.mediaType === VIDEO) {
bidResponse.vastXml = adUnit.vastXml;
} else if (adUnit.mediaType === BANNER) {
bidResponse.ad = adUnit.ad;
}
if (adUnit.adomain && adUnit.adomain.length) {
bidResponse.meta.advertiserDomains = adUnit.adomain;
} else {
bidResponse.meta.advertiserDomains = [];
}
if (adUnit?.meta?.ad_key) {
bidResponse.meta.ad_key = adUnit.meta.ad_key ?? null;
}
if (adUnit.campId) {
bidResponse.campId = adUnit.campId;
}
bidResponse.bidder = BIDDER_CODE;
bidResponses.push(bidResponse);
});
} else {
return [];
}
return bidResponses;
},
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = [];
for (const response of serverResponses) {
if (response.body && response.body.params) {
if (syncOptions.iframeEnabled && deepAccess(response, 'body.params.userSyncURL')) {
syncs.push({
type: 'iframe',
url: deepAccess(response, 'body.params.userSyncURL')
});
}
if (syncOptions.pixelEnabled && isArray(deepAccess(response, 'body.params.userSyncPixels'))) {
const pixels = response.body.params.userSyncPixels.map(pixel => {
return {
type: 'image',
url: pixel
};
});
syncs.push(...pixels);
}
}
}
return syncs;
},
onBidWon: function (bid) {
if (bid == null) {
return;
}
logInfo('onBidWon:', bid);
ajax(DEFAULT_IMPS_ENDPOINT, null, JSON.stringify(bid), { method: 'POST', mode: 'no-cors', credentials: 'include', headers: { 'Content-Type': 'application/json' } });
if (bid.hasOwnProperty('nurl') && bid.nurl.length > 0) {
triggerPixel(bid.nurl);
}
},
};
registerBidder(spec);