forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovertoneRtdProvider.js
79 lines (68 loc) · 2.13 KB
/
overtoneRtdProvider.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
import { submodule } from '../src/hook.js';
import { ajaxBuilder } from '../src/ajax.js';
import { safeJSONParse, logMessage as _logMessage } from '../src/utils.js';
export const OVERTONE_URL = 'https://prebid-1.overtone.ai/contextual';
const logMessage = (...args) => {
_logMessage('Overtone', ...args);
};
export async function fetchContextData(url = window.location.href) {
const pageUrl = encodeURIComponent(url);
const requestUrl = `${OVERTONE_URL}?URL=${pageUrl}&InApp=False`;
const request = window.ajaxBuilder || ajaxBuilder();
return new Promise((resolve, reject) => {
logMessage('Sending request to:', requestUrl);
request(requestUrl, {
success: (response) => {
const data = safeJSONParse(response);
logMessage('Fetched data:', data);
if (!data || typeof data.status !== 'number') {
reject(new Error('Invalid response format'));
return;
}
switch (data.status) {
case 1: // Success
resolve({ categories: data.categories || [] });
break;
case 3: // Fail
case 4: // Ignore
resolve({ categories: [] });
break;
default:
reject(new Error(`Unexpected response status: ${data.status}`));
}
},
error: (err) => {
logMessage('Error during request:', err);
reject(err);
},
});
});
}
function init(config) {
logMessage('init', config);
return true;
}
export const overtoneRtdProvider = {
name: 'overtone',
init: init,
getBidRequestData: function (bidReqConfig, callback) {
fetchContextData()
.then((contextData) => {
if (contextData) {
if (!bidReqConfig.ortb2Fragments.global.site.ext) {
bidReqConfig.ortb2Fragments.global.site.ext = {};
}
bidReqConfig.ortb2Fragments.global.site.ext.data = contextData;
}
callback();
})
.catch((error) => {
logMessage('Error fetching context data', error);
callback();
});
},
};
submodule('realTimeData', overtoneRtdProvider);
export const overtoneModule = {
fetchContextData,
};