Skip to content

Commit 974cea2

Browse files
committed
prebid#24 Adding a first untested implementation
1 parent d7b984f commit 974cea2

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

modules/id5AnalyticsAdapter.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// import {ajax} from '../src/ajax';
2+
import buildAdapter from '../src/AnalyticsAdapter.js';
3+
import CONSTANTS from '../src/constants.json';
4+
import adapterManager from '../src/adapterManager.js';
5+
import { ajax } from '../src/ajax.js';
6+
const utils = require('../src/utils.js');
7+
8+
const GVLID = 131;
9+
const EVENTS_TO_TRACK = [
10+
CONSTANTS.EVENTS.AUCTION_END,
11+
CONSTANTS.EVENTS.AUCTION_INIT,
12+
CONSTANTS.EVENTS.SET_TARGETING,
13+
CONSTANTS.EVENTS.TCF2_ENFORCEMENT,
14+
];
15+
const FLUSH_EVENTS = [
16+
CONSTANTS.EVENTS.TCF2_ENFORCEMENT,
17+
CONSTANTS.EVENTS.AUCTION_END,
18+
];
19+
const CONFIG_URL_PREFIX = 'https://api.id5-sync.com/analytics/'
20+
const TZ = new Date().getTimezoneOffset();
21+
const PBJS_VERSION = $$PREBID_GLOBAL$$.version;
22+
23+
let id5Analytics = Object.assign(buildAdapter({analyticsType: 'endpoint'}), {
24+
eventBuffer: {},
25+
track: (event) => {
26+
const _this = id5Analytics;
27+
const auctionId = event.args.auctionId;
28+
29+
if (EVENTS_TO_TRACK.indexOf(event.eventType) < 0) {
30+
return;
31+
}
32+
33+
try {
34+
_this.eventBuffer[auctionId] = _this.eventBuffer[auctionId] || [];
35+
36+
// Collect events and send them in a batch when the auction ends
37+
const que = _this.eventBuffer[auctionId];
38+
que.push(_this.makeEvent('PBJS_' + event.eventType.event.args));
39+
40+
if (FLUSH_EVENTS.indexOf(event.eventType) >= 0) {
41+
// Auction ended. Send the batch of collected events
42+
_this.sendEvents(que);
43+
44+
// From now on just send events to server side as they come
45+
que.push = (pushedEvent) => _this.sendEvents([pushedEvent]);
46+
}
47+
} catch (error) {
48+
utils.logError('id5Analytics', error);
49+
_this.sendErrorEvent(error);
50+
}
51+
},
52+
53+
sendEvents: (events) => {
54+
const _this = id5Analytics;
55+
events.forEach((event) => ajax(_this.options.backEndUrl, null, event));
56+
},
57+
58+
makeEvent: (event, payload) => {
59+
const _this = id5Analytics;
60+
return {
61+
event,
62+
payload,
63+
partnerId: _this.options.partnerId,
64+
meta: {
65+
sampling: _this.options.id5Sampling,
66+
pbjs: PBJS_VERSION,
67+
tz: TZ,
68+
}
69+
};
70+
},
71+
72+
sendErrorEvent: (error) => {
73+
const _this = id5Analytics;
74+
_this.sendEvents([
75+
_this.makeEvent('PBJS_ANALYTICS_ERROR', {
76+
message: error.message,
77+
stack: error.stack,
78+
})
79+
]);
80+
},
81+
});
82+
83+
// save the base class function
84+
id5Analytics.originEnableAnalytics = id5Analytics.enableAnalytics;
85+
86+
// override enableAnalytics so we can get access to the config passed in from the page
87+
id5Analytics.enableAnalytics = (config) => {
88+
id5Analytics.options = typeof config === 'object' ? config.options : {};
89+
90+
// We do our own sampling strategy (see AnalyticsAdapter.js)
91+
id5Analytics.options.sampling = undefined;
92+
93+
const partnerId = id5Analytics.options.partnerId;
94+
if (typeof partnerId !== 'number') {
95+
utils.logWarn('id5Analytics: cannot find partnerId in config.options');
96+
return;
97+
}
98+
99+
ajax(`${CONFIG_URL_PREFIX}/${partnerId}/pbjs`, (result) => {
100+
// Store our sampling in id5Sampling as opposed to standard property sampling
101+
102+
const sampling = id5Analytics.options.id5Sampling =
103+
typeof result.sampling === 'number' ? result.sampling : 0;
104+
105+
if (typeof result.evtUrl !== 'string') {
106+
utils.logWarn('id5Analytics: cannot find evtUrl in config endpoint response');
107+
return;
108+
}
109+
id5Analytics.options.backEndUrl = result.evtUrl;
110+
111+
if (sampling > 0 && Math.random() < (1 / sampling)) {
112+
// Init the module only if we got lucky
113+
utils.logInfo('id5Analytics: starting up!')
114+
id5Analytics.originEnableAnalytics(config); // call the base class function
115+
}
116+
});
117+
};
118+
119+
adapterManager.registerAnalyticsAdapter({
120+
adapter: id5Analytics,
121+
code: 'id5Analytics',
122+
gvlid: GVLID
123+
});
124+
125+
export default id5Analytics;

0 commit comments

Comments
 (0)