@mparticle/web-facebook-kit 2.1.1 → 2.2.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.
|
@@ -12,6 +12,7 @@ function isObject(val) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
var isobject = /*#__PURE__*/Object.freeze({
|
|
15
|
+
__proto__: null,
|
|
15
16
|
'default': isObject
|
|
16
17
|
});
|
|
17
18
|
|
|
@@ -83,11 +84,14 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
83
84
|
constructor = function () {
|
|
84
85
|
var self = this,
|
|
85
86
|
isInitialized = false,
|
|
86
|
-
reportingService = null
|
|
87
|
+
reportingService = null,
|
|
88
|
+
settings,
|
|
89
|
+
productAttributeMapping;
|
|
87
90
|
|
|
88
91
|
self.name = name;
|
|
89
92
|
|
|
90
|
-
function initForwarder(
|
|
93
|
+
function initForwarder(forwarderSettings, service, testMode, trackerId, userAttributes, userIdentities) {
|
|
94
|
+
settings = forwarderSettings;
|
|
91
95
|
reportingService = service;
|
|
92
96
|
|
|
93
97
|
SupportedCommerceTypes = [
|
|
@@ -130,6 +134,8 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
130
134
|
fbq.disablePushState = true;
|
|
131
135
|
}
|
|
132
136
|
fbq('init', settings.pixelId, visitorData);
|
|
137
|
+
|
|
138
|
+
loadMappings();
|
|
133
139
|
|
|
134
140
|
isInitialized = true;
|
|
135
141
|
|
|
@@ -141,6 +147,12 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
141
147
|
}
|
|
142
148
|
}
|
|
143
149
|
|
|
150
|
+
function loadMappings() {
|
|
151
|
+
productAttributeMapping = settings.productAttributeMapping
|
|
152
|
+
? JSON.parse(settings.productAttributeMapping.replace(/"/g, '"'))
|
|
153
|
+
: [];
|
|
154
|
+
}
|
|
155
|
+
|
|
144
156
|
function processEvent(event) {
|
|
145
157
|
var reportEvent = false;
|
|
146
158
|
|
|
@@ -229,13 +241,13 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
229
241
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
|
|
230
242
|
eventName = ADD_TO_CART_EVENT_NAME;
|
|
231
243
|
}
|
|
232
|
-
else{
|
|
244
|
+
else {
|
|
233
245
|
eventName = VIEW_CONTENT_EVENT_NAME;
|
|
234
246
|
}
|
|
235
247
|
|
|
236
248
|
}
|
|
237
249
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
|
|
238
|
-
|
|
250
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
239
251
|
|
|
240
252
|
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
|
|
241
253
|
|
|
@@ -250,6 +262,18 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
250
262
|
return sum;
|
|
251
263
|
}, 0);
|
|
252
264
|
params['num_items'] = num_items;
|
|
265
|
+
|
|
266
|
+
if (event.ProductAction.TransactionId) {
|
|
267
|
+
params['order_id'] = event.ProductAction.TransactionId;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Build contents array for Purchase events
|
|
271
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
272
|
+
var contents = buildProductContents(event.ProductAction.ProductList);
|
|
273
|
+
if (contents && contents.length > 0) {
|
|
274
|
+
params['contents'] = contents;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
253
277
|
}
|
|
254
278
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
|
|
255
279
|
eventName = REMOVE_FROM_CART_EVENT_NAME;
|
|
@@ -346,6 +370,60 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
|
346
370
|
return null;
|
|
347
371
|
}
|
|
348
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Builds contents array for Facebook Pixel commerce events.
|
|
375
|
+
* Creates a nested array of content items with product details.
|
|
376
|
+
*
|
|
377
|
+
* @param {Array} productList - Array of products from event.ProductAction.ProductList
|
|
378
|
+
* @returns {Array} Array of content objects for Facebook Pixel
|
|
379
|
+
*/
|
|
380
|
+
function buildProductContents(productList) {
|
|
381
|
+
if (!productList || productList.length === 0) {
|
|
382
|
+
return [];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return productList
|
|
386
|
+
.filter(function(product) {
|
|
387
|
+
return product && product.Sku;
|
|
388
|
+
})
|
|
389
|
+
.map(function(product) {
|
|
390
|
+
var contentItem = {
|
|
391
|
+
id: product.Sku,
|
|
392
|
+
quantity: isNumeric(product.Quantity) ? product.Quantity : 1,
|
|
393
|
+
name: product.Name,
|
|
394
|
+
brand: product.Brand,
|
|
395
|
+
category: product.Category,
|
|
396
|
+
variant: product.Variant,
|
|
397
|
+
item_price: isNumeric(product.Price) ? product.Price : null
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
// Apply configured mappings to custom attributes
|
|
401
|
+
productAttributeMapping.forEach(function(productMapping) {
|
|
402
|
+
if (!isobject$1(productMapping) || !productMapping.map || !productMapping.value) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
var sourceField = productMapping.map;
|
|
407
|
+
var facebookFieldName = productMapping.value;
|
|
408
|
+
var value = null;
|
|
409
|
+
|
|
410
|
+
// Check for Product level field first
|
|
411
|
+
if (product.hasOwnProperty(sourceField)) {
|
|
412
|
+
value = product[sourceField];
|
|
413
|
+
}
|
|
414
|
+
// then check for Product.Attributes level field
|
|
415
|
+
else if (product.Attributes && product.Attributes[sourceField]) {
|
|
416
|
+
value = product.Attributes[sourceField];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (value !== null && value !== undefined) {
|
|
420
|
+
contentItem[facebookFieldName] = value;
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return contentItem;
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
349
427
|
// https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
|
|
350
428
|
function createEventId(event) {
|
|
351
429
|
return {
|
|
@@ -9,6 +9,17 @@ function isObject(val) {
|
|
|
9
9
|
return val != null && typeof val === 'object' && Array.isArray(val) === false;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
var isobject = /*#__PURE__*/Object.freeze({
|
|
13
|
+
__proto__: null,
|
|
14
|
+
'default': isObject
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
function getCjsExportFromNamespace (n) {
|
|
18
|
+
return n && n['default'] || n;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
22
|
+
|
|
12
23
|
/* eslint-disable no-undef */
|
|
13
24
|
// Copyright 2015 mParticle, Inc.
|
|
14
25
|
//
|
|
@@ -71,11 +82,14 @@ function isObject(val) {
|
|
|
71
82
|
constructor = function () {
|
|
72
83
|
var self = this,
|
|
73
84
|
isInitialized = false,
|
|
74
|
-
reportingService = null
|
|
85
|
+
reportingService = null,
|
|
86
|
+
settings,
|
|
87
|
+
productAttributeMapping;
|
|
75
88
|
|
|
76
89
|
self.name = name;
|
|
77
90
|
|
|
78
|
-
function initForwarder(
|
|
91
|
+
function initForwarder(forwarderSettings, service, testMode, trackerId, userAttributes, userIdentities) {
|
|
92
|
+
settings = forwarderSettings;
|
|
79
93
|
reportingService = service;
|
|
80
94
|
|
|
81
95
|
SupportedCommerceTypes = [
|
|
@@ -118,6 +132,8 @@ function isObject(val) {
|
|
|
118
132
|
fbq.disablePushState = true;
|
|
119
133
|
}
|
|
120
134
|
fbq('init', settings.pixelId, visitorData);
|
|
135
|
+
|
|
136
|
+
loadMappings();
|
|
121
137
|
|
|
122
138
|
isInitialized = true;
|
|
123
139
|
|
|
@@ -129,6 +145,12 @@ function isObject(val) {
|
|
|
129
145
|
}
|
|
130
146
|
}
|
|
131
147
|
|
|
148
|
+
function loadMappings() {
|
|
149
|
+
productAttributeMapping = settings.productAttributeMapping
|
|
150
|
+
? JSON.parse(settings.productAttributeMapping.replace(/"/g, '"'))
|
|
151
|
+
: [];
|
|
152
|
+
}
|
|
153
|
+
|
|
132
154
|
function processEvent(event) {
|
|
133
155
|
var reportEvent = false;
|
|
134
156
|
|
|
@@ -217,13 +239,13 @@ function isObject(val) {
|
|
|
217
239
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
|
|
218
240
|
eventName = ADD_TO_CART_EVENT_NAME;
|
|
219
241
|
}
|
|
220
|
-
else{
|
|
242
|
+
else {
|
|
221
243
|
eventName = VIEW_CONTENT_EVENT_NAME;
|
|
222
244
|
}
|
|
223
245
|
|
|
224
246
|
}
|
|
225
247
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
|
|
226
|
-
|
|
248
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
227
249
|
|
|
228
250
|
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
|
|
229
251
|
|
|
@@ -238,6 +260,18 @@ function isObject(val) {
|
|
|
238
260
|
return sum;
|
|
239
261
|
}, 0);
|
|
240
262
|
params['num_items'] = num_items;
|
|
263
|
+
|
|
264
|
+
if (event.ProductAction.TransactionId) {
|
|
265
|
+
params['order_id'] = event.ProductAction.TransactionId;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Build contents array for Purchase events
|
|
269
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
270
|
+
var contents = buildProductContents(event.ProductAction.ProductList);
|
|
271
|
+
if (contents && contents.length > 0) {
|
|
272
|
+
params['contents'] = contents;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
241
275
|
}
|
|
242
276
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
|
|
243
277
|
eventName = REMOVE_FROM_CART_EVENT_NAME;
|
|
@@ -334,6 +368,60 @@ function isObject(val) {
|
|
|
334
368
|
return null;
|
|
335
369
|
}
|
|
336
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Builds contents array for Facebook Pixel commerce events.
|
|
373
|
+
* Creates a nested array of content items with product details.
|
|
374
|
+
*
|
|
375
|
+
* @param {Array} productList - Array of products from event.ProductAction.ProductList
|
|
376
|
+
* @returns {Array} Array of content objects for Facebook Pixel
|
|
377
|
+
*/
|
|
378
|
+
function buildProductContents(productList) {
|
|
379
|
+
if (!productList || productList.length === 0) {
|
|
380
|
+
return [];
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return productList
|
|
384
|
+
.filter(function(product) {
|
|
385
|
+
return product && product.Sku;
|
|
386
|
+
})
|
|
387
|
+
.map(function(product) {
|
|
388
|
+
var contentItem = {
|
|
389
|
+
id: product.Sku,
|
|
390
|
+
quantity: isNumeric(product.Quantity) ? product.Quantity : 1,
|
|
391
|
+
name: product.Name,
|
|
392
|
+
brand: product.Brand,
|
|
393
|
+
category: product.Category,
|
|
394
|
+
variant: product.Variant,
|
|
395
|
+
item_price: isNumeric(product.Price) ? product.Price : null
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// Apply configured mappings to custom attributes
|
|
399
|
+
productAttributeMapping.forEach(function(productMapping) {
|
|
400
|
+
if (!isobject$1(productMapping) || !productMapping.map || !productMapping.value) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
var sourceField = productMapping.map;
|
|
405
|
+
var facebookFieldName = productMapping.value;
|
|
406
|
+
var value = null;
|
|
407
|
+
|
|
408
|
+
// Check for Product level field first
|
|
409
|
+
if (product.hasOwnProperty(sourceField)) {
|
|
410
|
+
value = product[sourceField];
|
|
411
|
+
}
|
|
412
|
+
// then check for Product.Attributes level field
|
|
413
|
+
else if (product.Attributes && product.Attributes[sourceField]) {
|
|
414
|
+
value = product.Attributes[sourceField];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (value !== null && value !== undefined) {
|
|
418
|
+
contentItem[facebookFieldName] = value;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return contentItem;
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
337
425
|
// https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
|
|
338
426
|
function createEventId(event) {
|
|
339
427
|
return {
|
|
@@ -355,12 +443,12 @@ function isObject(val) {
|
|
|
355
443
|
return;
|
|
356
444
|
}
|
|
357
445
|
|
|
358
|
-
if (!
|
|
446
|
+
if (!isobject$1(config)) {
|
|
359
447
|
console.log('\'config\' must be an object. You passed in a ' + typeof config);
|
|
360
448
|
return;
|
|
361
449
|
}
|
|
362
450
|
|
|
363
|
-
if (
|
|
451
|
+
if (isobject$1(config.kits)) {
|
|
364
452
|
config.kits[name] = {
|
|
365
453
|
constructor: constructor
|
|
366
454
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mparticle/web-facebook-kit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
|
|
5
5
|
"description": "mParticle integration sdk for facebook",
|
|
6
6
|
"main": "dist/FacebookEventForwarder.common.js",
|
|
@@ -17,12 +17,24 @@
|
|
|
17
17
|
"test": "node test/boilerplate/test-karma.js"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
+
"@mparticle/web-sdk": "^2.50.0",
|
|
21
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
22
|
+
"@semantic-release/exec": "^6.0.0",
|
|
23
|
+
"@semantic-release/git": "^10.0.1",
|
|
24
|
+
"@semantic-release/commit-analyzer": "^10.0.0",
|
|
25
|
+
"@semantic-release/github": "^11.0.0",
|
|
26
|
+
"@semantic-release/npm": "^10.0.0",
|
|
27
|
+
"@semantic-release/release-notes-generator": "^14.0.0",
|
|
20
28
|
"chai": "^4.2.0",
|
|
21
29
|
"karma": "^5.1.0",
|
|
22
30
|
"karma-chai": "^0.1.0",
|
|
23
31
|
"karma-chrome-launcher": "^3.1.0",
|
|
32
|
+
"karma-edge-launcher": "^0.4.2",
|
|
24
33
|
"karma-firefox-launcher": "^1.3.0",
|
|
34
|
+
"karma-ie-launcher": "^1.0.0",
|
|
35
|
+
"karma-junit-reporter": "^1.2.0",
|
|
25
36
|
"karma-mocha": "^2.0.1",
|
|
37
|
+
"karma-safari-launcher": "^1.0.0",
|
|
26
38
|
"mocha": "^2.5.3",
|
|
27
39
|
"rollup": "^1.13.1",
|
|
28
40
|
"rollup-plugin-commonjs": "^10.0.0",
|
|
@@ -32,8 +44,7 @@
|
|
|
32
44
|
"watchify": "^3.11.1"
|
|
33
45
|
},
|
|
34
46
|
"dependencies": {
|
|
35
|
-
"isobject": "^4.0.0"
|
|
36
|
-
"@mparticle/web-sdk": "^2.11.1"
|
|
47
|
+
"isobject": "^4.0.0"
|
|
37
48
|
},
|
|
38
49
|
"license": "Apache-2.0"
|
|
39
50
|
}
|