@mparticle/web-facebook-kit 2.0.4 → 2.1.1

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.
@@ -46,7 +46,7 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
46
46
  PageEvent: 4,
47
47
  CrashReport: 5,
48
48
  OptOut: 6,
49
- Commerce: 16
49
+ Commerce: 16,
50
50
  },
51
51
  IdentityType = {
52
52
  Other: 0,
@@ -72,6 +72,14 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
72
72
  PhoneNumber3: 21,
73
73
  },
74
74
  SupportedCommerceTypes = [],
75
+ // Standard FB Event Names from https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
76
+ ADD_TO_CART_EVENT_NAME = 'AddToCart',
77
+ ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist',
78
+ CHECKOUT_EVENT_NAME = 'InitiateCheckout',
79
+ PAGE_VIEW_EVENT_NAME = 'PageView',
80
+ PURCHASE_EVENT_NAME = 'Purchase',
81
+ REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart',
82
+ VIEW_CONTENT_EVENT_NAME = 'ViewContent',
75
83
  constructor = function () {
76
84
  var self = this,
77
85
  isInitialized = false,
@@ -113,9 +121,15 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
113
121
  visitorData['external_id'] = selectedIdentity[0].Identity;
114
122
  }
115
123
  }
124
+ }
116
125
 
117
- fbq('init', settings.pixelId, visitorData);
126
+ if (settings.disablePushState === 'True') {
127
+ // Facebook will automatically track page views whenever a new state is pushed to the HTML 5 History State API
128
+ // this option can be disabled to prevent duplicate page views
129
+ // https://developers.facebook.com/docs/facebook-pixel/implementation/tag_spa/#tagging-single-page-applications
130
+ fbq.disablePushState = true;
118
131
  }
132
+ fbq('init', settings.pixelId, visitorData);
119
133
 
120
134
  isInitialized = true;
121
135
 
@@ -210,20 +224,20 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
210
224
  params['value'] = totalValue;
211
225
 
212
226
  if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist){
213
- eventName = 'AddToWishlist';
227
+ eventName = ADD_TO_WISHLIST_EVENT_NAME;
214
228
  }
215
229
  else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
216
- eventName = 'AddToCart';
230
+ eventName = ADD_TO_CART_EVENT_NAME;
217
231
  }
218
232
  else{
219
- eventName = 'ViewContent';
233
+ eventName = VIEW_CONTENT_EVENT_NAME;
220
234
  }
221
235
 
222
236
  }
223
237
  else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
224
238
  event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
225
239
 
226
- eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? 'InitiateCheckout' : 'Purchase';
240
+ eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
227
241
 
228
242
  if (event.ProductAction.TotalAmount) {
229
243
  params['value'] = event.ProductAction.TotalAmount;
@@ -238,7 +252,7 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
238
252
  params['num_items'] = num_items;
239
253
  }
240
254
  else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
241
- eventName = 'RemoveFromCart';
255
+ eventName = REMOVE_FROM_CART_EVENT_NAME;
242
256
 
243
257
  // remove from cart can be performed in 1 of 2 ways:
244
258
  // 1. mParticle.eCommerce.logProductEvent(), which contains event.ProductAction.TotalAmount
@@ -275,7 +289,7 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
275
289
  }
276
290
 
277
291
  function logPageView(event) {
278
- logPageEvent(event, 'Viewed ' + event.EventName);
292
+ logPageEvent(event, PAGE_VIEW_EVENT_NAME);
279
293
  }
280
294
 
281
295
  function logPageEvent(event, eventName) {
@@ -286,6 +300,7 @@ var isobject$1 = getCjsExportFromNamespace(isobject);
286
300
  if (event.EventName) {
287
301
  params['content_name'] = event.EventName;
288
302
  }
303
+
289
304
  fbq('trackCustom', eventName || 'customEvent', params, eventID);
290
305
  }
291
306
 
