-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mediaimpactBidAdapter.js
147 lines (125 loc) · 4.34 KB
/
mediaimpactBidAdapter.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
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { buildBidRequestsAndParams, postRequest, buildEndpointUrl } from '../libraries/mediaImpactUtils/index.js';
const BIDDER_CODE = 'mediaimpact';
export const ENDPOINT_PROTOCOL = 'https';
export const ENDPOINT_DOMAIN = 'bidder.smartytouch.co';
export const ENDPOINT_PATH = '/hb/bid';
export const spec = {
code: BIDDER_CODE,
isBidRequestValid: function (bidRequest) {
return !!parseInt(bidRequest.params.unitId) || !!parseInt(bidRequest.params.partnerId);
},
buildRequests: function (validBidRequests, bidderRequest) {
const referer = bidderRequest?.refererInfo?.page || window.location.href;
// Use the common function to build bidRequests and beaconParams
const { bidRequests, beaconParams } = buildBidRequestsAndParams(validBidRequests, referer);
const adRequestUrl = buildEndpointUrl(
ENDPOINT_PROTOCOL,
ENDPOINT_DOMAIN,
ENDPOINT_PATH,
beaconParams
);
return {
method: 'POST',
url: adRequestUrl,
data: JSON.stringify(bidRequests),
};
},
interpretResponse: function (serverResponse, bidRequest) {
const validBids = JSON.parse(bidRequest.data);
if (typeof serverResponse.body === 'undefined') {
return [];
}
return validBids
.map(bid => ({ bid: bid, ad: serverResponse.body[bid.adUnitCode] }))
.filter(item => item.ad)
.map(item => spec.adResponse(item.bid, item.ad));
},
adResponse: function (bid, ad) {
return {
requestId: bid.bidId,
ad: ad.ad,
cpm: ad.cpm,
width: ad.width,
height: ad.height,
ttl: 60,
creativeId: ad.creativeId,
netRevenue: ad.netRevenue,
currency: ad.currency,
winNotification: ad.winNotification,
meta: ad.meta || {},
};
},
onBidWon: function (data) {
data.winNotification.forEach(function (unitWon) {
const adBidWonUrl = buildEndpointUrl(
ENDPOINT_PROTOCOL,
ENDPOINT_DOMAIN,
unitWon.path
);
if (unitWon.method === 'POST') {
postRequest(adBidWonUrl, JSON.stringify(unitWon.data));
}
});
return true;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent, uspConsent) {
const syncs = [];
if (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled) {
return syncs;
}
let appendGdprParams = function (url, gdprParams) {
if (gdprParams === null) {
return url;
}
return url + (url.indexOf('?') >= 0 ? '&' : '?') + gdprParams;
};
let gdprParams = null;
if (gdprConsent) {
if (typeof gdprConsent.gdprApplies === 'boolean') {
gdprParams = `gdpr=${Number(gdprConsent.gdprApplies)}&gdpr_consent=${gdprConsent.consentString}`;
} else {
gdprParams = `gdpr_consent=${gdprConsent.consentString}`;
}
}
serverResponses.forEach(resp => {
if (resp.body) {
Object.keys(resp.body).map(function(key, index) {
let respObject = resp.body[key];
if (respObject['syncs'] !== undefined &&
Array.isArray(respObject.syncs) &&
respObject.syncs.length > 0) {
if (syncOptions.iframeEnabled) {
respObject.syncs.filter(function (syncIframeObject) {
if (syncIframeObject['type'] !== undefined &&
syncIframeObject['link'] !== undefined &&
syncIframeObject.type === 'iframe') { return true; }
return false;
}).forEach(function (syncIframeObject) {
syncs.push({
type: 'iframe',
url: appendGdprParams(syncIframeObject.link, gdprParams)
});
});
}
if (syncOptions.pixelEnabled) {
respObject.syncs.filter(function (syncImageObject) {
if (syncImageObject['type'] !== undefined &&
syncImageObject['link'] !== undefined &&
syncImageObject.type === 'image') { return true; }
return false;
}).forEach(function (syncImageObject) {
syncs.push({
type: 'image',
url: appendGdprParams(syncImageObject.link, gdprParams)
});
});
}
}
});
}
});
return syncs;
},
};
registerBidder(spec);