forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicAdBoostRtdProvider.js
119 lines (105 loc) · 3.63 KB
/
dynamicAdBoostRtdProvider.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
/**
* The {@link module:modules/realTimeData} module is required
* @module modules/dynamicAdBoost
* @requires module:modules/realTimeData
*/
import { submodule } from '../src/hook.js'
import { loadExternalScript } from '../src/adloader.js';
import { getGlobal } from '../src/prebidGlobal.js';
import { deepAccess, deepSetValue, isEmptyStr } from '../src/utils.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
const MODULE_NAME = 'dynamicAdBoost';
const SCRIPT_URL = 'https://adxbid.info';
const CLIENT_SUPPORTS_IO = window.IntersectionObserver && window.IntersectionObserverEntry && window.IntersectionObserverEntry.prototype &&
'intersectionRatio' in window.IntersectionObserverEntry.prototype;
// Options for the Intersection Observer
const dabOptions = {
threshold: 0.5 // Trigger callback when 50% of the element is visible
};
let observer;
let dabStartDate;
let dabStartTime;
// Array of div IDs to track
let dynamicAdBoostAdUnits = {};
function init(config, userConsent) {
dabStartDate = new Date();
dabStartTime = dabStartDate.getTime();
if (!CLIENT_SUPPORTS_IO) {
return false;
}
// Create an Intersection Observer instance
observer = new IntersectionObserver(dabHandleIntersection, dabOptions);
if (config.params.keyId) {
let keyId = config.params.keyId;
if (keyId && !isEmptyStr(keyId)) {
let dabDivIdsToTrack = config.params.adUnits;
let dabInterval = setInterval(function() {
// Observe each div by its ID
dabDivIdsToTrack.forEach(divId => {
let div = document.getElementById(divId);
if (div) {
observer.observe(div);
}
});
let dabDateNow = new Date();
let dabTimeNow = dabDateNow.getTime();
let dabElapsedSeconds = Math.floor((dabTimeNow - dabStartTime) / 1000);
let elapsedThreshold = 30;
if (config.params.threshold) {
elapsedThreshold = config.params.threshold;
}
if (dabElapsedSeconds >= elapsedThreshold) {
clearInterval(dabInterval); // Stop
loadLmScript(keyId);
}
}, 1000);
return true;
}
}
return false;
}
function loadLmScript(keyId) {
let viewableAdUnits = Object.keys(dynamicAdBoostAdUnits);
let viewableAdUnitsCSV = viewableAdUnits.join(',');
const scriptUrl = `${SCRIPT_URL}/${keyId}.js?viewableAdUnits=${viewableAdUnitsCSV}`;
loadExternalScript(scriptUrl, MODULE_TYPE_RTD, MODULE_NAME);
observer.disconnect();
}
function getBidRequestData(reqBidsConfigObj, callback, config, userConsent) {
const reqAdUnits = reqBidsConfigObj.adUnits || getGlobal().adUnits;
if (Array.isArray(reqAdUnits)) {
reqAdUnits.forEach(adunit => {
let gptCode = deepAccess(adunit, 'code');
if (dynamicAdBoostAdUnits.hasOwnProperty(gptCode)) {
// AdUnits has reached target viewablity at some point
deepSetValue(adunit, `ortb2Imp.ext.data.${MODULE_NAME}.${gptCode}`, dynamicAdBoostAdUnits[gptCode]);
}
});
}
callback();
}
let markViewed = (entry, observer) => {
return () => {
observer.unobserve(entry.target);
}
}
// Callback function when an observed element becomes visible
function dabHandleIntersection(entries) {
entries.forEach(entry => {
if (entry.isIntersecting && entry.intersectionRatio > 0.5) {
dynamicAdBoostAdUnits[entry.target.id] = entry.intersectionRatio;
markViewed(entry, observer)
}
});
}
/** @type {RtdSubmodule} */
export const subModuleObj = {
name: MODULE_NAME,
init,
getBidRequestData,
markViewed
};
submodule('realTimeData', subModuleObj);