forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid5AnalyticsAdapter.js
309 lines (267 loc) · 9.32 KB
/
id5AnalyticsAdapter.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import buildAdapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import { EVENTS } from '../src/constants.js';
import adapterManager from '../src/adapterManager.js';
import { ajax } from '../src/ajax.js';
import { logInfo, logError } from '../src/utils.js';
import * as events from '../src/events.js';
const {
AUCTION_END,
TCF2_ENFORCEMENT,
BID_WON,
BID_VIEWABLE,
AD_RENDER_FAILED
} = EVENTS
const GVLID = 131;
const STANDARD_EVENTS_TO_TRACK = [
AUCTION_END,
TCF2_ENFORCEMENT,
BID_WON,
];
// These events cause the buffered events to be sent over
const FLUSH_EVENTS = [
TCF2_ENFORCEMENT,
AUCTION_END,
BID_WON,
BID_VIEWABLE,
AD_RENDER_FAILED
];
const CONFIG_URL_PREFIX = 'https://api.id5-sync.com/analytics'
const TZ = new Date().getTimezoneOffset();
const PBJS_VERSION = 'v' + '$prebid.version$';
const ID5_REDACTED = '__ID5_REDACTED__';
const isArray = Array.isArray;
let id5Analytics = Object.assign(buildAdapter({analyticsType: 'endpoint'}), {
// Keeps an array of events for each auction
eventBuffer: {},
eventsToTrack: STANDARD_EVENTS_TO_TRACK,
track: (event) => {
const _this = id5Analytics;
if (!event || !event.args) {
return;
}
try {
const auctionId = event.args.auctionId;
_this.eventBuffer[auctionId] = _this.eventBuffer[auctionId] || [];
// Collect events and send them in a batch when the auction ends
const que = _this.eventBuffer[auctionId];
que.push(_this.makeEvent(event.eventType, event.args));
if (FLUSH_EVENTS.indexOf(event.eventType) >= 0) {
// Auction ended. Send the batch of collected events
_this.sendEvents(que);
// From now on just send events to server side as they come
que.push = (pushedEvent) => _this.sendEvents([pushedEvent]);
}
} catch (error) {
logError('id5Analytics: ERROR', error);
_this.sendErrorEvent(error);
}
},
sendEvents: (eventsToSend) => {
const _this = id5Analytics;
// By giving some content this will be automatically a POST
eventsToSend.forEach((event) =>
ajax(_this.options.ingestUrl, null, JSON.stringify(event)));
},
makeEvent: (event, payload) => {
const _this = id5Analytics;
const filteredPayload = deepTransformingClone(payload,
transformFnFromCleanupRules(event));
return {
source: 'pbjs',
event,
payload: filteredPayload,
partnerId: _this.options.partnerId,
meta: {
sampling: _this.options.id5Sampling,
pbjs: PBJS_VERSION,
tz: TZ,
}
};
},
sendErrorEvent: (error) => {
const _this = id5Analytics;
_this.sendEvents([
_this.makeEvent('analyticsError', {
message: error.message,
stack: error.stack,
})
]);
},
random: () => Math.random(),
});
const ENABLE_FUNCTION = (config) => {
const _this = id5Analytics;
_this.options = (config && config.options) || {};
const partnerId = _this.options.partnerId;
if (typeof partnerId !== 'number') {
logError('id5Analytics: partnerId in config.options must be a number representing the id5 partner ID');
return;
}
ajax(`${CONFIG_URL_PREFIX}/${partnerId}/pbjs`, (result) => {
logInfo('id5Analytics: Received from configuration endpoint', result);
const configFromServer = JSON.parse(result);
const sampling = _this.options.id5Sampling =
typeof configFromServer.sampling === 'number' ? configFromServer.sampling : 0;
if (typeof configFromServer.ingestUrl !== 'string') {
logError('id5Analytics: cannot find ingestUrl in config endpoint response; no analytics will be available');
return;
}
_this.options.ingestUrl = configFromServer.ingestUrl;
// 3-way fallback for which events to track: server > config > standard
_this.eventsToTrack = configFromServer.eventsToTrack || _this.options.eventsToTrack || STANDARD_EVENTS_TO_TRACK;
_this.eventsToTrack = isArray(_this.eventsToTrack) ? _this.eventsToTrack : STANDARD_EVENTS_TO_TRACK;
logInfo('id5Analytics: Configuration is', _this.options);
logInfo('id5Analytics: Tracking events', _this.eventsToTrack);
if (sampling > 0 && _this.random() < (1 / sampling)) {
// Init the module only if we got lucky
logInfo('id5Analytics: Selected by sampling. Starting up!');
// Clean start
_this.eventBuffer = {};
// Replay all events until now
if (!config.disablePastEventsProcessing) {
events.getEvents().forEach((event) => {
if (event && _this.eventsToTrack.indexOf(event.eventType) >= 0) {
_this.track(event);
}
});
}
// Merge in additional cleanup rules
if (configFromServer.additionalCleanupRules) {
const newRules = configFromServer.additionalCleanupRules;
_this.eventsToTrack.forEach((key) => {
// Some protective checks in case we mess up server side
if (
isArray(newRules[key]) &&
newRules[key].every((eventRules) =>
isArray(eventRules.match) &&
(eventRules.apply in TRANSFORM_FUNCTIONS))
) {
logInfo('id5Analytics: merging additional cleanup rules for event ' + key);
CLEANUP_RULES[key].push(...newRules[key]);
}
});
}
// Register to the events of interest
_this.handlers = {};
_this.eventsToTrack.forEach((eventType) => {
const handler = _this.handlers[eventType] = (args) =>
_this.track({ eventType, args });
events.on(eventType, handler);
});
}
});
// Make only one init possible within a lifecycle
_this.enableAnalytics = () => {};
};
id5Analytics.enableAnalytics = ENABLE_FUNCTION;
id5Analytics.disableAnalytics = () => {
const _this = id5Analytics;
// Un-register to the events of interest
_this.eventsToTrack.forEach((eventType) => {
if (_this.handlers && _this.handlers[eventType]) {
events.off(eventType, _this.handlers[eventType]);
}
});
// Make re-init possible. Work around the fact that past events cannot be forgotten
_this.enableAnalytics = (config) => {
config.disablePastEventsProcessing = true;
ENABLE_FUNCTION(config);
};
};
adapterManager.registerAnalyticsAdapter({
adapter: id5Analytics,
code: 'id5Analytics',
gvlid: GVLID
});
export default id5Analytics;
function redact(obj, key) {
obj[key] = ID5_REDACTED;
}
function erase(obj, key) {
delete obj[key];
}
// The transform function matches against a path and applies
// required transformation if match is found.
function deepTransformingClone(obj, transform, currentPath = []) {
const result = isArray(obj) ? [] : {};
const recursable = typeof obj === 'object' && obj !== null;
if (recursable) {
const keys = Object.keys(obj);
if (keys.length > 0) {
keys.forEach((key) => {
const newPath = currentPath.concat(key);
result[key] = deepTransformingClone(obj[key], transform, newPath);
transform(newPath, result, key);
});
return result;
}
}
return obj;
}
// Every set of rules is an object where "match" is an array and
// "apply" is the function to apply in case of match. The function to apply
// takes (obj, prop) and transforms property "prop" in object "obj".
// The "match" is an array of path parts. Each part is either a string or an array.
// In case of array, it represents alternatives which all would match.
// Special path part '*' matches any subproperty or array index.
// Prefixing a part with "!" makes it negative match (doesn't work with multiple alternatives)
const CLEANUP_RULES = {};
CLEANUP_RULES[AUCTION_END] = [{
match: [['adUnits', 'bidderRequests'], '*', 'bids', '*', ['userId', 'crumbs'], '!id5id'],
apply: 'redact'
}, {
match: [['adUnits', 'bidderRequests'], '*', 'bids', '*', ['userId', 'crumbs'], 'id5id', 'uid'],
apply: 'redact'
}, {
match: [['adUnits', 'bidderRequests'], '*', 'bids', '*', 'userIdAsEids', '*', 'uids', '*', ['id', 'ext']],
apply: 'redact'
}, {
match: ['bidderRequests', '*', 'gdprConsent', 'vendorData'],
apply: 'erase'
}, {
match: ['bidsReceived', '*', ['ad', 'native']],
apply: 'erase'
}, {
match: ['noBids', '*', ['userId', 'crumbs'], '*'],
apply: 'redact'
}, {
match: ['noBids', '*', 'userIdAsEids', '*', 'uids', '*', ['id', 'ext']],
apply: 'redact'
}];
CLEANUP_RULES[BID_WON] = [{
match: [['ad', 'native']],
apply: 'erase'
}];
const TRANSFORM_FUNCTIONS = {
'redact': redact,
'erase': erase,
};
// Builds a rule function depending on the event type
function transformFnFromCleanupRules(eventType) {
const rules = CLEANUP_RULES[eventType] || [];
return (path, obj, key) => {
for (let i = 0; i < rules.length; i++) {
let match = true;
const ruleMatcher = rules[i].match;
const transformation = rules[i].apply;
if (ruleMatcher.length !== path.length) {
continue;
}
for (let fragment = 0; fragment < ruleMatcher.length && match; fragment++) {
const choices = makeSureArray(ruleMatcher[fragment]);
match = !choices.every((choice) => choice !== '*' &&
(choice.charAt(0) === '!'
? path[fragment] === choice.substring(1)
: path[fragment] !== choice));
}
if (match) {
const transformfn = TRANSFORM_FUNCTIONS[transformation];
transformfn(obj, key);
break;
}
}
};
}
function makeSureArray(object) {
return isArray(object) ? object : [object];
}