forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiantRtdProvider.js
133 lines (120 loc) · 4.08 KB
/
confiantRtdProvider.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
/**
* This module provides comprehensive detection of security, quality, and privacy threats by Confiant Inc,
* the industry leader in real-time detecting and blocking of bad ads
*
* The {@link module:modules/realTimeData} module is required
* The module will inject a Confiant Inc. script into the page to monitor ad impressions
* @module modules/confiantRtdProvider
* @requires module:modules/realTimeData
*/
import { submodule } from '../src/hook.js';
import { logError, generateUUID } from '../src/utils.js';
import { loadExternalScript } from '../src/adloader.js';
import * as events from '../src/events.js';
import { EVENTS } from '../src/constants.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
/**
* Injects the Confiant Inc. configuration script into the page, based on proprtyId provided
* @param {string} propertyId
*/
function injectConfigScript(propertyId) {
const scriptSrc = `https://cdn.confiant-integrations.net/${propertyId}/gpt_and_prebid/config.js`;
loadExternalScript(scriptSrc, MODULE_TYPE_RTD, 'confiant', () => {
});
}
/**
* Set up page with Confiant integration
* @param {Object} config
*/
function setupPage(config) {
const propertyId = config?.params?.propertyId;
if (!propertyId) {
logError('Confiant pbjs module: no propertyId provided');
return false;
}
const confiant = window.confiant || Object.create(null);
confiant[propertyId] = confiant[propertyId] || Object.create(null);
confiant[propertyId].clientSettings = confiant[propertyId].clientSettings || Object.create(null);
confiant[propertyId].clientSettings.isMGBL = true;
confiant[propertyId].clientSettings.prebidExcludeBidders = config?.params?.prebidExcludeBidders;
confiant[propertyId].clientSettings.prebidNameSpace = config?.params?.prebidNameSpace;
if (config?.params?.shouldEmitBillableEvent) {
if (window.frames['cnftComm']) {
subscribeToConfiantCommFrame(window, propertyId);
} else {
setUpMutationObserver();
}
}
injectConfigScript(propertyId);
return true;
}
/**
* Subscribe to window's message events to report Billable events
* @param {Window} targetWindow window instance to subscribe to
*/
function subscribeToConfiantCommFrame(targetWindow, propertyId) {
targetWindow.addEventListener('message', getEventHandlerFunction(propertyId));
}
let mutationObserver;
/**
* Set up mutation observer to subscribe to Confiant's communication channel ASAP
*/
function setUpMutationObserver() {
mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if (addedNode.nodeName === 'IFRAME' && addedNode.name === 'cnftComm' && !addedNode.pbjsModuleSubscribed) {
addedNode.pbjsModuleSubscribed = true;
mutationObserver.disconnect();
mutationObserver = null;
const iframeWindow = addedNode.contentWindow;
subscribeToConfiantCommFrame(iframeWindow);
}
});
});
});
mutationObserver.observe(document.head, { childList: true, subtree: true });
}
/**
* Emit billable event when Confiant integration reports that it has monitored an impression
*/
function getEventHandlerFunction(propertyId) {
return function reportBillableEvent(e) {
if (e.data.type.indexOf('cnft:reportBillableEvent:' + propertyId) > -1) {
events.emit(EVENTS.BILLABLE_EVENT, {
auctionId: e.data.auctionId,
billingId: generateUUID(),
transactionId: e.data.transactionId,
type: 'impression',
vendor: 'confiant'
});
}
}
}
/**
* Confiant submodule registration
*/
function registerConfiantSubmodule() {
submodule('realTimeData', {
name: 'confiant',
init: (config) => {
try {
return setupPage(config);
} catch (err) {
logError(err.message);
if (mutationObserver) {
mutationObserver.disconnect();
}
return false;
}
}
});
}
registerConfiantSubmodule();
export default {
injectConfigScript,
setupPage,
subscribeToConfiantCommFrame,
setUpMutationObserver,
registerConfiantSubmodule
};