forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheightPodAnalyticsAdapter.js
205 lines (178 loc) · 4.97 KB
/
eightPodAnalyticsAdapter.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
import {logError, logInfo, 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';
import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js'
import {getStorageManager} from '../src/storageManager.js';
const analyticsType = 'endpoint';
const MODULE_NAME = `eightPod`;
const MODULE = `${MODULE_NAME}AnalyticProvider`;
/**
* Custom tracking server that gets internal events from EightPod's ad unit
*/
const trackerUrl = 'https://demo.8pod.com/tracker/track';
export const storage = getStorageManager({moduleType: MODULE_TYPE_ANALYTICS, moduleName: MODULE_NAME})
const {
BID_WON
} = EVENTS;
export let queue = [];
let context = {};
/**
* Create eightPod Analytic adapter
*/
let eightPodAnalytics = Object.assign(adapter({url: trackerUrl, analyticsType}), {
/**
* Execute on bid won - setup basic settings, save context about EightPod's bid. We will send it with our events later
*/
track({ eventType, args }) {
switch (eventType) {
case BID_WON:
if (args.bidder === 'eightPod') {
context[args.adUnitCode] = makeContext(args);
eightPodAnalytics.setupPage(args);
break;
}
}
},
/**
* Execute on bid won upload events from local storage
*/
setupPage() {
queue = this.getEventFromLocalStorage();
},
/**
* Subscribe on internal ad unit tracking events
*/
eventSubscribe() {
window.addEventListener('message', async (event) => {
const data = event.data;
const frameElement = event.source?.frameElement;
const parentElement = frameElement?.parentElement;
const adUnitCode = parentElement?.id;
trackEvent(data, adUnitCode);
});
if (!this._interval) {
this._interval = setInterval(sendEvents, 10_000);
}
},
resetQueue() {
queue = [];
},
getContext() {
return context;
},
resetContext() {
context = {};
},
getEventFromLocalStorage,
});
/**
* Create context of event, who emits it
*/
function makeContext(args) {
const params = args?.params?.[0];
return {
bidId: args.seatBidId,
variantId: args.creativeId || '',
campaignId: args.cid || '',
publisherId: params.publisherId,
placementId: params.placementId,
};
}
/**
* Create event, add context and push it to queue
*/
export function trackEvent(event, adUnitCode) {
if (!event.detail) {
return;
}
const fullEvent = {
context: eightPodAnalytics.getContext()[adUnitCode],
eventType: event.detail.type,
eventClass: 'adunit',
timestamp: new Date().getTime(),
eventName: event.detail.name,
payload: event.detail.payload
};
logMessage(fullEvent);
addEvent(fullEvent);
}
/**
* Push event to queue, save event list in local storage
*/
function addEvent(eventPayload) {
queue.push(eventPayload);
storage.setDataInLocalStorage(`EIGHT_POD_EVENTS`, JSON.stringify(queue), null);
}
/**
* Gets previously saved event that has not been sent
*/
function getEventFromLocalStorage() {
const storedEvents = storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage('EIGHT_POD_EVENTS') : null;
if (storedEvents) {
return JSON.parse(storedEvents);
} else {
return [];
}
}
/**
* Send event to our custom tracking server and reset queue
*/
function sendEvents() {
eightPodAnalytics.eventsStorage = queue;
if (queue.length) {
try {
sendEventsApi(queue, {
success: () => {
resetLocalStorage();
eightPodAnalytics.resetQueue();
},
error: (e) => {
logError(MODULE, 'Cant send events', e);
}
})
} catch (e) {
logError(MODULE, 'Cant send events', e);
}
}
}
/**
* Send event to our custom tracking server
*/
function sendEventsApi(eventList, callbacks) {
ajax(trackerUrl, callbacks, JSON.stringify(eventList), {keepalive: true});
}
/**
* Remove saved events in success scenario
*/
const resetLocalStorage = () => {
storage.setDataInLocalStorage(`EIGHT_POD_EVENTS`, JSON.stringify([]), null);
}
// save the base class function
eightPodAnalytics.originEnableAnalytics = eightPodAnalytics.enableAnalytics;
eightPodAnalytics.eventsStorage = [];
// override enableAnalytics so we can get access to the config passed in from the page
// Subscribe on events from adUnit
eightPodAnalytics.enableAnalytics = function (config) {
eightPodAnalytics.originEnableAnalytics(config);
logInfo(MODULE, 'init', config);
eightPodAnalytics.eventSubscribe();
};
eightPodAnalytics.disableAnalytics = ((orig) => {
return function () {
if (this._interval) {
clearInterval(this._interval);
this._interval = null;
}
return orig.apply(this, arguments);
}
})(eightPodAnalytics.disableAnalytics)
/**
* Register Analytics Adapter
*/
adapterManager.registerAnalyticsAdapter({
adapter: eightPodAnalytics,
code: MODULE_NAME
});
export default eightPodAnalytics;