-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
airgridRtdProvider.js
161 lines (148 loc) · 4.12 KB
/
airgridRtdProvider.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
/**
* This module adds the AirGrid provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will fetch real-time audience data from AirGrid
* @module modules/airgridRtdProvider
* @requires module:modules/realTimeData
*/
import { config } from '../src/config.js';
import { submodule } from '../src/hook.js';
import {
deepSetValue,
deepAccess,
} from '../src/utils.js';
import { getGlobal } from '../src/prebidGlobal.js';
import {getStorageManager} from '../src/storageManager.js';
import {MODULE_TYPE_RTD} from '../src/activities/modules.js';
const MODULE_NAME = 'realTimeData';
const SUBMODULE_NAME = 'airgrid';
const AG_TCF_ID = 782;
export const AG_AUDIENCE_IDS_KEY = 'edkt_matched_audience_ids';
export const storage = getStorageManager({
moduleType: MODULE_TYPE_RTD,
moduleName: SUBMODULE_NAME,
});
/**
* Attach script tag to DOM
* @param {Object} rtdConfig
* @return {void}
*/
export function attachScriptTagToDOM(rtdConfig) {
var edktInitializor = (window.edktInitializor = window.edktInitializor || {});
if (!edktInitializor.invoked) {
edktInitializor.invoked = true;
edktInitializor.accountId = rtdConfig.params.accountId;
edktInitializor.publisherId = rtdConfig.params.publisherId;
edktInitializor.apiKey = rtdConfig.params.apiKey;
edktInitializor.load = function (e) {
var p = e || 'sdk';
var n = document.createElement('script');
n.type = 'module';
n.async = true;
n.src = 'https://cdn.edkt.io/' + p + '/edgekit.min.js';
document.getElementsByTagName('head')[0].appendChild(n);
};
edktInitializor.load(edktInitializor.accountId);
}
}
/**
* Fetch audiences from localStorage
* @return {Array}
*/
export function getMatchedAudiencesFromStorage() {
const audiences = storage.getDataFromLocalStorage(AG_AUDIENCE_IDS_KEY);
if (!audiences) return [];
try {
return JSON.parse(audiences);
} catch (e) {
return [];
}
}
/**
* Mutates the adUnits object
* @param {Object} adUnits
* @param {Array} audiences
* @return {void}
*/
function setAudiencesToAppNexusAdUnits(adUnits, audiences) {
adUnits.forEach((adUnit) => {
adUnit.bids.forEach((bid) => {
if (bid.bidder && bid.bidder === 'appnexus') {
deepSetValue(bid, 'params.keywords.perid', audiences || []);
}
});
});
}
/**
* Pass audience data to configured bidders, using ORTB2
* @param {Object} rtdConfig
* @param {Array} audiences
* @return {{}} a map from bidder code to ORTB2 config
*/
export function setAudiencesAsBidderOrtb2(rtdConfig, audiences) {
const bidders = deepAccess(rtdConfig, 'params.bidders');
if (!bidders || bidders.length === 0 || !audiences || audiences.length === 0) return;
const keywords = audiences.map(
(audienceId) => `perid=${audienceId}`
).join(',');
config.mergeBidderConfig({
bidders: bidders,
config: {
ortb2: {
site: {
keywords,
}
}
}
})
}
export function setAudiencesUsingAppNexusAuctionKeywords(audiences) {
config.setConfig({
appnexusAuctionKeywords: {
perid: audiences,
},
});
}
/**
* Module init
* @param {Object} rtdConfig
* @param {Object} userConsent
* @return {boolean}
*/
function init(rtdConfig, userConsent) {
attachScriptTagToDOM(rtdConfig);
return true;
}
/**
* Real-time data retrieval from AirGrid
* @param {Object} reqBidsConfigObj
* @param {function} onDone
* @param {Object} rtdConfig
* @param {Object} userConsent
* @return {void}
*/
export function passAudiencesToBidders(
bidConfig,
onDone,
rtdConfig,
userConsent
) {
const adUnits = bidConfig.adUnits || getGlobal().adUnits;
const audiences = getMatchedAudiencesFromStorage();
if (audiences.length > 0) {
setAudiencesUsingAppNexusAuctionKeywords(audiences);
setAudiencesAsBidderOrtb2(rtdConfig, audiences)
if (adUnits) {
setAudiencesToAppNexusAdUnits(adUnits, audiences);
}
}
onDone();
}
/** @type {RtdSubmodule} */
export const airgridSubmodule = {
name: SUBMODULE_NAME,
init: init,
getBidRequestData: passAudiencesToBidders,
gvlid: AG_TCF_ID
};
submodule(MODULE_NAME, airgridSubmodule);