forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliveIntentAnalyticsAdapter.js
141 lines (126 loc) · 4.08 KB
/
liveIntentAnalyticsAdapter.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
import {ajax} from '../src/ajax.js';
import { generateUUID, logInfo, logWarn } from '../src/utils.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import { EVENTS } from '../src/constants.js';
import adapterManager from '../src/adapterManager.js';
import { auctionManager } from '../src/auctionManager.js';
import { getRefererInfo } from '../src/refererDetection.js';
const ANALYTICS_TYPE = 'endpoint';
const URL = 'https://wba.liadm.com/analytic-events';
const GVL_ID = 148;
const ADAPTER_CODE = 'liveintent';
const DEFAULT_BID_WON_TIMEOUT = 2000;
const { AUCTION_END } = EVENTS;
let bidWonTimeout;
function handleAuctionEnd(args) {
setTimeout(() => {
const auction = auctionManager.index.getAuction(args.auctionId);
const winningBids = (auction) ? auction.getWinningBids() : [];
const data = createAnalyticsEvent(args, winningBids);
sendAnalyticsEvent(data);
}, bidWonTimeout);
}
function getAnalyticsEventBids(bidsReceived) {
return bidsReceived.map(bid => {
return {
adUnitCode: bid.adUnitCode,
timeToRespond: bid.timeToRespond,
cpm: bid.cpm,
currency: bid.currency,
ttl: bid.ttl,
bidder: bid.bidder
};
});
}
function getBannerSizes(banner) {
if (banner && banner.sizes) {
return banner.sizes.map(size => {
const [width, height] = size;
return {w: width, h: height};
});
} else return [];
}
function getUniqueBy(arr, key) {
return [...new Map(arr.map(item => [item[key], item])).values()]
}
function createAnalyticsEvent(args, winningBids) {
let payload = {
instanceId: generateUUID(),
url: getRefererInfo().page,
bidsReceived: getAnalyticsEventBids(args.bidsReceived),
auctionStart: args.timestamp,
auctionEnd: args.auctionEnd,
adUnits: [],
userIds: [],
bidders: []
}
let allUserIds = [];
if (args.adUnits) {
args.adUnits.forEach(unit => {
if (unit.mediaTypes && unit.mediaTypes.banner) {
payload['adUnits'].push({
code: unit.code,
mediaType: 'banner',
sizes: getBannerSizes(unit.mediaTypes.banner),
ortb2Imp: unit.ortb2Imp
});
}
if (unit.bids) {
let userIds = unit.bids.flatMap(getAnalyticsEventUserIds);
allUserIds.push(...userIds);
let bidders = unit.bids.map(({bidder, params}) => {
return { bidder, params }
});
payload['bidders'].push(...bidders);
}
})
let uniqueUserIds = getUniqueBy(allUserIds, 'source');
payload['userIds'] = uniqueUserIds;
}
payload['winningBids'] = getAnalyticsEventBids(winningBids);
payload['auctionId'] = args.auctionId;
return payload;
}
function getAnalyticsEventUserIds(bid) {
if (bid && bid.userIdAsEids) {
return bid.userIdAsEids.map(({source, uids, ext}) => {
let analyticsEventUserId = {source, uids, ext};
return ignoreUndefined(analyticsEventUserId)
});
} else { return []; }
}
function sendAnalyticsEvent(data) {
ajax(URL, {
success: function () {
logInfo('LiveIntent Prebid Analytics: send data success');
},
error: function (e) {
logWarn('LiveIntent Prebid Analytics: send data error' + e);
}
}, JSON.stringify(data), {
contentType: 'application/json',
method: 'POST'
})
}
function ignoreUndefined(data) {
const filteredData = Object.entries(data).filter(([key, value]) => value)
return Object.fromEntries(filteredData)
}
let liAnalytics = Object.assign(adapter({URL, ANALYTICS_TYPE}), {
track({ eventType, args }) {
if (eventType == AUCTION_END && args) { handleAuctionEnd(args); }
}
});
// save the base class function
liAnalytics.originEnableAnalytics = liAnalytics.enableAnalytics;
// override enableAnalytics so we can get access to the config passed in from the page
liAnalytics.enableAnalytics = function (config) {
bidWonTimeout = config?.options?.bidWonTimeout ?? DEFAULT_BID_WON_TIMEOUT;
liAnalytics.originEnableAnalytics(config); // call the base class function
};
adapterManager.registerAnalyticsAdapter({
adapter: liAnalytics,
code: ADAPTER_CODE,
gvlid: GVL_ID
});
export default liAnalytics;