forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameraRtdProvider.js
107 lines (94 loc) · 3.92 KB
/
gameraRtdProvider.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
import { submodule } from '../src/hook.js';
import { getGlobal } from '../src/prebidGlobal.js';
import {
isPlainObject,
logError,
mergeDeep,
deepClone,
} from '../src/utils.js';
const MODULE_NAME = 'gamera';
const MODULE = `${MODULE_NAME}RtdProvider`;
/**
* Initialize the Gamera RTD Module.
* @param {Object} config
* @param {Object} userConsent
* @returns {boolean}
*/
function init(config, userConsent) {
return true;
}
/**
* Modify bid request data before auction
* @param {Object} reqBidsConfigObj - The bid request config object
* @param {function} callback - Callback function to execute after data handling
* @param {Object} config - Module configuration
* @param {Object} userConsent - User consent data
*/
function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) {
// Check if window.gamera.getPrebidSegments is available
if (typeof window.gamera?.getPrebidSegments !== 'function') {
window.gamera = window.gamera || {};
window.gamera.cmd = window.gamera.cmd || [];
window.gamera.cmd.push(function () {
enrichAuction(reqBidsConfigObj, callback, config, userConsent);
});
return;
}
enrichAuction(reqBidsConfigObj, callback, config, userConsent);
}
/**
* Enriches the auction with user and content segments from Gamera's on-page script
* @param {Object} reqBidsConfigObj - The bid request config object
* @param {Function} callback - Callback function to execute after data handling
* @param {Object} config - Module configuration
* @param {Object} userConsent - User consent data
*/
function enrichAuction(reqBidsConfigObj, callback, config, userConsent) {
try {
/**
* @function external:"window.gamera".getPrebidSegments
* @description Retrieves user and content segments from Gamera's on-page script
* @param {Function|null} onSegmentsUpdateCallback - Callback for segment updates (not used here)
* @param {Object} config - Module configuration
* @param {Object} userConsent - User consent data
* @returns {Object|undefined} segments - The targeting segments object containing:
* @property {Object} [user] - User-level attributes to merge into ortb2.user
* @property {Object} [site] - Site-level attributes to merge into ortb2.site
* @property {Object.<string, Object>} [adUnits] - Ad unit specific attributes, keyed by adUnitCode,
* to merge into each ad unit's ortb2Imp
*/
const segments = window.gamera.getPrebidSegments(null, deepClone(config || {}), deepClone(userConsent || {})) || {};
// Initialize ortb2Fragments and its nested objects
reqBidsConfigObj.ortb2Fragments = reqBidsConfigObj.ortb2Fragments || {};
reqBidsConfigObj.ortb2Fragments.global = reqBidsConfigObj.ortb2Fragments.global || {};
// Add user-level data
if (segments.user && isPlainObject(segments.user)) {
reqBidsConfigObj.ortb2Fragments.global.user = reqBidsConfigObj.ortb2Fragments.global.user || {};
mergeDeep(reqBidsConfigObj.ortb2Fragments.global.user, segments.user);
}
// Add site-level data
if (segments.site && isPlainObject(segments.site)) {
reqBidsConfigObj.ortb2Fragments.global.site = reqBidsConfigObj.ortb2Fragments.global.site || {};
mergeDeep(reqBidsConfigObj.ortb2Fragments.global.site, segments.site);
}
// Add adUnit-level data
const adUnits = reqBidsConfigObj.adUnits || getGlobal().adUnits || [];
adUnits.forEach(adUnit => {
const gameraData = segments.adUnits && segments.adUnits[adUnit.code];
if (!gameraData || !isPlainObject(gameraData)) {
return;
}
adUnit.ortb2Imp = adUnit.ortb2Imp || {};
mergeDeep(adUnit.ortb2Imp, gameraData);
});
} catch (error) {
logError(MODULE, 'Error getting segments:', error);
}
callback();
}
export const subModuleObj = {
name: MODULE_NAME,
init: init,
getBidRequestData: getBidRequestData,
};
submodule('realTimeData', subModuleObj);