forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
my6senseBidAdapter.js
200 lines (171 loc) · 6.03 KB
/
my6senseBidAdapter.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { BANNER, NATIVE } from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { convertOrtbRequestToProprietaryNative } from '../src/native.js';
const BIDDER_CODE = 'my6sense';
const END_POINT = 'https://hb.mynativeplatform.com/pub2/web/v1.15.0/hbwidget.json';
const END_POINT_METHOD = 'POST';
// called first
function isBidRequestValid(bid) {
return !(bid.bidder !== BIDDER_CODE || !bid.params || !bid.params.key);
}
function getUrl(url) {
// TODO: this should probably look at refererInfo
if (!url) {
url = window.location.href;// "clean" url of current web page
}
var canonicalLink = null;
// first look for meta data with property "og:url"
var metaElements = document.getElementsByTagName('meta');
for (var i = 0; i < metaElements.length && !canonicalLink; i++) {
if (metaElements[i].getAttribute('property') == 'og:url') {
canonicalLink = metaElements[i].content;
}
}
if (!canonicalLink) {
var canonicalLinkContainer = document.querySelector("link[rel='canonical']");// html element containing the canonical link
if (canonicalLinkContainer) {
// get clean url from href of <link rel='canocial' .../>
canonicalLink = canonicalLinkContainer.href;
}
}
url = canonicalLink || url;
return encodeURIComponent(url).toString();
}
/**
* this function is used to fix param value before sending them to server, if user did not set it,
* default value for parameter will be returned
* example1: paidClicks: '[PAID_TRACKING_PIXEL]', will return {value: '', fromUser: false}
* example2: pageURL: 'www.my6sense.com', will return {value: 'www.my6sense.com', fromUser: true}
* @param key
* @param value
* @returns {{value: *, fromUser: boolean}}
*/
function fixRequestParamForServer(key, value) {
function isEmptyValue(key, value) {
return value === parametersMap[key].emptyValue;
}
const parametersMap = {
'pageUrl': {
emptyValue: '[PAGE_URL]',
defaultValue: getUrl()
},
'displayWithinIframe': {
emptyValue: '',
defaultValue: ''
},
'dataParams': {
emptyValue: '[KEY_VALUES]',
defaultValue: ''
},
'paidClicks': {
emptyValue: '[PAID_TRACKING_PIXEL]',
defaultValue: ''
},
'organicClicks': {
emptyValue: '[ORGANIC_TRACKING_PIXEL]',
defaultValue: ''
},
'dataView': {
emptyValue: '[VIEW_TRACKING_PIXEL]',
defaultValue: ''
},
// ZONE is not part of this object, handled on server side
};
// if param is not in list we do not change it (return it as is)
if (!parametersMap.hasOwnProperty(key)) {
return {
value: value,
fromUser: true
};
}
// if no value given by user set it to default
if (!value || isEmptyValue(key, value)) {
return {
value: parametersMap[key].defaultValue,
fromUser: false
};
}
return {
value: value,
fromUser: true
};
}
// called second
function buildGdprServerProperty(bidderRequest) {
var gdprObj = {
gdpr_consent: null,
gdpr: null
};
if (bidderRequest && 'gdprConsent' in bidderRequest) {
gdprObj.gdpr_consent = bidderRequest.gdprConsent.consentString || null;
gdprObj.gdpr = gdprObj.gdpr === null && bidderRequest.gdprConsent.gdprApplies == true ? true : gdprObj.gdpr;
gdprObj.gdpr = gdprObj.gdpr === null && bidderRequest.gdprConsent.gdprApplies == false ? false : gdprObj.gdpr;
gdprObj.gdpr = gdprObj.gdpr === null && bidderRequest.gdprConsent.gdprApplies == 1 ? true : gdprObj.gdpr;
gdprObj.gdpr = gdprObj.gdpr === null && bidderRequest.gdprConsent.gdprApplies == 0 ? false : gdprObj.gdpr;
}
return gdprObj;
}
function buildRequests(validBidRequests, bidderRequest) {
// convert Native ORTB definition to old-style prebid native definition
validBidRequests = convertOrtbRequestToProprietaryNative(validBidRequests);
let requests = [];
if (validBidRequests && validBidRequests.length) {
validBidRequests.forEach(bidRequest => {
bidRequest.widget_num = 1; // mandatory property for server side
let isDataUrlSetByUser = false;
let debug = false;
if (bidRequest.params) {
for (let key in bidRequest.params) {
// loop over params and remove empty/untouched values
if (bidRequest.params.hasOwnProperty(key)) {
// if debug we update url string to get core debug version
if (key === 'debug' && bidRequest.params[key] === true) {
debug = true;
delete bidRequest.params[key];
continue;
}
let fixedObj = fixRequestParamForServer(key, bidRequest.params[key]);
bidRequest.params[key] = fixedObj.value;
// if pageUrl is set by user we should update variable for query string param
if (key === 'pageUrl' && fixedObj.fromUser === true) {
isDataUrlSetByUser = true;
}
// remove empty params from request
if (!bidRequest.params[key]) {
delete bidRequest.params[key];
}
}
}
}
let url = `${END_POINT}?widget_key=${bidRequest.params.key}&is_data_url_set=${isDataUrlSetByUser}`; // mandatory query string for server side
if (debug) {
url = `${END_POINT}?env=debug&widget_key=${bidRequest.params.key}&is_data_url_set=${isDataUrlSetByUser}`; // this url is for debugging
}
bidRequest.gdpr = buildGdprServerProperty(bidderRequest);
requests.push({
url: url,
method: END_POINT_METHOD,
data: JSON.stringify(bidRequest)
});
});
}
return requests;
}
// called third
function interpretResponse(serverResponse) {
const bidResponses = [];
// currently server returns a single response which is the body property
if (serverResponse.body) {
serverResponse.body.bidderCode = BIDDER_CODE;
bidResponses.push(serverResponse.body);
}
return bidResponses;
}
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, NATIVE],
isBidRequestValid,
buildRequests,
interpretResponse
};
registerBidder(spec);