@mparticle/web-facebook-kit 2.1.0 → 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.
- package/dist/FacebookEventForwarder.common.js +101 -13
- package/dist/FacebookEventForwarder.esm.js +480 -0
- package/package.json +17 -4
- package/CHANGELOG.md +0 -7
|
@@ -11,6 +11,17 @@ function isObject(val) {
|
|
|
11
11
|
return val != null && typeof val === 'object' && Array.isArray(val) === false;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
var isobject = /*#__PURE__*/Object.freeze({
|
|
15
|
+
__proto__: null,
|
|
16
|
+
'default': isObject
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function getCjsExportFromNamespace (n) {
|
|
20
|
+
return n && n['default'] || n;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var isobject$1 = getCjsExportFromNamespace(isobject);
|
|
24
|
+
|
|
14
25
|
/* eslint-disable no-undef */
|
|
15
26
|
// Copyright 2015 mParticle, Inc.
|
|
16
27
|
//
|
|
@@ -63,21 +74,24 @@ function isObject(val) {
|
|
|
63
74
|
},
|
|
64
75
|
SupportedCommerceTypes = [],
|
|
65
76
|
// Standard FB Event Names from https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
|
|
66
|
-
ADD_TO_CART_EVENT_NAME = 'AddToCart'
|
|
67
|
-
ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist'
|
|
68
|
-
CHECKOUT_EVENT_NAME = 'InitiateCheckout'
|
|
69
|
-
PAGE_VIEW_EVENT_NAME = 'PageView'
|
|
70
|
-
PURCHASE_EVENT_NAME = 'Purchase'
|
|
71
|
-
REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart'
|
|
72
|
-
VIEW_CONTENT_EVENT_NAME = 'ViewContent'
|
|
77
|
+
ADD_TO_CART_EVENT_NAME = 'AddToCart',
|
|
78
|
+
ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist',
|
|
79
|
+
CHECKOUT_EVENT_NAME = 'InitiateCheckout',
|
|
80
|
+
PAGE_VIEW_EVENT_NAME = 'PageView',
|
|
81
|
+
PURCHASE_EVENT_NAME = 'Purchase',
|
|
82
|
+
REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart',
|
|
83
|
+
VIEW_CONTENT_EVENT_NAME = 'ViewContent',
|
|
73
84
|
constructor = function () {
|
|
74
85
|
var self = this,
|
|
75
86
|
isInitialized = false,
|
|
76
|
-
reportingService = null
|
|
87
|
+
reportingService = null,
|
|
88
|
+
settings,
|
|
89
|
+
productAttributeMapping;
|
|
77
90
|
|
|
78
91
|
self.name = name;
|
|
79
92
|
|
|
80
|
-
function initForwarder(
|
|
93
|
+
function initForwarder(forwarderSettings, service, testMode, trackerId, userAttributes, userIdentities) {
|
|
94
|
+
settings = forwarderSettings;
|
|
81
95
|
reportingService = service;
|
|
82
96
|
|
|
83
97
|
SupportedCommerceTypes = [
|
|
@@ -120,6 +134,8 @@ function isObject(val) {
|
|
|
120
134
|
fbq.disablePushState = true;
|
|
121
135
|
}
|
|
122
136
|
fbq('init', settings.pixelId, visitorData);
|
|
137
|
+
|
|
138
|
+
loadMappings();
|
|
123
139
|
|
|
124
140
|
isInitialized = true;
|
|
125
141
|
|
|
@@ -131,6 +147,12 @@ function isObject(val) {
|
|
|
131
147
|
}
|
|
132
148
|
}
|
|
133
149
|
|
|
150
|
+
function loadMappings() {
|
|
151
|
+
productAttributeMapping = settings.productAttributeMapping
|
|
152
|
+
? JSON.parse(settings.productAttributeMapping.replace(/"/g, '"'))
|
|
153
|
+
: [];
|
|
154
|
+
}
|
|
155
|
+
|
|
134
156
|
function processEvent(event) {
|
|
135
157
|
var reportEvent = false;
|
|
136
158
|
|
|
@@ -219,13 +241,13 @@ function isObject(val) {
|
|
|
219
241
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
|
|
220
242
|
eventName = ADD_TO_CART_EVENT_NAME;
|
|
221
243
|
}
|
|
222
|
-
else{
|
|
244
|
+
else {
|
|
223
245
|
eventName = VIEW_CONTENT_EVENT_NAME;
|
|
224
246
|
}
|
|
225
247
|
|
|
226
248
|
}
|
|
227
249
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
|
|
228
|
-
|
|
250
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
229
251
|
|
|
230
252
|
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
|
|
231
253
|
|
|
@@ -240,6 +262,18 @@ function isObject(val) {
|
|
|
240
262
|
return sum;
|
|
241
263
|
}, 0);
|
|
242
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
|
+
}
|
|
243
277
|
}
|
|
244
278
|
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
|
|
245
279
|
eventName = REMOVE_FROM_CART_EVENT_NAME;
|
|
@@ -336,6 +370,60 @@ function isObject(val) {
|
|
|
336
370
|
return null;
|
|
337
371
|
}
|
|
338
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
|
+
|
|
339
427
|
// https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
|
|
340
428
|
function createEventId(event) {
|
|
341
429
|
return {
|
|
@@ -357,12 +445,12 @@ function isObject(val) {
|
|
|
357
445
|
return;
|
|
358
446
|
}
|
|
359
447
|
|
|
360
|
-
if (!
|
|
448
|
+
if (!isobject$1(config)) {
|
|
361
449
|
console.log('\'config\' must be an object. You passed in a ' + typeof config);
|
|
362
450
|
return;
|
|
363
451
|
}
|
|
364
452
|
|
|
365
|
-
if (
|
|
453
|
+
if (isobject$1(config.kits)) {
|
|
366
454
|
config.kits[name] = {
|
|
367
455
|
constructor: constructor
|
|
368
456
|
};
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* isobject <https://github.com/jonschlinkert/isobject>
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2014-2017, Jon Schlinkert.
|
|
5
|
+
* Released under the MIT License.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function isObject(val) {
|
|
9
|
+
return val != null && typeof val === 'object' && Array.isArray(val) === false;
|
|
10
|
+
}
|
|
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
|
+
|
|
23
|
+
/* eslint-disable no-undef */
|
|
24
|
+
// Copyright 2015 mParticle, Inc.
|
|
25
|
+
//
|
|
26
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
27
|
+
// you may not use this file except in compliance with the License.
|
|
28
|
+
// You may obtain a copy of the License at
|
|
29
|
+
//
|
|
30
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
31
|
+
//
|
|
32
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
33
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
34
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
35
|
+
// See the License for the specific language governing permissions and
|
|
36
|
+
// limitations under the License.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
var name = 'Facebook',
|
|
40
|
+
moduleId = 45,
|
|
41
|
+
MessageType = {
|
|
42
|
+
SessionStart: 1,
|
|
43
|
+
SessionEnd: 2,
|
|
44
|
+
PageView: 3,
|
|
45
|
+
PageEvent: 4,
|
|
46
|
+
CrashReport: 5,
|
|
47
|
+
OptOut: 6,
|
|
48
|
+
Commerce: 16,
|
|
49
|
+
},
|
|
50
|
+
IdentityType = {
|
|
51
|
+
Other: 0,
|
|
52
|
+
CustomerId: 1,
|
|
53
|
+
Facebook: 2,
|
|
54
|
+
Twitter: 3,
|
|
55
|
+
Google: 4,
|
|
56
|
+
Microsoft: 5,
|
|
57
|
+
Yahoo: 6,
|
|
58
|
+
Email: 7,
|
|
59
|
+
FacebookCustomAudienceId: 9,
|
|
60
|
+
Other2: 10,
|
|
61
|
+
Other3: 11,
|
|
62
|
+
Other4: 12,
|
|
63
|
+
Other5: 13,
|
|
64
|
+
Other6: 14,
|
|
65
|
+
Other7: 15,
|
|
66
|
+
Other8: 16,
|
|
67
|
+
Other9: 17,
|
|
68
|
+
Other10: 18,
|
|
69
|
+
MobileNumber: 19,
|
|
70
|
+
PhoneNumber2: 20,
|
|
71
|
+
PhoneNumber3: 21,
|
|
72
|
+
},
|
|
73
|
+
SupportedCommerceTypes = [],
|
|
74
|
+
// Standard FB Event Names from https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
|
|
75
|
+
ADD_TO_CART_EVENT_NAME = 'AddToCart',
|
|
76
|
+
ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist',
|
|
77
|
+
CHECKOUT_EVENT_NAME = 'InitiateCheckout',
|
|
78
|
+
PAGE_VIEW_EVENT_NAME = 'PageView',
|
|
79
|
+
PURCHASE_EVENT_NAME = 'Purchase',
|
|
80
|
+
REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart',
|
|
81
|
+
VIEW_CONTENT_EVENT_NAME = 'ViewContent',
|
|
82
|
+
constructor = function () {
|
|
83
|
+
var self = this,
|
|
84
|
+
isInitialized = false,
|
|
85
|
+
reportingService = null,
|
|
86
|
+
settings,
|
|
87
|
+
productAttributeMapping;
|
|
88
|
+
|
|
89
|
+
self.name = name;
|
|
90
|
+
|
|
91
|
+
function initForwarder(forwarderSettings, service, testMode, trackerId, userAttributes, userIdentities) {
|
|
92
|
+
settings = forwarderSettings;
|
|
93
|
+
reportingService = service;
|
|
94
|
+
|
|
95
|
+
SupportedCommerceTypes = [
|
|
96
|
+
mParticle.ProductActionType.Checkout,
|
|
97
|
+
mParticle.ProductActionType.Purchase,
|
|
98
|
+
mParticle.ProductActionType.AddToCart,
|
|
99
|
+
mParticle.ProductActionType.RemoveFromCart,
|
|
100
|
+
mParticle.ProductActionType.AddToWishlist,
|
|
101
|
+
mParticle.ProductActionType.ViewDetail
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
if (!testMode) {
|
|
106
|
+
!function (f, b, e, v, n, t, s) {
|
|
107
|
+
if (f.fbq) return; n = f.fbq = function () { n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments); }; if (!f._fbq) f._fbq = n;
|
|
108
|
+
n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0; t.src = v; s = b.getElementsByTagName(e)[0];
|
|
109
|
+
s.parentNode.insertBefore(t, s);
|
|
110
|
+
} (window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
|
|
111
|
+
|
|
112
|
+
var visitorData = {};
|
|
113
|
+
|
|
114
|
+
if(settings.externalUserIdentityType && userIdentities && userIdentities.length > 0) {
|
|
115
|
+
var selectedType = IdentityType[settings.externalUserIdentityType];
|
|
116
|
+
var selectedIdentity = userIdentities.filter(function (identityElement) {
|
|
117
|
+
if (identityElement.Type === selectedType) {
|
|
118
|
+
return identityElement.Identity;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (selectedIdentity.length > 0) {
|
|
123
|
+
visitorData['external_id'] = selectedIdentity[0].Identity;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (settings.disablePushState === 'True') {
|
|
129
|
+
// Facebook will automatically track page views whenever a new state is pushed to the HTML 5 History State API
|
|
130
|
+
// this option can be disabled to prevent duplicate page views
|
|
131
|
+
// https://developers.facebook.com/docs/facebook-pixel/implementation/tag_spa/#tagging-single-page-applications
|
|
132
|
+
fbq.disablePushState = true;
|
|
133
|
+
}
|
|
134
|
+
fbq('init', settings.pixelId, visitorData);
|
|
135
|
+
|
|
136
|
+
loadMappings();
|
|
137
|
+
|
|
138
|
+
isInitialized = true;
|
|
139
|
+
|
|
140
|
+
return 'Successfully initialized: ' + name;
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
return 'Can\'t initialize forwarder: ' + name + ':' + e;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function loadMappings() {
|
|
149
|
+
productAttributeMapping = settings.productAttributeMapping
|
|
150
|
+
? JSON.parse(settings.productAttributeMapping.replace(/"/g, '"'))
|
|
151
|
+
: [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function processEvent(event) {
|
|
155
|
+
var reportEvent = false;
|
|
156
|
+
|
|
157
|
+
if (!isInitialized) {
|
|
158
|
+
return 'Can\'t send forwarder ' + name + ', not initialized';
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
if (event.EventDataType == MessageType.PageView) {
|
|
163
|
+
reportEvent = true;
|
|
164
|
+
logPageView(event);
|
|
165
|
+
}
|
|
166
|
+
else if (event.EventDataType == MessageType.PageEvent) {
|
|
167
|
+
reportEvent = true;
|
|
168
|
+
logPageEvent(event);
|
|
169
|
+
}
|
|
170
|
+
else if (event.EventDataType == MessageType.Commerce) {
|
|
171
|
+
reportEvent = logCommerceEvent(event);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (reportEvent && reportingService) {
|
|
175
|
+
reportingService(self, event);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return 'Successfully sent to forwarder ' + name;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
return 'Can\'t send to forwarder: ' + name + ' ' + error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function logCommerceEvent(event) {
|
|
186
|
+
if (event.ProductAction &&
|
|
187
|
+
event.ProductAction.ProductList &&
|
|
188
|
+
event.ProductAction.ProductActionType &&
|
|
189
|
+
SupportedCommerceTypes.indexOf(event.ProductAction.ProductActionType) > -1) {
|
|
190
|
+
|
|
191
|
+
var eventName,
|
|
192
|
+
totalValue,
|
|
193
|
+
params = cloneEventAttributes(event),
|
|
194
|
+
eventID = createEventId(event);
|
|
195
|
+
params['currency'] = event.CurrencyCode || 'USD';
|
|
196
|
+
|
|
197
|
+
if (event.EventName) {
|
|
198
|
+
params['content_name'] = event.EventName;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
var productSkus = event.ProductAction.ProductList.reduce(function (arr, curr) {
|
|
202
|
+
if (curr.Sku) {
|
|
203
|
+
arr.push(curr.Sku);
|
|
204
|
+
}
|
|
205
|
+
return arr;
|
|
206
|
+
}, []);
|
|
207
|
+
|
|
208
|
+
if (productSkus && productSkus.length > 0) {
|
|
209
|
+
params['content_ids'] = productSkus;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
|
|
213
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) {
|
|
214
|
+
var eventCategory = getEventCategoryString(event);
|
|
215
|
+
if (eventCategory) {
|
|
216
|
+
params['content_category'] = eventCategory;
|
|
217
|
+
}
|
|
218
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout && event.ProductAction.CheckoutStep) {
|
|
219
|
+
params['checkout_step'] = event.ProductAction.CheckoutStep;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart ||
|
|
224
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
|
|
225
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.ViewDetail) {
|
|
226
|
+
|
|
227
|
+
totalValue = event.ProductAction.ProductList.reduce(function(sum, product){
|
|
228
|
+
if (isNumeric(product.Price) && isNumeric(product.Quantity)) {
|
|
229
|
+
sum += product.Price * product.Quantity;
|
|
230
|
+
}
|
|
231
|
+
return sum;
|
|
232
|
+
}, 0);
|
|
233
|
+
|
|
234
|
+
params['value'] = totalValue;
|
|
235
|
+
|
|
236
|
+
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist){
|
|
237
|
+
eventName = ADD_TO_WISHLIST_EVENT_NAME;
|
|
238
|
+
}
|
|
239
|
+
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
|
|
240
|
+
eventName = ADD_TO_CART_EVENT_NAME;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
eventName = VIEW_CONTENT_EVENT_NAME;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
}
|
|
247
|
+
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
|
|
248
|
+
event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
|
|
249
|
+
|
|
250
|
+
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
|
|
251
|
+
|
|
252
|
+
if (event.ProductAction.TotalAmount) {
|
|
253
|
+
params['value'] = event.ProductAction.TotalAmount;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
var num_items = event.ProductAction.ProductList.reduce(function(sum, product){
|
|
257
|
+
if (isNumeric(product.Quantity)) {
|
|
258
|
+
sum += product.Quantity;
|
|
259
|
+
}
|
|
260
|
+
return sum;
|
|
261
|
+
}, 0);
|
|
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
|
+
}
|
|
275
|
+
}
|
|
276
|
+
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
|
|
277
|
+
eventName = REMOVE_FROM_CART_EVENT_NAME;
|
|
278
|
+
|
|
279
|
+
// remove from cart can be performed in 1 of 2 ways:
|
|
280
|
+
// 1. mParticle.eCommerce.logProductEvent(), which contains event.ProductAction.TotalAmount
|
|
281
|
+
// 2. mParticle.eCommerce.Cart.remove(), which does not contain event.ProductAction.TotalAmount
|
|
282
|
+
// when there is no TotalAmount, a manual calculation must be done
|
|
283
|
+
if (event.ProductAction.TotalAmount) {
|
|
284
|
+
totalValue = event.ProductAction.TotalAmount;
|
|
285
|
+
} else {
|
|
286
|
+
totalValue = event.ProductAction.ProductList.reduce(function(sum, product) {
|
|
287
|
+
if (isNumeric(product.TotalAmount)) {
|
|
288
|
+
sum += product.TotalAmount;
|
|
289
|
+
}
|
|
290
|
+
return sum;
|
|
291
|
+
}, 0);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
params['value'] = totalValue;
|
|
295
|
+
|
|
296
|
+
fbq('trackCustom', eventName || 'customEvent', params, eventID);
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (eventName) {
|
|
301
|
+
fbq('track', eventName, params, eventID);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function logPageView(event) {
|
|
314
|
+
logPageEvent(event, PAGE_VIEW_EVENT_NAME);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function logPageEvent(event, eventName) {
|
|
318
|
+
var params = cloneEventAttributes(event);
|
|
319
|
+
var eventID = createEventId(event);
|
|
320
|
+
|
|
321
|
+
eventName = eventName || event.EventName;
|
|
322
|
+
if (event.EventName) {
|
|
323
|
+
params['content_name'] = event.EventName;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fbq('trackCustom', eventName || 'customEvent', params, eventID);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function cloneEventAttributes(event) {
|
|
330
|
+
var attr = {};
|
|
331
|
+
if (event && event.EventAttributes) {
|
|
332
|
+
try {
|
|
333
|
+
attr = JSON.parse(JSON.stringify(event.EventAttributes));
|
|
334
|
+
}
|
|
335
|
+
catch (e) {
|
|
336
|
+
//
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return attr;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function isNumeric(n) {
|
|
343
|
+
return !isNaN(parseFloat(n)) && isFinite(n);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function getEventCategoryString(event) {
|
|
347
|
+
|
|
348
|
+
var enumTypeValues;
|
|
349
|
+
var enumValue;
|
|
350
|
+
if (event.EventDataType == MessageType.Commerce) {
|
|
351
|
+
enumTypeValues = event.EventCategory ? mParticle.CommerceEventType : mParticle.ProductActionType;
|
|
352
|
+
enumValue = event.EventCategory || event.ProductAction.ProductActionType;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
enumTypeValues = mParticle.EventType;
|
|
356
|
+
enumValue = event.EventCategory;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (enumTypeValues && enumValue) {
|
|
360
|
+
|
|
361
|
+
for (var category in enumTypeValues) {
|
|
362
|
+
if (enumValue == enumTypeValues[category]) {
|
|
363
|
+
return category;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
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
|
+
|
|
425
|
+
// https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
|
|
426
|
+
function createEventId(event) {
|
|
427
|
+
return {
|
|
428
|
+
eventID: event.SourceMessageId || null
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.init = initForwarder;
|
|
433
|
+
this.process = processEvent;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
function getId() {
|
|
437
|
+
return moduleId;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function register(config) {
|
|
441
|
+
if (!config) {
|
|
442
|
+
console.log('You must pass a config object to register the kit ' + name);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (!isobject$1(config)) {
|
|
447
|
+
console.log('\'config\' must be an object. You passed in a ' + typeof config);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (isobject$1(config.kits)) {
|
|
452
|
+
config.kits[name] = {
|
|
453
|
+
constructor: constructor
|
|
454
|
+
};
|
|
455
|
+
} else {
|
|
456
|
+
config.kits = {};
|
|
457
|
+
config.kits[name] = {
|
|
458
|
+
constructor: constructor
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
console.log('Successfully registered ' + name + ' to your mParticle configuration');
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (typeof window !== 'undefined') {
|
|
465
|
+
if (window && window.mParticle && window.mParticle.addForwarder) {
|
|
466
|
+
window.mParticle.addForwarder({
|
|
467
|
+
name: name,
|
|
468
|
+
constructor: constructor,
|
|
469
|
+
getId: getId
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
var FacebookEventForwarder = {
|
|
475
|
+
register: register
|
|
476
|
+
};
|
|
477
|
+
var FacebookEventForwarder_1 = FacebookEventForwarder.register;
|
|
478
|
+
|
|
479
|
+
export default FacebookEventForwarder;
|
|
480
|
+
export { FacebookEventForwarder_1 as register };
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
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",
|
|
7
|
+
"module": "dist/FacebookEventForwarder.esm.js",
|
|
7
8
|
"browser": "dist/FacebookEventForwarder.common.js",
|
|
8
9
|
"files": [
|
|
9
|
-
"dist/FacebookEventForwarder.common.js"
|
|
10
|
+
"dist/FacebookEventForwarder.common.js",
|
|
11
|
+
"dist/FacebookEventForwarder.esm.js"
|
|
10
12
|
],
|
|
11
13
|
"repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-facebook",
|
|
12
14
|
"scripts": {
|
|
@@ -15,12 +17,24 @@
|
|
|
15
17
|
"test": "node test/boilerplate/test-karma.js"
|
|
16
18
|
},
|
|
17
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",
|
|
18
28
|
"chai": "^4.2.0",
|
|
19
29
|
"karma": "^5.1.0",
|
|
20
30
|
"karma-chai": "^0.1.0",
|
|
21
31
|
"karma-chrome-launcher": "^3.1.0",
|
|
32
|
+
"karma-edge-launcher": "^0.4.2",
|
|
22
33
|
"karma-firefox-launcher": "^1.3.0",
|
|
34
|
+
"karma-ie-launcher": "^1.0.0",
|
|
35
|
+
"karma-junit-reporter": "^1.2.0",
|
|
23
36
|
"karma-mocha": "^2.0.1",
|
|
37
|
+
"karma-safari-launcher": "^1.0.0",
|
|
24
38
|
"mocha": "^2.5.3",
|
|
25
39
|
"rollup": "^1.13.1",
|
|
26
40
|
"rollup-plugin-commonjs": "^10.0.0",
|
|
@@ -30,8 +44,7 @@
|
|
|
30
44
|
"watchify": "^3.11.1"
|
|
31
45
|
},
|
|
32
46
|
"dependencies": {
|
|
33
|
-
"isobject": "^4.0.0"
|
|
34
|
-
"@mparticle/web-sdk": "^2.11.1"
|
|
47
|
+
"isobject": "^4.0.0"
|
|
35
48
|
},
|
|
36
49
|
"license": "Apache-2.0"
|
|
37
50
|
}
|