@mparticle/web-facebook-kit 2.1.1 → 2.3.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.
@@ -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
  //
@@ -23,369 +34,473 @@ function isObject(val) {
23
34
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
35
  // See the License for the specific language governing permissions and
25
36
  // limitations under the License.
26
-
27
-
28
- var name = 'Facebook',
29
- moduleId = 45,
30
- MessageType = {
31
- SessionStart: 1,
32
- SessionEnd: 2,
33
- PageView: 3,
34
- PageEvent: 4,
35
- CrashReport: 5,
36
- OptOut: 6,
37
- Commerce: 16,
38
- },
39
- IdentityType = {
40
- Other: 0,
41
- CustomerId: 1,
42
- Facebook: 2,
43
- Twitter: 3,
44
- Google: 4,
45
- Microsoft: 5,
46
- Yahoo: 6,
47
- Email: 7,
48
- FacebookCustomAudienceId: 9,
49
- Other2: 10,
50
- Other3: 11,
51
- Other4: 12,
52
- Other5: 13,
53
- Other6: 14,
54
- Other7: 15,
55
- Other8: 16,
56
- Other9: 17,
57
- Other10: 18,
58
- MobileNumber: 19,
59
- PhoneNumber2: 20,
60
- PhoneNumber3: 21,
61
- },
62
- SupportedCommerceTypes = [],
63
- // Standard FB Event Names from https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
64
- ADD_TO_CART_EVENT_NAME = 'AddToCart',
65
- ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist',
66
- CHECKOUT_EVENT_NAME = 'InitiateCheckout',
67
- PAGE_VIEW_EVENT_NAME = 'PageView',
68
- PURCHASE_EVENT_NAME = 'Purchase',
69
- REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart',
70
- VIEW_CONTENT_EVENT_NAME = 'ViewContent',
71
- constructor = function () {
72
- var self = this,
73
- isInitialized = false,
74
- reportingService = null;
75
-
76
- self.name = name;
77
-
78
- function initForwarder(settings, service, testMode, trackerId, userAttributes, userIdentities) {
79
- reportingService = service;
80
-
81
- SupportedCommerceTypes = [
82
- mParticle.ProductActionType.Checkout,
83
- mParticle.ProductActionType.Purchase,
84
- mParticle.ProductActionType.AddToCart,
85
- mParticle.ProductActionType.RemoveFromCart,
86
- mParticle.ProductActionType.AddToWishlist,
87
- mParticle.ProductActionType.ViewDetail
88
- ];
89
37
 
90
- try {
91
- if (!testMode) {
92
- !function (f, b, e, v, n, t, s) {
93
- 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;
94
- 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];
95
- s.parentNode.insertBefore(t, s);
96
- } (window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
97
-
98
- var visitorData = {};
99
-
100
- if(settings.externalUserIdentityType && userIdentities && userIdentities.length > 0) {
101
- var selectedType = IdentityType[settings.externalUserIdentityType];
102
- var selectedIdentity = userIdentities.filter(function (identityElement) {
103
- if (identityElement.Type === selectedType) {
104
- return identityElement.Identity;
105
- }
106
- });
107
-
108
- if (selectedIdentity.length > 0) {
109
- visitorData['external_id'] = selectedIdentity[0].Identity;
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;
110
119
  }
120
+ });
121
+
122
+ if (selectedIdentity.length > 0) {
123
+ visitorData['external_id'] = selectedIdentity[0].Identity;
111
124
  }
112
125
  }
126
+ }
113
127
 
114
- if (settings.disablePushState === 'True') {
115
- // Facebook will automatically track page views whenever a new state is pushed to the HTML 5 History State API
116
- // this option can be disabled to prevent duplicate page views
117
- // https://developers.facebook.com/docs/facebook-pixel/implementation/tag_spa/#tagging-single-page-applications
118
- fbq.disablePushState = true;
119
- }
120
- fbq('init', settings.pixelId, visitorData);
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();
121
137
 
122
- isInitialized = true;
138
+ isInitialized = true;
123
139
 
124
- return 'Successfully initialized: ' + name;
140
+ return 'Successfully initialized: ' + name;
125
141
 
126
- }
127
- catch (e) {
128
- return 'Can\'t initialize forwarder: ' + name + ':' + e;
129
- }
130
142
  }
143
+ catch (e) {
144
+ return 'Can\'t initialize forwarder: ' + name + ':' + e;
145
+ }
146
+ }
131
147
 
