@mparticle/web-bing-ads-kit 2.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,198 +25,378 @@ function isObject(val) {
25
25
  // See the License for the specific language governing permissions and
26
26
  // limitations under the License.
27
27
 
28
-
29
-
30
- var name = 'Bing',
31
- moduleId = 107,
32
- MessageType = {
33
- SessionStart: 1,
34
- SessionEnd: 2,
35
- PageView: 3,
36
- PageEvent: 4,
37
- CrashReport: 5,
38
- OptOut: 6,
39
- Profile: 14,
40
- Commerce: 16
41
- };
42
-
43
- var constructor = function () {
44
- var self = this,
45
- isInitialized = false,
46
- forwarderSettings = null,
47
- reportingService = null;
48
-
49
- self.name = name;
50
28
 
51
- function initForwarder(settings, service, testMode) {
52
- forwarderSettings = settings;
53
- reportingService = service;
54
29
 
55
- try {
56
-
57
- if(!testMode) {
58
- (function(window, document, tag, url, queue) {
59
- var f, n, i;
60
- window[queue] = window[queue] || [],
61
- window.uetq = window.uetq || [],
62
- f = function() {
30
+ var name = 'Bing';
31
+ var moduleId = 107;
32
+ var MessageType = {
33
+ SessionStart: 1,
34
+ SessionEnd: 2,
35
+ PageView: 3,
36
+ PageEvent: 4,
37
+ CrashReport: 5,
38
+ OptOut: 6,
39
+ Profile: 14,
40
+ Commerce: 16,
41
+ };
42
+
43
+ var bingConsentValues = { Denied: 'denied', Granted: 'granted' };
44
+ var bingConsentProperties = ['ad_storage'];
45
+ var bingToMpConsentSettingsMapping = {
46
+ ad_storage: 'defaultAdStorageConsentWeb',
47
+ };
48
+
49
+ var constructor = function() {
50
+ var self = this;
51
+ var isInitialized = false;
52
+ var forwarderSettings = null;
53
+ var reportingService = null;
54
+
55
+ self.consentMappings = [];
56
+ self.consentPayloadAsString = '';
57
+ self.consentPayloadDefaults = {};
58
+
59
+ self.name = name;
60
+
61
+ function initForwarder(settings, service, testMode) {
62
+ forwarderSettings = settings;
63
+ reportingService = service;
64
+
65
+ if (forwarderSettings.consentMappingWeb) {
66
+ self.consentMappings = parseSettingsString(
67
+ forwarderSettings.consentMappingWeb
68
+ );
69
+ }
70
+ self.consentPayloadDefaults = getConsentSettings(forwarderSettings);
71
+
72
+ var initialConsentPayload = cloneObject(self.consentPayloadDefaults);
73
+ var userConsentState = getUserConsentState();
74
+
75
+ var updatedConsentPayload = generateConsentPayload(
76
+ userConsentState,
77
+ self.consentMappings
78
+ );
79
+
80
+ try {
81
+ if (!testMode) {
82
+ (function(window, document, tag, url, queue) {
83
+ var f;
84
+ var n;
85
+ var i;
86
+ (window[queue] = window[queue] || []),
87
+ (window.uetq = window.uetq || []),
88
+ sendConsentDefaultToBing(initialConsentPayload),
89
+ (f = function() {
63
90
  var obj = {
64
91
  ti: forwarderSettings.tagId,
65
- q: window.uetq
92
+ q: window.uetq,
66
93
  };
67
- obj.q = window[queue], window[queue] = new UET(obj), window[queue].push('pageLoad');
68
- },
69
- n = document.createElement(tag), n.src = url, n.async = 1, n.onload = n.onreadystatechange = function() {
94
+ (obj.q = window[queue]),
95
+ (window[queue] = new UET(obj)),
96
+ maybeSendConsentUpdateToBing(
97
+ updatedConsentPayload
98
+ );
99
+ window[queue].push('pageLoad');
100
+ }),
101
+ (n = document.createElement(tag)),
102
+ (n.src = url),
103
+ (n.async = 1),
104
+ (n.onload = n.onreadystatechange = function() {
70
105
  var state = this.readyState;
71
- state && state !== 'loaded' && state !== 'complete' || (f(), n.onload = n.onreadystatechange = null);
72
- },
73
- i = document.getElementsByTagName(tag)[0], i.parentNode.insertBefore(n, i);
74
- })(window, document, 'script', '//bat.bing.com/bat.js', 'uetq');
75
-
76
- if (window.uetq && window.queue && window.queue.length > 0) {
77
- for (var i = 0, length = window.queue.length; i < length; i++) {
78
- processEvent(window.queue[i]);
79
- }
80
-
81
- window.queue.length = 0;
106
+ (state &&
107
+ state !== 'loaded' &&
108
+ state !== 'complete') ||
109
+ (f(), (n.onload = n.onreadystatechange = null));
110
+ }),
111
+ (i = document.getElementsByTagName(tag)[0]),
112
+ i.parentNode.insertBefore(n, i);
113
+ })(window, document, 'script', '//bat.bing.com/bat.js', 'uetq');
114
+
115
+ if (window.uetq && window.queue && window.queue.length > 0) {
116
+ for (
117
+ var i = 0, length = window.queue.length;
118
+ i < length;
119
+ i++
120
+ ) {
121
+ processEvent(window.queue[i]);
82
122
  }
123
+
124
+ window.queue.length = 0;
83
125
  }
126
+ }
84
127
 
85
- isInitialized = true;
86
- return 'Successfully initialized: ' + name;
128
+ isInitialized = true;
129
+ return 'Successfully initialized: ' + name;
130
+ } catch (e) {
131
+ return "Can't initialize forwarder: " + name + ': ' + e;
132
+ }
133
+ }
134
+
135
+ function processEvent(event) {
136
+ if (!isInitialized) {
137
+ return "Can't send to forwarder: " + name + ', not initialized';
138
+ }
139
+
140
+ var reportEvent = false;
141
+ try {
142
+ if (
143
+ event.EventDataType == MessageType.PageEvent ||
144
+ event.EventDataType == MessageType.PageView
145
+ ) {
146
+ reportEvent = true;
147
+ logEvent(event);
148
+ } else if (
149
+ event.EventDataType == MessageType.Commerce &&
150
+ event.ProductAction &&
151
+ event.ProductAction.ProductActionType ==
152
+ mParticle.ProductActionType.Purchase
153
+ ) {
154
+ reportEvent = true;
155
+ logPurchaseEvent(event);
87
156
  }
88
- catch (e) {
89
- return 'Can\'t initialize forwarder: ' + name + ': ' + e;
157
+
158
+ if (reportEvent && reportingService) {
159
+ reportingService(self, event);
160
+ return 'Successfully sent to forwarder: ' + name;
90
161
  }
162
+ } catch (e) {
163
+ return "Can't send to forwarder: " + name + ' ' + e;
91
164
  }
165
+ }
92
166
 
93
- function processEvent(event) {
94
- if (!isInitialized) {
95
- return 'Can\'t send to forwarder: ' + name + ', not initialized';
96
- }
167
+ function logEvent(event) {
168
+ if (!isInitialized) {
169
+ return (
170
+ "Can't log event on forwarder: " + name + ', not initialized'
171
+ );
172
+ }
173
+ try {
174
+ var obj = createUetObject(event, 'pageLoad');
97
175
 
98
- var reportEvent = false;
99
- try {
100
- if (event.EventDataType == MessageType.PageEvent ||
101
- event.EventDataType == MessageType.PageView) {
176
+ var eventConsentState = getEventConsentState(event.ConsentState);
102
177
 
103
- reportEvent = true;
104
- logEvent(event);
105
- }
106
- else if (event.EventDataType == MessageType.Commerce &&
107
- event.ProductAction &&
108
- event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
178
+ maybeSendConsentUpdateToBing(eventConsentState);
109
179
 
110
- reportEvent = true;
111
- logPurchaseEvent(event);
112
- }
180
+ window.uetq.push(obj);
181
+ } catch (e) {
182
+ return "Can't log event on forwarder: " + name + ': ' + e;
183
+ }
113
184
 
114
- if(reportEvent && reportingService) {
115
- reportingService(self, event);
116
- return 'Successfully sent to forwarder: ' + name;
117
- }
185
+ return 'Successfully logged event from forwarder: ' + name;
186
+ }
118
187
 
119
- }
120
- catch (e) {
121
- return 'Can\'t send to forwarder: ' + name + ' ' + e;
122
- }
188
+ function logPurchaseEvent(event) {
189
+ if (!isInitialized) {
190
+ return (
191
+ "Can't log purchase event on forwarder: " +
192
+ name +
193
+ ', not initialized'
194
+ );
123
195
  }
124
196
 
125
- function logEvent(event) {
126
- if (!isInitialized) {
127
- return 'Can\'t log event on forwarder: ' + name + ', not initialized';
128
- }
197
+ if (
198
+ event.ProductAction.TotalAmount === undefined ||
199
+ event.ProductAction.TotalAmount === null
200
+ ) {
201
+ return "Can't log purchase event without a total amount on product action";
202
+ }
129
203
 
130
- try {
131
- var obj = createUetObject(event, 'pageLoad');
204
+ try {
205
+ var obj = createUetObject(event, 'eCommerce');
206
+ obj.gv = event.ProductAction.TotalAmount;
132
207
 
133
- window.uetq.push(obj);
134
- }
135
- catch (e) {
136
- return 'Can\'t log event on forwarder: ' + name + ': ' + e;
137
- }
208
+ window.uetq.push(obj);
209
+ } catch (e) {
210
+ return "Can't log commerce event on forwarder: " + name + ': ' + e;
211
+ }
212
+ }
138
213
 
139
- return 'Successfully logged event from forwarder: ' + name;
214
+ function createUetObject(event, action) {
215
+ var obj = {
216
+ ea: action,
217
+ ec: window.mParticle.EventType.getName(event.EventCategory),
218
+ el: event.EventName,
219
+ };
220
+
221
+ if (event.CustomFlags && event.CustomFlags['Bing.EventValue']) {
222
+ obj.ev = event.CustomFlags['Bing.EventValue'];
140
223
  }
141
224
 
142
- function logPurchaseEvent(event) {
143
- if (!isInitialized) {
144
- return 'Can\'t log purchase event on forwarder: ' + name + ', not initialized';
145
- }
225
+ return obj;
226
+ }
146
227
 
147
- if (event.ProductAction.TotalAmount === undefined ||
148
- event.ProductAction.TotalAmount === null) {
149
- return 'Can\'t log purchase event without a total amount on product action';
150
- }
228
+ function getEventConsentState(eventConsentState) {
229
+ return eventConsentState && eventConsentState.getGDPRConsentState
230
+ ? eventConsentState.getGDPRConsentState()
231
+ : {};
232
+ }
151
233
 
152
- try {
153
- var obj = createUetObject(event, 'eCommerce');
154
- obj.gv = event.ProductAction.TotalAmount;
234
+ function generateConsentPayload(consentState, mappings) {
235
+ if (!mappings) {
236
+ return {};
237
+ }
155
238
 
156
- window.uetq.push(obj);
157
- }
158
- catch (e) {
159
- return 'Can\'t log commerce event on forwarder: ' + name + ': ' + e;
239
+ var payload = cloneObject(self.consentPayloadDefaults);
240
+ if (mappings && mappings.length > 0) {
241
+ for (var i = 0; i < mappings.length; i++) {
242
+ var mappingEntry = mappings[i];
243
+ var mpMappedConsentName = mappingEntry.map.toLowerCase();
244
+ var bingMappedConsentName = mappingEntry.value;
245
+
246
+ if (
247
+ consentState[mpMappedConsentName] &&
248
+ bingConsentProperties.indexOf(bingMappedConsentName) !== -1
249
+ ) {
250
+ payload[bingMappedConsentName] = consentState[
251
+ mpMappedConsentName
252
+ ].Consented
253
+ ? bingConsentValues.Granted
254
+ : bingConsentValues.Denied;
255
+ }
160
256
  }
161
257
  }
162
258
 
163
- function createUetObject (event, action) {
164
- var obj = {
165
- ea: action,
166
- ec: window.mParticle.EventType.getName(event.EventCategory),
167
- el: event.EventName
168
- };
259
+ return payload;
260
+ }
169
261
 
170
- if (event.CustomFlags && event.CustomFlags['Bing.EventValue']) {
171
- obj.ev = event.CustomFlags['Bing.EventValue'];
262
+ function maybeSendConsentUpdateToBing(consentState) {
263
+ if (
264
+ self.consentPayloadAsString &&
265
+ self.consentMappings &&
266
+ !isEmpty(consentState)
267
+ ) {
268
+ var updatedConsentPayload = generateConsentPayload(
269
+ consentState,
270
+ self.consentMappings
271
+ );
272
+
273
+ var eventConsentAsString = JSON.stringify(updatedConsentPayload);
274
+
275
+ if (eventConsentAsString !== self.consentPayloadAsString) {
276
+ window.uetq.push('consent', 'update', updatedConsentPayload);
277
+ self.consentPayloadAsString = JSON.stringify(
278
+ updatedConsentPayload
279
+ );
172
280
  }
173
-
174
- return obj;
175
281
  }
282
+ }
176
283
 
177
- this.init = initForwarder;
178
- this.process = processEvent;
179
- };
284
+ function sendConsentDefaultToBing(consentPayload) {
285
+ self.consentPayloadAsString = JSON.stringify(consentPayload);
180
286
 
181
- function getId() {
182
- return moduleId;
287
+ window.uetq.push('consent', 'default', consentPayload);
183
288
  }
184
289
 
185
- function register(config) {
186
- if (!config) {
187
- window.console.log('You must pass a config object to register the kit ' + name);
188
- return;
290
+ this.init = initForwarder;
291
+ this.process = processEvent;
292
+ };
293
+
294
+ function getUserConsentState() {
295
+ var userConsentState = {};
296
+
297
+ if (mParticle.Identity && mParticle.Identity.getCurrentUser) {
298
+ var currentUser = mParticle.Identity.getCurrentUser();
299
+
300
+ if (!currentUser) {
301
+ return {};
189
302
  }
190
303
 
191
- if (!isObject(config)) {
192
- window.console.log('\'config\' must be an object. You passed in a ' + typeof config);
193
- return;
304
+ var consentState = mParticle.Identity.getCurrentUser().getConsentState();
305
+
306
+ if (consentState && consentState.getGDPRConsentState) {
307
+ userConsentState = consentState.getGDPRConsentState();
194
308
  }
309
+ }
310
+
311
+ return userConsentState;
312
+ }
195
313
 
196
- if (isObject(config.kits)) {
197
- config.kits[name] = {
198
- constructor: constructor
199
- };
314
+ function getConsentSettings(settings) {
315
+ var consentSettings = {};
316
+
317
+ Object.keys(bingToMpConsentSettingsMapping).forEach(function(
318
+ bingConsentKey
319
+ ) {
320
+ var mpConsentSettingKey =
321
+ bingToMpConsentSettingsMapping[bingConsentKey];
322
+ var bingConsentValuesKey = settings[mpConsentSettingKey];
323
+
324
+ // Microsoft recommends that for most countries, we should default to 'Granted'
325
+ // if a default value is not provided
326
+ // https://help.ads.microsoft.com/apex/index/3/en/60119
327
+ if (bingConsentValuesKey && mpConsentSettingKey) {
328
+ consentSettings[bingConsentKey] = bingConsentValues[
329
+ bingConsentValuesKey
330
+ ]
331
+ ? bingConsentValues[bingConsentValuesKey]
332
+ : bingConsentValues.Granted;
200
333
  } else {
201
- config.kits = {};
202
- config.kits[name] = {
203
- constructor: constructor
204
- };
334
+ consentSettings[bingConsentKey] = bingConsentValues.Granted;
205
335
  }
206
- window.console.log('Successfully registered ' + name + ' to your mParticle configuration');
336
+ });
337
+
338
+ return consentSettings;
339
+ }
340
+
341
+ function parseSettingsString(settingsString) {
342
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
343
+ }
344
+
345
+ function getId() {
346
+ return moduleId;
347
+ }
348
+
349
+ function register(config) {
350
+ if (!config) {
351
+ console.log(
352
+ 'You must pass a config object to register the kit ' + name
353
+ );
354
+ return;
355
+ }
356
+
357
+ if (!isObject(config)) {
358
+ console.log(
359
+ "'config' must be an object. You passed in a " + typeof config
360
+ );
361
+ return;
207
362
  }
208
363
 
364
+ if (isObject(config.kits)) {
365
+ config.kits[name] = {
366
+ constructor: constructor,
367
+ };
368
+ } else {
369
+ config.kits = {};
370
+ config.kits[name] = {
371
+ constructor: constructor,
372
+ };
373
+ }
374
+ console.log(
375
+ 'Successfully registered ' + name + ' to your mParticle configuration'
376
+ );
377
+ }
378
+
379
+ if (typeof window !== 'undefined') {
209
380
  if (window && window.mParticle && window.mParticle.addForwarder) {
210
381
  window.mParticle.addForwarder({
211
382
  name: name,
212
383
  constructor: constructor,
213
- getId: getId
384
+ getId: getId,
214
385
  });
215
386
  }
387
+ }
388
+
389
+ function isEmpty(value) {
390
+ return value == null || !(Object.keys(value) || value).length;
391
+ }
392
+
393
+ function cloneObject(obj) {
394
+ return JSON.parse(JSON.stringify(obj));
395
+ }
216
396
 
217
- var BingAdsEventForwarder = {
218
- register: register
219
- };
397
+ var BingAdsEventForwarder = {
398
+ register: register,
399
+ };
220
400
  var BingAdsEventForwarder_1 = BingAdsEventForwarder.register;
221
401
 
222
402
  exports.default = BingAdsEventForwarder;
package/package.json CHANGED
@@ -1,28 +1,56 @@
1
1
  {
2
- "name": "@mparticle/web-bing-ads-kit",
3
- "version": "2.0.1",
4
- "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
- "description": "mParticle integration sdk for Bing Ads",
6
- "main": "dist/BingAdsEventForwarder.common.js",
7
- "browser": "dist/BingAdsEventForwarder.common.js",
8
- "files": [
9
- "dist/BingAdsEventForwarder.common.js"
10
- ],
11
- "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-bingads",
12
- "scripts": {
13
- "build": "rollup --config rollup.config.js",
14
- "watch": "rollup --config rollup.config.js -w"
15
- },
16
- "devDependencies": {
17
- "should": "7.1.0",
18
- "mocha": "^2.3.2",
19
- "rollup": "^1.13.1",
20
- "rollup-plugin-commonjs": "^10.0.0",
21
- "rollup-plugin-node-resolve": "^5.0.1"
22
- },
23
- "dependencies": {
24
- "isobject": "^4.0.0",
25
- "@mparticle/web-sdk": "^2.11.1"
26
- },
27
- "license": "Apache-2.0"
2
+ "name": "@mparticle/web-bing-ads-kit",
3
+ "version": "3.1.0",
4
+ "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
+ "description": "mParticle integration sdk for Bing Ads",
6
+ "main": "dist/BingAdsEventForwarder.common.js",
7
+ "browser": "dist/BingAdsEventForwarder.common.js",
8
+ "files": [
9
+ "dist/BingAdsEventForwarder.common.js"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/mParticle/mparticle-web-sdk",
14
+ "directory": "kits/bingads"
15
+ },
16
+ "scripts": {
17
+ "build": "ENVIRONMENT=production rollup --config rollup.config.js",
18
+ "build:test": "ENVIRONMENT=production rollup --config rollup.test.config.js",
19
+ "lint": "eslint src/ test/src/",
20
+ "lint:fix": "eslint src/ test/src/ --fix",
21
+ "test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js",
22
+ "test:debug": "npm run build && npm run build:test && DEBUG=true karma start test/karma.config.js",
23
+ "watch": "ENVIRONMENT=production rollup --config rollup.config.js -w",
24
+ "watch:tests": "ENVIRONMENT=production rollup --config rollup.test.config.js -w"
25
+ },
26
+ "devDependencies": {
27
+ "@semantic-release/changelog": "^5.0.1",
28
+ "@semantic-release/exec": "^5.0.0",
29
+ "@semantic-release/git": "^9.0.0",
30
+ "chai": "^4.2.0",
31
+ "eslint": "^8.36.0",
32
+ "eslint-config-prettier": "9.0.0",
33
+ "eslint-plugin-prettier": "3.4.1",
34
+ "karma": "^5.1.0",
35
+ "karma-chai": "^0.1.0",
36
+ "karma-chrome-launcher": "^3.1.0",
37
+ "karma-firefox-launcher": "^1.3.0",
38
+ "karma-mocha": "^2.0.1",
39
+ "mocha": "^2.5.3",
40
+ "prettier": "1.18.2",
41
+ "rollup": "^1.13.1",
42
+ "rollup-plugin-commonjs": "^10.0.0",
43
+ "rollup-plugin-node-resolve": "^5.0.1",
44
+ "shelljs": "^0.8.4",
45
+ "should": "^7.1.0",
46
+ "watchify": "^3.11.1"
47
+ },
48
+ "dependencies": {
49
+ "isobject": "^4.0.0",
50
+ "@mparticle/web-sdk": "^3.0.0"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "license": "Apache-2.0"
28
56
  }