forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bidwatchAnalyticsAdapter.js
239 lines (219 loc) · 8.33 KB
/
bidwatchAnalyticsAdapter.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { EVENTS } from '../src/constants.js';
import { ajax } from '../src/ajax.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { deepClone } from '../src/utils.js';
const analyticsType = 'endpoint';
const url = 'URL_TO_SERVER_ENDPOINT';
const {
AUCTION_END,
BID_WON,
BID_RESPONSE,
BID_REQUESTED,
BID_TIMEOUT,
} = EVENTS;
let saveEvents = {}
let allEvents = {}
let auctionEnd = {}
let initOptions = {}
let endpoint = 'https://default'
let requestsAttributes = ['adUnitCode', 'auctionId', 'bidder', 'bidderCode', 'bidId', 'cpm', 'creativeId', 'currency', 'width', 'height', 'mediaType', 'netRevenue', 'originalCpm', 'originalCurrency', 'requestId', 'size', 'source', 'status', 'timeToRespond', 'transactionId', 'ttl', 'sizes', 'mediaTypes', 'src', 'params', 'userId', 'labelAny', 'bids', 'adId'];
function getAdapterNameForAlias(aliasName) {
return adapterManager.aliasRegistry[aliasName] || aliasName;
}
function filterAttributes(arg, removead) {
let response = {};
if (typeof arg == 'object') {
if (typeof arg['bidderCode'] == 'string') {
response['originalBidder'] = getAdapterNameForAlias(arg['bidderCode']);
} else if (typeof arg['bidder'] == 'string') {
response['originalBidder'] = getAdapterNameForAlias(arg['bidder']);
}
if (!removead && typeof arg['ad'] != 'undefined') {
response['ad'] = arg['ad'];
}
if (typeof arg['gdprConsent'] != 'undefined') {
response['gdprConsent'] = {};
if (typeof arg['gdprConsent']['consentString'] != 'undefined') { response['gdprConsent']['consentString'] = arg['gdprConsent']['consentString']; }
}
if (typeof arg['meta'] == 'object' && typeof arg['meta']['advertiserDomains'] != 'undefined') {
response['meta'] = {'advertiserDomains': arg['meta']['advertiserDomains']};
}
requestsAttributes.forEach((attr) => {
if (typeof arg[attr] != 'undefined') { response[attr] = arg[attr]; }
});
if (typeof response['creativeId'] == 'number') { response['creativeId'] = response['creativeId'].toString(); }
}
return response;
}
function cleanAuctionEnd(args) {
let response = {};
let filteredObj;
let objects = ['bidderRequests', 'bidsReceived', 'noBids', 'adUnits'];
objects.forEach((attr) => {
if (Array.isArray(args[attr])) {
response[attr] = [];
args[attr].forEach((obj) => {
filteredObj = filterAttributes(obj, true);
if (typeof obj['bids'] == 'object') {
filteredObj['bids'] = [];
obj['bids'].forEach((bid) => {
filteredObj['bids'].push(filterAttributes(bid, true));
});
}
response[attr].push(filteredObj);
});
}
});
return response;
}
function cleanCreatives(args) {
let stringArgs = JSON.parse(dereferenceWithoutRenderer(args));
return filterAttributes(stringArgs, false);
}
function enhanceMediaType(arg) {
saveEvents['bidRequested'].forEach((bidRequested) => {
if (bidRequested['auctionId'] == arg['auctionId'] && Array.isArray(bidRequested['bids'])) {
bidRequested['bids'].forEach((bid) => {
if (bid['transactionId'] == arg['transactionId'] && bid['bidId'] == arg['requestId']) { arg['mediaTypes'] = bid['mediaTypes']; }
});
}
});
return arg;
}
function addBidResponse(args) {
let eventType = BID_RESPONSE;
let argsCleaned = cleanCreatives(args); ;
if (allEvents[eventType] == undefined) { allEvents[eventType] = [] }
allEvents[eventType].push(argsCleaned);
}
function addBidRequested(args) {
let eventType = BID_REQUESTED;
let argsCleaned = filterAttributes(args, true);
if (saveEvents[eventType] == undefined) { saveEvents[eventType] = [] }
saveEvents[eventType].push(argsCleaned);
}
function addTimeout(args) {
let eventType = BID_TIMEOUT;
if (saveEvents[eventType] == undefined) { saveEvents[eventType] = [] }
saveEvents[eventType].push(args);
let argsCleaned = [];
let argsDereferenced = {}
let stringArgs = JSON.parse(dereferenceWithoutRenderer(args));
argsDereferenced = stringArgs;
argsDereferenced.forEach((attr) => {
argsCleaned.push(filterAttributes(deepClone(attr), false));
});
if (auctionEnd[eventType] == undefined) { auctionEnd[eventType] = [] }
auctionEnd[eventType].push(argsCleaned);
}
export const dereferenceWithoutRenderer = function(args) {
if (args.renderer) {
let tmp = args.renderer;
delete args.renderer;
let stringified = JSON.stringify(args);
args['renderer'] = tmp;
return stringified;
}
if (args.bidsReceived) {
let tmp = {}
for (let key in args.bidsReceived) {
if (args.bidsReceived[key].renderer) {
tmp[key] = args.bidsReceived[key].renderer;
delete args.bidsReceived[key].renderer;
}
}
let stringified = JSON.stringify(args);
for (let key in tmp) {
args.bidsReceived[key].renderer = tmp[key];
}
return stringified;
}
return JSON.stringify(args);
}
function addAuctionEnd(args) {
let eventType = AUCTION_END;
if (saveEvents[eventType] == undefined) { saveEvents[eventType] = [] }
saveEvents[eventType].push(args);
let argsCleaned = cleanAuctionEnd(JSON.parse(dereferenceWithoutRenderer(args)));
if (auctionEnd[eventType] == undefined) { auctionEnd[eventType] = [] }
auctionEnd[eventType].push(argsCleaned);
}
function handleBidWon(args) {
args = enhanceMediaType(filterAttributes(JSON.parse(dereferenceWithoutRenderer(args)), true));
let increment = args['cpm'];
if (typeof saveEvents['auctionEnd'] == 'object') {
saveEvents['auctionEnd'].forEach((auction) => {
if (auction['auctionId'] == args['auctionId'] && typeof auction['bidsReceived'] == 'object') {
auction['bidsReceived'].forEach((bid) => {
if (bid['transactionId'] == args['transactionId'] && bid['adId'] != args['adId']) {
if (args['cpm'] < bid['cpm']) {
increment = 0;
} else if (increment > args['cpm'] - bid['cpm']) {
increment = args['cpm'] - bid['cpm'];
}
}
});
}
});
}
args['cpmIncrement'] = increment;
args['referer'] = encodeURIComponent(getRefererInfo().page || getRefererInfo().topmostLocation);
if (typeof saveEvents.bidRequested == 'object' && saveEvents.bidRequested.length > 0 && saveEvents.bidRequested[0].gdprConsent) { args.gdpr = saveEvents.bidRequested[0].gdprConsent; }
ajax(endpoint + '.bidwatch.io/analytics/bid_won', null, JSON.stringify(args), {method: 'POST', withCredentials: true});
}
function handleAuctionEnd() {
ajax(endpoint + '.bidwatch.io/analytics/auctions', function (data) {
let list = JSON.parse(data);
if (Array.isArray(list) && typeof allEvents['bidResponse'] != 'undefined') {
let alreadyCalled = [];
allEvents['bidResponse'].forEach((bidResponse) => {
let tmpId = bidResponse['originalBidder'] + '_' + bidResponse['creativeId'];
if (list.includes(tmpId) && !alreadyCalled.includes(tmpId)) {
alreadyCalled.push(tmpId);
ajax(endpoint + '.bidwatch.io/analytics/creatives', null, JSON.stringify(bidResponse), {method: 'POST', withCredentials: true});
}
});
}
allEvents = {};
}, JSON.stringify(auctionEnd), {method: 'POST', withCredentials: true});
auctionEnd = {};
}
let bidwatchAnalytics = Object.assign(adapter({url, analyticsType}), {
track({
eventType,
args
}) {
switch (eventType) {
case AUCTION_END:
addAuctionEnd(args);
handleAuctionEnd();
break;
case BID_WON:
handleBidWon(args);
break;
case BID_RESPONSE:
addBidResponse(args);
break;
case BID_REQUESTED:
addBidRequested(args);
break;
case BID_TIMEOUT:
addTimeout(args);
break;
}
}});
// save the base class function
bidwatchAnalytics.originEnableAnalytics = bidwatchAnalytics.enableAnalytics;
// override enableAnalytics so we can get access to the config passed in from the page
bidwatchAnalytics.enableAnalytics = function (config) {
bidwatchAnalytics.originEnableAnalytics(config); // call the base class function
initOptions = config.options;
if (initOptions.domain) { endpoint = 'https://' + initOptions.domain; }
};
adapterManager.registerAnalyticsAdapter({
adapter: bidwatchAnalytics,
code: 'bidwatch'
});
export default bidwatchAnalytics;