-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mgidXBidAdapter.js
99 lines (89 loc) · 3.7 KB
/
mgidXBidAdapter.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
import { isPlainObject, isNumber, isArray, isStr } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
import { USERSYNC_DEFAULT_CONFIG } from '../src/userSync.js';
import { isBidRequestValid, buildRequestsBase, interpretResponse } from '../libraries/teqblazeUtils/bidderUtils.js';
const BIDDER_CODE = 'mgidX';
const GVLID = 358;
const AD_URL = 'https://#{REGION}#.mgid.com/pbjs';
const PIXEL_SYNC_URL = 'https://cm.mgid.com/i.gif';
const IFRAME_SYNC_URL = 'https://cm.mgid.com/i.html';
const buildRequests = (validBidRequests = [], bidderRequest = {}) => {
const request = buildRequestsBase({ adUrl: AD_URL, validBidRequests, bidderRequest });
const region = validBidRequests[0].params?.region;
if (region === 'eu') {
request.url = AD_URL.replace('#{REGION}#', 'eu');
} else {
request.url = AD_URL.replace('#{REGION}#', 'us-east-x');
}
return request;
};
export const spec = {
code: BIDDER_CODE,
gvlid: GVLID,
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
isBidRequestValid: isBidRequestValid(),
buildRequests,
interpretResponse,
getUserSyncs: (syncOptions, serverResponses, gdprConsent, uspConsent, gppConsent) => {
const spb = isPlainObject(config.getConfig('userSync')) &&
isNumber(config.getConfig('userSync').syncsPerBidder)
? config.getConfig('userSync').syncsPerBidder : USERSYNC_DEFAULT_CONFIG.syncsPerBidder;
if (spb > 0 && isPlainObject(syncOptions) && (syncOptions.iframeEnabled || syncOptions.pixelEnabled)) {
let pixels = [];
if (serverResponses &&
isArray(serverResponses) &&
serverResponses.length > 0 &&
isPlainObject(serverResponses[0].body) &&
isPlainObject(serverResponses[0].body.ext) &&
isArray(serverResponses[0].body.ext.cm) &&
serverResponses[0].body.ext.cm.length > 0) {
pixels = serverResponses[0].body.ext.cm;
}
const syncs = [];
const query = [];
query.push('cbuster={cbuster}');
query.push('gdpr_consent=' + encodeURIComponent(isPlainObject(gdprConsent) && isStr(gdprConsent?.consentString) ? gdprConsent.consentString : ''));
if (isPlainObject(gdprConsent) && typeof gdprConsent?.gdprApplies === 'boolean' && gdprConsent.gdprApplies) {
query.push('gdpr=1');
} else {
query.push('gdpr=0');
}
if (isPlainObject(uspConsent) && uspConsent?.consentString) {
query.push(`us_privacy=${encodeURIComponent(uspConsent?.consentString)}`);
}
if (isPlainObject(gppConsent) && gppConsent?.gppString) {
query.push(`gppString=${encodeURIComponent(gppConsent?.gppString)}`);
}
if (config.getConfig('coppa')) {
query.push('coppa=1')
}
const q = query.join('&')
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: IFRAME_SYNC_URL + '?' + q.replace('{cbuster}', Math.round(new Date().getTime()))
});
} else if (syncOptions.pixelEnabled) {
if (pixels.length === 0) {
for (let i = 0; i < spb; i++) {
syncs.push({
type: 'image',
url: PIXEL_SYNC_URL + '?' + q.replace('{cbuster}', Math.round(new Date().getTime())) // randomly selects partner if sync required
});
}
} else {
for (let i = 0; i < spb && i < pixels.length; i++) {
syncs.push({
type: 'image',
url: pixels[i] + (pixels[i].indexOf('?') > 0 ? '&' : '?') + q.replace('{cbuster}', Math.round(new Date().getTime()))
});
}
}
}
return syncs;
}
}
};
registerBidder(spec);