forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubxaiRtdProvider.js
166 lines (153 loc) · 4.77 KB
/
pubxaiRtdProvider.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
import { ajax } from '../src/ajax.js';
import { config } from '../src/config.js';
import { submodule } from '../src/hook.js';
import { deepAccess } from '../src/utils.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import { getStorageManager } from '../src/storageManager.js';
/**
* This RTD module has a dependency on the priceFloors module.
* We utilize the createFloorsDataForAuction function from the priceFloors module to incorporate price floors data into the current auction.
*/
import { createFloorsDataForAuction } from './priceFloors.js'; // eslint-disable-line prebid/validate-imports
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'pubxai';
window.__pubxFloorRulesPromise__ = null;
export const FloorsApiStatus = Object.freeze({
IN_PROGRESS: 'IN_PROGRESS',
SUCCESS: 'SUCCESS',
ERROR: 'ERROR',
});
export const storage = getStorageManager({ moduleType: MODULE_TYPE_RTD, moduleName: SUBMODULE_NAME });
export const FLOORS_EVENT_HANDLE = 'floorsApi';
export const FLOORS_END_POINT = 'https://floor.pbxai.com/';
export const FLOOR_PROVIDER = 'PubxFloorProvider';
export const getFloorsConfig = (provider, floorsResponse) => {
const floorsConfig = {
floors: {
enforcement: { floorDeals: true },
data: floorsResponse,
},
};
const { floorMin, enforcement } = deepAccess(provider, 'params');
if (floorMin) {
floorsConfig.floors.floorMin = floorMin;
}
if (enforcement) {
floorsConfig.floors.enforcement = enforcement;
}
return floorsConfig;
};
export const setFloorsConfig = (provider, data) => {
if (data) {
const floorsConfig = getFloorsConfig(provider, data);
config.setConfig(floorsConfig);
window.__pubxLoaded__ = true;
window.__pubxFloorsConfig__ = floorsConfig;
} else {
config.setConfig({ floors: window.__pubxPrevFloorsConfig__ });
window.__pubxLoaded__ = false;
window.__pubxFloorsConfig__ = null;
}
};
export const setDefaultPriceFloors = (provider) => {
const { data } = deepAccess(provider, 'params');
if (data !== undefined) {
data.floorProvider = FLOOR_PROVIDER;
setFloorsConfig(provider, data);
}
};
export const setPriceFloors = async (provider) => {
window.__pubxPrevFloorsConfig__ = config.getConfig('floors');
setDefaultPriceFloors(provider);
return fetchFloorRules(provider)
.then((floorsResponse) => {
setFloorsConfig(provider, floorsResponse);
setFloorsApiStatus(FloorsApiStatus.SUCCESS);
})
.catch((_) => {
setFloorsApiStatus(FloorsApiStatus.ERROR);
});
};
export const setFloorsApiStatus = (status) => {
window.__pubxFloorsApiStatus__ = status;
window.dispatchEvent(
new CustomEvent(FLOORS_EVENT_HANDLE, { detail: { status } })
);
};
export const getUrl = (provider) => {
const { pubxId, endpoint } = deepAccess(provider, 'params');
if (!endpoint) {
return null; // Indicate that no endpoint is provided
}
return `${endpoint || FLOORS_END_POINT}?pubxId=${pubxId}&page=${window.location.href}`;
};
export const fetchFloorRules = async (provider) => {
return new Promise((resolve, reject) => {
setFloorsApiStatus(FloorsApiStatus.IN_PROGRESS);
const url = getUrl(provider);
if (url) {
// Fetch from remote endpoint
ajax(url, {
success: (responseText, response) => {
try {
if (response && response.response) {
const floorsResponse = JSON.parse(response.response);
resolve(floorsResponse);
} else {
resolve(null);
}
} catch (error) {
reject(error);
}
},
error: (responseText, response) => {
reject(response);
},
});
} else {
// Fetch from local storage
try {
const localData = storage.getDataFromSessionStorage('pubx:dynamicFloors') || window.__pubxDynamicFloors__;
if (localData) {
resolve(JSON.parse(localData));
} else {
resolve(null);
}
} catch (error) {
reject(error);
}
}
});
};
const init = (provider) => {
window.__pubxFloorRulesPromise__ = setPriceFloors(provider);
return true;
};
const getBidRequestData = (() => {
let floorsAttached = false;
return (reqBidsConfigObj, onDone) => {
if (!floorsAttached) {
createFloorsDataForAuction(
reqBidsConfigObj.adUnits,
reqBidsConfigObj.auctionId
);
window.__pubxFloorRulesPromise__.then(() => {
createFloorsDataForAuction(
reqBidsConfigObj.adUnits,
reqBidsConfigObj.auctionId
);
onDone();
});
floorsAttached = true;
}
};
})();
export const pubxaiSubmodule = {
name: SUBMODULE_NAME,
init,
getBidRequestData,
};
export const beforeInit = () => {
submodule(MODULE_NAME, pubxaiSubmodule);
};
beforeInit();