132
- function processEvent(event) {
133
- var reportEvent = false;
134
-
135
- if (!isInitialized) {
136
- return 'Can\'t send forwarder ' + name + ', not initialized';
137
- }
148
+ function loadMappings() {
149
+ productAttributeMapping = settings.productAttributeMapping
150
+ ? JSON.parse(settings.productAttributeMapping.replace(/"/g, '"'))
151
+ : [];
152
+ }
138
153
 
139
- try {
140
- if (event.EventDataType == MessageType.PageView) {
141
- reportEvent = true;
142
- logPageView(event);
143
- }
144
- else if (event.EventDataType == MessageType.PageEvent) {
145
- reportEvent = true;
146
- logPageEvent(event);
147
- }
148
- else if (event.EventDataType == MessageType.Commerce) {
149
- reportEvent = logCommerceEvent(event);
150
- }
154
+ function processEvent(event) {
155
+ var reportEvent = false;
151
156
 
152
- if (reportEvent && reportingService) {
153
- reportingService(self, event);
154
- }
157
+ if (!isInitialized) {
158
+ return 'Can\'t send forwarder ' + name + ', not initialized';
159
+ }
155
160
 
156
- return 'Successfully sent to forwarder ' + name;
161
+ try {
162
+ if (event.EventDataType == MessageType.PageView) {
163
+ reportEvent = true;
164
+ logPageView(event);
157
165
  }
158
- catch (error) {
159
- return 'Can\'t send to forwarder: ' + name + ' ' + error;
166
+ else if (event.EventDataType == MessageType.PageEvent) {
167
+ reportEvent = true;
168
+ logPageEvent(event);
160
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;
161
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
+ var eventName,
191
+ totalValue,
192
+ params = cloneEventAttributes(event),
193
+ eventID = createEventId(event),
194
+ sendProductNamesAsContents = settings.sendProductNamesasContents || false;
162
195
 
163
- function logCommerceEvent(event) {
164
- if (event.ProductAction &&
165
- event.ProductAction.ProductList &&
166
- event.ProductAction.ProductActionType &&
167
- SupportedCommerceTypes.indexOf(event.ProductAction.ProductActionType) > -1) {
196
+ params['currency'] = event.CurrencyCode || 'USD';
168
197
 
169
- var eventName,
170
- totalValue,
171
- params = cloneEventAttributes(event),
172
- eventID = createEventId(event);
173
- params['currency'] = event.CurrencyCode || 'USD';
198
+ if (event.EventName) {
199
+ params['content_name'] = event.EventName;
200
+ }
174
201
 
175
- if (event.EventName) {
176
- params['content_name'] = event.EventName;
202
+ var productSkus = event.ProductAction.ProductList.reduce(function (arr, curr) {
203
+ if (curr.Sku) {
204
+ arr.push(curr.Sku);
177
205
  }
206
+ return arr;
207
+ }, []);
178
208
 
179
- var productSkus = event.ProductAction.ProductList.reduce(function (arr, curr) {
180
- if (curr.Sku) {
181
- arr.push(curr.Sku);
182
- }
183
- return arr;
184
- }, []);
209
+ if (productSkus && productSkus.length > 0) {
210
+ params['content_ids'] = productSkus;
211
+ }
185
212
 
186
- if (productSkus && productSkus.length > 0) {
187
- params['content_ids'] = productSkus;
213
+ // Override content_name with product names array if setting enabled
214
+ if (sendProductNamesAsContents) {
215
+ var productNames = buildProductNames(event.ProductAction.ProductList);
216
+ if (productNames && productNames.length > 0) {
217
+ params['content_name'] = productNames;
188
218
  }
219
+ }
189
220
 
190
- if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
191
- event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) {
192
- var eventCategory = getEventCategoryString(event);
193
- if (eventCategory) {
194
- params['content_category'] = eventCategory;
195
- }
196
- if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout && event.ProductAction.CheckoutStep) {
197
- params['checkout_step'] = event.ProductAction.CheckoutStep;
198
- }
199
- }
221
+ // Set order_id if TransactionId exists
222
+ if (event.ProductAction.TransactionId) {
223
+ params['order_id'] = event.ProductAction.TransactionId;
224
+ }
200
225
 
201
- if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart ||
202
- event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
203
- event.ProductAction.ProductActionType == mParticle.ProductActionType.ViewDetail) {
226
+ // Build contents array
227
+ var contents = buildProductContents(event.ProductAction.ProductList);
228
+ if (contents && contents.length > 0) {
229
+ params['contents'] = contents;
230
+ }
204
231
 
205
- totalValue = event.ProductAction.ProductList.reduce(function(sum, product){
206
- if (isNumeric(product.Price) && isNumeric(product.Quantity)) {
207
- sum += product.Price * product.Quantity;
208
- }
209
- return sum;
210
- }, 0);
232
+ if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
233
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout) {
234
+ var eventCategory = getEventCategoryString(event);
235
+ if (eventCategory) {
236
+ params['content_category'] = eventCategory;
237
+ }
238
+ if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout && event.ProductAction.CheckoutStep) {
239
+ params['checkout_step'] = event.ProductAction.CheckoutStep;
240
+ }
241
+ }
211
242
 
212
- params['value'] = totalValue;
243
+ if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart ||
244
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
245
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.ViewDetail) {
213
246
 
214
- if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist){
215
- eventName = ADD_TO_WISHLIST_EVENT_NAME;
216
- }
217
- else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
218
- eventName = ADD_TO_CART_EVENT_NAME;
219
- }
220
- else{
221
- eventName = VIEW_CONTENT_EVENT_NAME;
247
+ totalValue = event.ProductAction.ProductList.reduce(function(sum, product){
248
+ if (isNumeric(product.Price) && isNumeric(product.Quantity)) {
249
+ sum += product.Price * product.Quantity;
222
250
  }
251
+ return sum;
252
+ }, 0);
253
+
254
+ params['value'] = totalValue;
223
255
 
256
+ if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist){
257
+ eventName = ADD_TO_WISHLIST_EVENT_NAME;
258
+ }
259
+ else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
260
+ eventName = ADD_TO_CART_EVENT_NAME;
261
+ }
262
+ else {
263
+ eventName = VIEW_CONTENT_EVENT_NAME;
224
264
  }
225
- else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
226
- event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
227
265
 
228
- eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
266
+ }
267
+ else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
268
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
229
269
 
230
- if (event.ProductAction.TotalAmount) {
231
- params['value'] = event.ProductAction.TotalAmount;
232
- }
270
+ eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
271
+
272
+ if (event.ProductAction.TotalAmount) {
273
+ params['value'] = event.ProductAction.TotalAmount;
274
+ }
233
275
 
234
- var num_items = event.ProductAction.ProductList.reduce(function(sum, product){
235
- if (isNumeric(product.Quantity)) {
236
- sum += product.Quantity;
276
+ var num_items = event.ProductAction.ProductList.reduce(function(sum, product){
277
+ if (isNumeric(product.Quantity)) {
278
+ sum += product.Quantity;
279
+ }
280
+ return sum;
281
+ }, 0);
282
+ params['num_items'] = num_items;
283
+ }
284
+ else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
285
+ eventName = REMOVE_FROM_CART_EVENT_NAME;
286
+
287
+ // remove from cart can be performed in 1 of 2 ways:
288
+ // 1. mParticle.eCommerce.logProductEvent(), which contains event.ProductAction.TotalAmount
289
+ // 2. mParticle.eCommerce.Cart.remove(), which does not contain event.ProductAction.TotalAmount
290
+ // when there is no TotalAmount, a manual calculation must be done
291
+ if (event.ProductAction.TotalAmount) {
292
+ totalValue = event.ProductAction.TotalAmount;
293
+ } else {
294
+ totalValue = event.ProductAction.ProductList.reduce(function(sum, product) {
295
+ if (isNumeric(product.TotalAmount)) {
296
+ sum += product.TotalAmount;
237
297
  }
238
298
  return sum;
239
299
  }, 0);
240
- params['num_items'] = num_items;
241
300
  }
242
- else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
243
- eventName = REMOVE_FROM_CART_EVENT_NAME;
244
-
245
- // remove from cart can be performed in 1 of 2 ways:
246
- // 1. mParticle.eCommerce.logProductEvent(), which contains event.ProductAction.TotalAmount
247
- // 2. mParticle.eCommerce.Cart.remove(), which does not contain event.ProductAction.TotalAmount
248
- // when there is no TotalAmount, a manual calculation must be done
249
- if (event.ProductAction.TotalAmount) {
250
- totalValue = event.ProductAction.TotalAmount;
251
- } else {
252
- totalValue = event.ProductAction.ProductList.reduce(function(sum, product) {
253
- if (isNumeric(product.TotalAmount)) {
254
- sum += product.TotalAmount;
255
- }
256
- return sum;
257
- }, 0);
258
- }
259
-
260
- params['value'] = totalValue;
261
301
 
262
- fbq('trackCustom', eventName || 'customEvent', params, eventID);
263
- return true;
264
- }
265
-
266
- if (eventName) {
267
- fbq('track', eventName, params, eventID);
268
- }
269
- else {
270
- return false;
271
- }
302
+ params['value'] = totalValue;
272
303
 
304
+ fbq('trackCustom', eventName || 'customEvent', params, eventID);
273
305
  return true;
274
306
  }
275
307
 
276
- return false;
277
- }
308
+ if (eventName) {
309
+ fbq('track', eventName, params, eventID);
310
+ }
311
+ else {
312
+ return false;
313
+ }
278
314
 
279
- function logPageView(event) {
280
- logPageEvent(event, PAGE_VIEW_EVENT_NAME);
315
+ return true;
281
316
  }
282
317
 
283
- function logPageEvent(event, eventName) {
284
- var params = cloneEventAttributes(event);
285
- var eventID = createEventId(event);
286
-
287
- eventName = eventName || event.EventName;
288
- if (event.EventName) {
289
- params['content_name'] = event.EventName;
290
- }
318
+ return false;
319
+ }
291
320
 
292
- fbq('trackCustom', eventName || 'customEvent', params, eventID);
293
- }
321
+ function logPageView(event) {
322
+ logPageEvent(event, PAGE_VIEW_EVENT_NAME);
323
+ }
294
324
 
295
- function cloneEventAttributes(event) {
296
- var attr = {};
297
- if (event && event.EventAttributes) {
298
- try {
299
- attr = JSON.parse(JSON.stringify(event.EventAttributes));
300
- }
301
- catch (e) {
302
- //
303
- }
304
- }
305
- return attr;
306
- }
325
+ function logPageEvent(event, eventName) {
326
+ var params = cloneEventAttributes(event);
327
+ var eventID = createEventId(event);
307
328
 
308
- function isNumeric(n) {
309
- return !isNaN(parseFloat(n)) && isFinite(n);
329
+ eventName = eventName || event.EventName;
330
+ if (event.EventName) {
331
+ params['content_name'] = event.EventName;
310
332
  }
311
333
 
312
- function getEventCategoryString(event) {
334
+ fbq('trackCustom', eventName || 'customEvent', params, eventID);
335
+ }
313
336
 
314
- var enumTypeValues;
315
- var enumValue;
316
- if (event.EventDataType == MessageType.Commerce) {
317
- enumTypeValues = event.EventCategory ? mParticle.CommerceEventType : mParticle.ProductActionType;
318
- enumValue = event.EventCategory || event.ProductAction.ProductActionType;
337
+ function cloneEventAttributes(event) {
338
+ var attr = {};
339
+ if (event && event.EventAttributes) {
340
+ try {
341
+ attr = JSON.parse(JSON.stringify(event.EventAttributes));
319
342
  }
320
- else {
321
- enumTypeValues = mParticle.EventType;
322
- enumValue = event.EventCategory;
343
+ catch (e) {
344
+ //
323
345
  }
346
+ }
347
+ return attr;
348
+ }
324
349
 
325
- if (enumTypeValues && enumValue) {
350
+ function isNumeric(n) {
351
+ return !isNaN(parseFloat(n)) && isFinite(n);
352
+ }
326
353
 
327
- for (var category in enumTypeValues) {
328
- if (enumValue == enumTypeValues[category]) {
329
- return category;
330
- }
331
- }
332
- }
354
+ function getEventCategoryString(event) {
333
355
 
334
- return null;
356
+ var enumTypeValues;
357
+ var enumValue;
358
+ if (event.EventDataType == MessageType.Commerce) {
359
+ enumTypeValues = event.EventCategory ? mParticle.CommerceEventType : mParticle.ProductActionType;
360
+ enumValue = event.EventCategory || event.ProductAction.ProductActionType;
361
+ }
362
+ else {
363
+ enumTypeValues = mParticle.EventType;
364
+ enumValue = event.EventCategory;
335
365
  }
336
366
 
337
- // https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
338
- function createEventId(event) {
339
- return {
340
- eventID: event.SourceMessageId || null
367
+ if (enumTypeValues && enumValue) {
368
+
369
+ for (var category in enumTypeValues) {
370
+ if (enumValue == enumTypeValues[category]) {
371
+ return category;
372
+ }
341
373
  }
342
374
  }
343
375
 
344
- this.init = initForwarder;
345
- this.process = processEvent;
346
- };
347
-
348
- function getId() {
349
- return moduleId;
350
- }
376
+ return null;
377
+ }
351
378
 
352
- function register(config) {
353
- if (!config) {
354
- console.log('You must pass a config object to register the kit ' + name);
355
- return;
379
+ /**
380
+ * Builds contents array for Facebook Pixel commerce events.
381
+ * Creates a nested array of content items with product details.
382
+ *
383
+ * @param {Array} productList - Array of products from event.ProductAction.ProductList
384
+ * @returns {Array} Array of content objects for Facebook Pixel
385
+ */
386
+ function buildProductContents(productList) {
387
+ if (!productList || productList.length === 0) {
388
+ return [];
389
+ }
390
+
391
+ return productList
392
+ .filter(function(product) {
393
+ return product && product.Sku;
394
+ })
395
+ .map(function(product) {
396
+ var contentItem = {
397
+ id: product.Sku,
398
+ quantity: isNumeric(product.Quantity) ? product.Quantity : 1,
399
+ name: product.Name,
400
+ brand: product.Brand,
401
+ category: product.Category,
402
+ variant: product.Variant,
403
+ item_price: isNumeric(product.Price) ? product.Price : null
404
+ };
405
+
406
+ // Apply configured mappings to custom attributes
407
+ productAttributeMapping.forEach(function(productMapping) {
408
+ if (!isobject$1(productMapping) || !productMapping.map || !productMapping.value) {
409
+ return;
410
+ }
411
+
412
+ var sourceField = productMapping.map;
413
+ var facebookFieldName = productMapping.value;
414
+ var value = null;
415
+
416
+ // Check for Product level field first
417
+ if (product.hasOwnProperty(sourceField)) {
418
+ value = product[sourceField];
419
+ }
420
+ // then check for Product.Attributes level field
421
+ else if (product.Attributes && product.Attributes[sourceField]) {
422
+ value = product.Attributes[sourceField];
423
+ }
424
+
425
+ if (value !== null && value !== undefined) {
426
+ contentItem[facebookFieldName] = value;
427
+ }
428
+ });
429
+ return contentItem;
430
+ });
356
431
  }
357
432
 
358
- if (!isObject(config)) {
359
- console.log('\'config\' must be an object. You passed in a ' + typeof config);
360
- return;
433
+ // https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
434
+ function createEventId(event) {
435
+ return {
436
+ eventID: event.SourceMessageId || null
437
+ }
361
438
  }
362
439
 
363
- if (isObject(config.kits)) {
364
- config.kits[name] = {
365
- constructor: constructor
366
- };
367
- } else {
368
- config.kits = {};
369
- config.kits[name] = {
370
- constructor: constructor
371
- };
440
+ /**
441
+ * Builds array of product names from product list
442
+ * @param {Array} productList - Array of products
443
+ * @returns {Array} Array of product names
444
+ */
445
+ function buildProductNames(productList) {
446
+ if (!productList || productList.length === 0) {
447
+ return [];
448
+ }
449
+
450
+ return productList
451
+ .filter(function(product) {
452
+ return product && product.Name;
453
+ })
454
+ .map(function(product) {
455
+ return product.Name;
456
+ });
372
457
  }
373
- console.log('Successfully registered ' + name + ' to your mParticle configuration');
458
+
459
+ this.init = initForwarder;
460
+ this.process = processEvent;
461
+ };
462
+
463
+ function getId() {
464
+ return moduleId;
465
+ }
466
+
467
+ function register(config) {
468
+ if (!config) {
469
+ console.log('You must pass a config object to register the kit ' + name);
470
+ return;
374
471
  }
375
472
 
376
- if (typeof window !== 'undefined') {
377
- if (window && window.mParticle && window.mParticle.addForwarder) {
378
- window.mParticle.addForwarder({
379
- name: name,
380
- constructor: constructor,
381
- getId: getId
382
- });
383
- }
473
+ if (!isobject$1(config)) {
474
+ console.log('\'config\' must be an object. You passed in a ' + typeof config);
475
+ return;
476
+ }
477
+
478
+ if (isobject$1(config.kits)) {
479
+ config.kits[name] = {
480
+ constructor: constructor
481
+ };
482
+ } else {
483
+ config.kits = {};
484
+ config.kits[name] = {
485
+ constructor: constructor
486
+ };
487
+ }
488
+ console.log('Successfully registered ' + name + ' to your mParticle configuration');
489
+ }
490
+
491
+ if (typeof window !== 'undefined') {
492
+ if (window && window.mParticle && window.mParticle.addForwarder) {
493
+ window.mParticle.addForwarder({
494
+ name: name,
495
+ constructor: constructor,
496
+ getId: getId
497
+ });
384
498
  }
499
+ }
385
500
 
386
- var FacebookEventForwarder = {
387
- register: register
388
- };
501
+ var FacebookEventForwarder = {
502
+ register: register
503
+ };
389
504
  var FacebookEventForwarder_1 = FacebookEventForwarder.register;
390
505
 
391
506
  export default FacebookEventForwarder;