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