forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkargoAnalyticsAdapter.js
96 lines (80 loc) · 2.44 KB
/
kargoAnalyticsAdapter.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
import { logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { EVENTS } from '../src/constants.js';
const EVENT_URL = 'https://krk.kargo.com/api/v1/event';
const KARGO_BIDDER_CODE = 'kargo';
const analyticsType = 'endpoint';
let _initOptions = {};
let _logBidResponseData = {
auctionId: '',
auctionTimeout: 0,
responseTime: 0,
};
let _bidResponseDataLogged = [];
var kargoAnalyticsAdapter = Object.assign(
adapter({ analyticsType }), {
track({ eventType, args }) {
switch (eventType) {
case EVENTS.AUCTION_INIT: {
_logBidResponseData.auctionTimeout = args.timeout;
break;
}
case EVENTS.BID_RESPONSE: {
handleBidResponseData(args);
break;
}
}
}
}
);
// handleBidResponseData: Get auction data for Kargo bids and send to server
function handleBidResponseData (bidResponse) {
// Verify this is Kargo and we haven't seen this auction data yet
if (bidResponse.bidder !== KARGO_BIDDER_CODE || _bidResponseDataLogged.includes(bidResponse.auctionId) !== false) {
return
}
_logBidResponseData.auctionId = bidResponse.auctionId;
_logBidResponseData.responseTime = bidResponse.timeToRespond;
sendAuctionData(_logBidResponseData);
}
// sendAuctionData: Send auction data to the server
function sendAuctionData (data) {
try {
_bidResponseDataLogged.push(data.auctionId);
if (!shouldFireEventRequest()) {
return;
}
ajax(
`${EVENT_URL}/auction-data`,
null,
{
aid: data.auctionId,
ato: data.auctionTimeout,
rt: data.responseTime,
it: 0,
},
{
method: 'GET',
}
);
} catch (err) {
logError('Error sending auction data: ', err);
}
}
// Sampling rate out of 100
function shouldFireEventRequest () {
const samplingRate = (_initOptions && _initOptions.sampling) || 100;
return ((Math.floor(Math.random() * 100) + 1) <= parseInt(samplingRate));
}
kargoAnalyticsAdapter.originEnableAnalytics = kargoAnalyticsAdapter.enableAnalytics;
kargoAnalyticsAdapter.enableAnalytics = function (config) {
_initOptions = config.options;
kargoAnalyticsAdapter.originEnableAnalytics(config);
};
adapterManager.registerAnalyticsAdapter({
adapter: kargoAnalyticsAdapter,
code: 'kargo'
});
export default kargoAnalyticsAdapter;