forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
brandmetricsRtdProvider.js
208 lines (186 loc) · 5.9 KB
/
brandmetricsRtdProvider.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* This module adds brandmetrics provider to the real time data module
* The {@link module:modules/realTimeData} module is required
* The module will load load the brandmetrics script and set survey- targeting to ad units of specific bidders.
* @module modules/brandmetricsRtdProvider
* @requires module:modules/realTimeData
*/
import { submodule } from '../src/hook.js';
import { deepAccess, deepSetValue, logError, mergeDeep, generateUUID } from '../src/utils.js';
import { loadExternalScript } from '../src/adloader.js';
import * as events from '../src/events.js';
import { EVENTS } from '../src/constants.js';
/**
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/
const MODULE_NAME = 'brandmetrics'
const MODULE_CODE = MODULE_NAME
const RECEIVED_EVENTS = []
const GVL_ID = 422
const TCF_PURPOSES = [1, 7]
let billableEventsInitialized = false
function init (config, userConsent) {
const hasConsent = checkConsent(userConsent)
const initialize = hasConsent !== false
if (initialize) {
const moduleConfig = getMergedConfig(config)
initializeBrandmetrics(moduleConfig.params.scriptId)
initializeBillableEvents()
}
return initialize
}
/**
* Checks TCF and USP consents
* @param {Object} userConsent
* @returns {boolean}
*/
function checkConsent (userConsent) {
let consent
if (userConsent) {
if (userConsent.gdpr && userConsent.gdpr.gdprApplies) {
const gdpr = userConsent.gdpr
if (gdpr.vendorData) {
const vendor = gdpr.vendorData.vendor
const purpose = gdpr.vendorData.purpose
let vendorConsent = false
if (vendor.consents) {
vendorConsent = vendor.consents[GVL_ID]
}
if (vendor.legitimateInterests) {
vendorConsent = vendorConsent || vendor.legitimateInterests[GVL_ID]
}
const purposes = TCF_PURPOSES.map(id => {
return (purpose.consents && purpose.consents[id]) || (purpose.legitimateInterests && purpose.legitimateInterests[id])
})
const purposesValid = purposes.filter(p => p === true).length === TCF_PURPOSES.length
consent = vendorConsent && purposesValid
}
} else if (userConsent.usp) {
const usp = userConsent.usp
consent = usp[1] !== 'N' && usp[2] !== 'Y'
}
}
return consent
}
/**
* Add event- listeners to hook in to brandmetrics events
* @param {Object} reqBidsConfigObj
* @param {Object} moduleConfig
* @param {function} callback
*/
function processBrandmetricsEvents (reqBidsConfigObj, moduleConfig, callback) {
const callBidTargeting = (event) => {
if (event.available && event.conf) {
const targetingConf = event.conf.displayOption || {}
if (targetingConf.type === 'pbjs') {
setBidderTargeting(reqBidsConfigObj, moduleConfig, targetingConf.targetKey || 'brandmetrics_survey', event.survey.measurementId)
}
}
callback()
}
if (RECEIVED_EVENTS.length > 0) {
callBidTargeting(RECEIVED_EVENTS[RECEIVED_EVENTS.length - 1])
} else {
window._brandmetrics.push({
cmd: '_addeventlistener',
val: {
event: 'surveyloaded',
reEmitLast: true,
handler: (ev) => {
RECEIVED_EVENTS.push(ev)
if (RECEIVED_EVENTS.length === 1) {
// Call bid targeting only for the first received event, if called subsequently, last event from the RECEIVED_EVENTS array is used
callBidTargeting(ev)
}
},
}
})
}
}
/**
* Sets bid targeting of specific bidders
* @param {Object} reqBidsConfigObj
* @param {Object} moduleConfig
* @param {string} key Targeting key
* @param {string} val Targeting value
*/
function setBidderTargeting (reqBidsConfigObj, moduleConfig, key, val) {
const bidders = deepAccess(moduleConfig, 'params.bidders')
if (bidders && bidders.length > 0) {
bidders.forEach(bidder => {
deepSetValue(reqBidsConfigObj, `ortb2Fragments.bidder.${bidder}.user.ext.data.${key}`, val);
})
}
}
/**
* Add the brandmetrics script to the page.
* @param {string} scriptId - The script- id provided by brandmetrics or brandmetrics partner
*/
function initializeBrandmetrics(scriptId) {
window._brandmetrics = window._brandmetrics || []
if (scriptId) {
const path = 'https://cdn.brandmetrics.com/survey/script/'
const file = scriptId + '.js'
const url = path + file
loadExternalScript(url, MODULE_CODE)
}
}
/**
* Hook in to brandmetrics creative_in_view- event and emit billable- event for creatives measured by brandmetrics.
*/
function initializeBillableEvents() {
if (!billableEventsInitialized) {
window._brandmetrics.push({
cmd: '_addeventlistener',
val: {
event: 'creative_in_view',
handler: (ev) => {
if (ev.source && ev.source.type === 'pbj') {
const bid = ev.source.data;
events.emit(EVENTS.BILLABLE_EVENT, {
vendor: 'brandmetrics',
type: 'creative_in_view',
measurementId: ev.mid,
billingId: generateUUID(),
auctionId: bid.auctionId,
transactionId: bid.transactionId,
});
}
},
}
})
billableEventsInitialized = true
}
}
/**
* Merges a provided config with default values
* @param {Object} customConfig
* @returns
*/
function getMergedConfig(customConfig) {
return mergeDeep({
waitForIt: false,
params: {
bidders: [],
scriptId: undefined,
}
}, customConfig)
}
/** @type {RtdSubmodule} */
export const brandmetricsSubmodule = {
name: MODULE_NAME,
getBidRequestData: function (reqBidsConfigObj, callback, customConfig) {
try {
const moduleConfig = getMergedConfig(customConfig)
if (moduleConfig.waitForIt) {
processBrandmetricsEvents(reqBidsConfigObj, moduleConfig, callback)
} else {
callback()
}
} catch (e) {
logError(e)
}
},
init
}
submodule('realTimeData', brandmetricsSubmodule)