forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcertAnalyticsAdapter.js
118 lines (95 loc) · 2.96 KB
/
concertAnalyticsAdapter.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
import { logMessage } from '../src/utils.js';
import {ajax} from '../src/ajax.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import { EVENTS } from '../src/constants.js';
import adapterManager from '../src/adapterManager.js';
const analyticsType = 'endpoint';
// We only want to send about 1% of events for sampling purposes
const SAMPLE_RATE_PERCENTAGE = 1 / 100;
const pageIncludedInSample = sampleAnalytics();
const url = 'https://bids.concert.io/analytics';
const {
BID_RESPONSE,
BID_WON,
AUCTION_END
} = EVENTS;
let queue = [];
let concertAnalytics = Object.assign(adapter({url, analyticsType}), {
track({ eventType, args }) {
switch (eventType) {
case BID_RESPONSE:
if (args.bidder !== 'concert') break;
queue.push(mapBidEvent(eventType, args));
break;
case BID_WON:
if (args.bidder !== 'concert') break;
queue.push(mapBidEvent(eventType, args));
break;
case AUCTION_END:
// Set a delay, as BID_WON events will come after AUCTION_END events
setTimeout(() => sendEvents(), 3000);
break;
default:
break;
}
}
});
function mapBidEvent(eventType, args) {
const { adId, auctionId, cpm, creativeId, width, height, timeToRespond } = args;
const [gamCreativeId, concertRequestId] = getConcertRequestId(creativeId);
const payload = {
event: eventType,
concert_rid: concertRequestId,
adId,
auctionId,
creativeId: gamCreativeId,
position: args.adUnitCode,
url: window.location.href,
cpm,
width,
height,
timeToRespond
}
return payload;
}
/**
* In order to pass back the concert_rid from CBS, it is tucked into the `creativeId`
* slot in the bid response and combined with a pipe `|`. This method splits the creative ID
* and the concert_rid.
*
* @param {string} creativeId
*/
function getConcertRequestId(creativeId) {
if (!creativeId || creativeId.indexOf('|') < 0) return [null, null];
return creativeId.split('|');
}
function sampleAnalytics() {
return Math.random() <= SAMPLE_RATE_PERCENTAGE;
}
function sendEvents() {
concertAnalytics.eventsStorage = queue;
if (!queue.length) return;
if (!pageIncludedInSample) {
logMessage('Page not included in sample for Concert Analytics');
return;
}
try {
const body = JSON.stringify(queue);
ajax(url, () => queue = [], body, {
contentType: 'application/json',
method: 'POST'
});
} catch (err) { logMessage('Concert Analytics error') }
}
// save the base class function
concertAnalytics.originEnableAnalytics = concertAnalytics.enableAnalytics;
concertAnalytics.eventsStorage = [];
// override enableAnalytics so we can get access to the config passed in from the page
concertAnalytics.enableAnalytics = function (config) {
concertAnalytics.originEnableAnalytics(config);
};
adapterManager.registerAnalyticsAdapter({
adapter: concertAnalytics,
code: 'concert'
});
export default concertAnalytics;