-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
azerionedgeRtdProvider.js
143 lines (132 loc) · 3.26 KB
/
azerionedgeRtdProvider.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
/**
* This module adds the Azerion provider to the real time data module of prebid.
*
* The {@link module:modules/realTimeData} module is required
* @module modules/azerionedgeRtdProvider
* @requires module:modules/realTimeData
*/
import { submodule } from '../src/hook.js';
import { mergeDeep } from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
import { loadExternalScript } from '../src/adloader.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
/**
* @typedef {import('./rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
const REAL_TIME_MODULE = 'realTimeData';
const SUBREAL_TIME_MODULE = 'azerionedge';
export const STORAGE_KEY = 'ht-pa-v1-a';
export const storage = getStorageManager({
moduleType: MODULE_TYPE_RTD,
moduleName: SUBREAL_TIME_MODULE,
});
/**
* Get script url to load
*
* @param {Object} config
*
* @return {String}
*/
function getScriptURL(config) {
const VERSION = 'v1';
const key = config.params?.key;
const publisherPath = key ? `${key}/` : '';
return `https://edge.hyth.io/js/${VERSION}/${publisherPath}azerion-edge.min.js`;
}
/**
* Attach script tag to DOM
*
* @param {Object} config
*
* @return {void}
*/
export function attachScript(config) {
const script = getScriptURL(config);
loadExternalScript(script, SUBREAL_TIME_MODULE, () => {
if (typeof window.azerionPublisherAudiences === 'function') {
window.azerionPublisherAudiences(config.params?.process || {});
}
});
}
/**
* Fetch audiences info from localStorage.
*
* @return {Array} Audience ids.
*/
export function getAudiences() {
try {
const data = storage.getDataFromLocalStorage(STORAGE_KEY);
return JSON.parse(data).map(({ id }) => id);
} catch (_) {
return [];
}
}
/**
* Pass audience data to configured bidders, using ORTB2
*
* @param {Object} reqBidsConfigObj
* @param {Object} config
* @param {Array} audiences
*
* @return {void}
*/
export function setAudiencesToBidders(reqBidsConfigObj, config, audiences) {
const defaultBidders = ['improvedigital'];
const bidders = config.params?.bidders || defaultBidders;
bidders.forEach((bidderCode) =>
mergeDeep(reqBidsConfigObj.ortb2Fragments.bidder, {
[bidderCode]: {
user: {
data: [
{
name: 'azerionedge',
ext: { segtax: 4 },
segment: audiences.map((id) => ({ id })),
},
],
},
},
})
);
}
/**
* Module initialisation.
*
* @param {Object} config
* @param {Object} userConsent
*
* @return {boolean}
*/
function init(config, userConsent) {
attachScript(config);
return true;
}
/**
* Real-time user audiences retrieval
*
* @param {Object} reqBidsConfigObj
* @param {function} callback
* @param {Object} config
* @param {Object} userConsent
*
* @return {void}
*/
export function getBidRequestData(
reqBidsConfigObj,
callback,
config,
userConsent
) {
const audiences = getAudiences();
if (audiences.length > 0) {
setAudiencesToBidders(reqBidsConfigObj, config, audiences);
}
callback();
}
/** @type {RtdSubmodule} */
export const azerionedgeSubmodule = {
name: SUBREAL_TIME_MODULE,
init: init,
getBidRequestData: getBidRequestData,
};
submodule(REAL_TIME_MODULE, azerionedgeSubmodule);