@@ -0,0 +1,392 @@
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
+ /* eslint-disable no-undef */
13
+ // Copyright 2015 mParticle, Inc.
14
+ //
15
+ // Licensed under the Apache License, Version 2.0 (the "License");
16
+ // you may not use this file except in compliance with the License.
17
+ // You may obtain a copy of the License at
18
+ //
19
+ // http://www.apache.org/licenses/LICENSE-2.0
20
+ //
21
+ // Unless required by applicable law or agreed to in writing, software
22
+ // distributed under the License is distributed on an "AS IS" BASIS,
23
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ // See the License for the specific language governing permissions and
25
+ // 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
+
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;
110
+ }
111
+ }
112
+ }
113
+
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);
121
+
122
+ isInitialized = true;
123
+
124
+ return 'Successfully initialized: ' + name;
125
+
126
+ }
127
+ catch (e) {
128
+ return 'Can\'t initialize forwarder: ' + name + ':' + e;
129
+ }
130
+ }
131
+
132
+ function processEvent(event) {
133
+ var reportEvent = false;
134
+
135
+ if (!isInitialized) {
136
+ return 'Can\'t send forwarder ' + name + ', not initialized';
137
+ }
138
+
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
+ }
151
+
152
+ if (reportEvent && reportingService) {
153
+ reportingService(self, event);
154
+ }
155
+
156
+ return 'Successfully sent to forwarder ' + name;
157
+ }
158
+ catch (error) {
159
+ return 'Can\'t send to forwarder: ' + name + ' ' + error;
160
+ }
161
+ }
162
+
163
+ function logCommerceEvent(event) {
164
+ if (event.ProductAction &&
165
+ event.ProductAction.ProductList &&
166
+ event.ProductAction.ProductActionType &&
167
+ SupportedCommerceTypes.indexOf(event.ProductAction.ProductActionType) > -1) {
168
+
169
+ var eventName,
170
+ totalValue,
171
+ params = cloneEventAttributes(event),
172
+ eventID = createEventId(event);
173
+ params['currency'] = event.CurrencyCode || 'USD';
174
+
175
+ if (event.EventName) {
176
+ params['content_name'] = event.EventName;
177
+ }
178
+
179
+ var productSkus = event.ProductAction.ProductList.reduce(function (arr, curr) {
180
+ if (curr.Sku) {
181
+ arr.push(curr.Sku);
182
+ }
183
+ return arr;
184
+ }, []);
185
+
186
+ if (productSkus && productSkus.length > 0) {
187
+ params['content_ids'] = productSkus;
188
+ }
189
+
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
+ }
200
+
201
+ if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart ||
202
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist ||
203
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.ViewDetail) {
204
+
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);
211
+
212
+ params['value'] = totalValue;
213
+
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;
222
+ }
223
+
224
+ }
225
+ else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
226
+ event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
227
+
228
+ eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
229
+
230
+ if (event.ProductAction.TotalAmount) {
231
+ params['value'] = event.ProductAction.TotalAmount;
232
+ }
233
+
234
+ var num_items = event.ProductAction.ProductList.reduce(function(sum, product){
235
+ if (isNumeric(product.Quantity)) {
236
+ sum += product.Quantity;
237
+ }
238
+ return sum;
239
+ }, 0);
240
+ params['num_items'] = num_items;
241
+ }
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
+
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
+ }
272
+
273
+ return true;
274
+ }
275
+
276
+ return false;
277
+ }
278
+
279
+ function logPageView(event) {
280
+ logPageEvent(event, PAGE_VIEW_EVENT_NAME);
281
+ }
282
+
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
+ }
291
+
292
+ fbq('trackCustom', eventName || 'customEvent', params, eventID);
293
+ }
294
+
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
+ }
307
+
308
+ function isNumeric(n) {
309
+ return !isNaN(parseFloat(n)) && isFinite(n);
310
+ }
311
+
312
+ function getEventCategoryString(event) {
313
+
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;
319
+ }
320
+ else {
321
+ enumTypeValues = mParticle.EventType;
322
+ enumValue = event.EventCategory;
323
+ }
324
+
325
+ if (enumTypeValues && enumValue) {
326
+
327
+ for (var category in enumTypeValues) {
328
+ if (enumValue == enumTypeValues[category]) {
329
+ return category;
330
+ }
331
+ }
332
+ }
333
+
334
+ return null;
335
+ }
336
+
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
341
+ }
342
+ }
343
+
344
+ this.init = initForwarder;
345
+ this.process = processEvent;
346
+ };
347
+
348
+ function getId() {
349
+ return moduleId;
350
+ }
351
+
352
+ function register(config) {
353
+ if (!config) {
354
+ console.log('You must pass a config object to register the kit ' + name);
355
+ return;
356
+ }
357
+
358
+ if (!isObject(config)) {
359
+ console.log('\'config\' must be an object. You passed in a ' + typeof config);
360
+ return;
361
+ }
362
+
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
+ };
372
+ }
373
+ console.log('Successfully registered ' + name + ' to your mParticle configuration');
374
+ }
375
+
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
+ }
384
+ }
385
+
386
+ var FacebookEventForwarder = {
387
+ register: register
388
+ };
389
+ var FacebookEventForwarder_1 = FacebookEventForwarder.register;
390
+
391
+ export default FacebookEventForwarder;
392
+ 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.0.4",
3
+ "version": "2.1.1",
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": {