@mparticle/web-taplytics-kit 2.0.1 → 3.1.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.
@@ -28,307 +28,298 @@ function isObject(val) {
28
28
  // See the License for the specific language governing permissions and
29
29
  // limitations under the License.
30
30
 
31
-
32
-
33
- var name = 'Taplytics',
34
- moduleId = 129,
35
- MessageType = {
36
- SessionStart: 1,
37
- SessionEnd: 2,
38
- PageView: 3,
39
- PageEvent: 4,
40
- CrashReport: 5,
41
- OptOut: 6,
42
- Commerce: 16
43
- };
44
-
45
- var constructor = function () {
46
- var self = this,
47
- isInitialized = false,
48
- reportingService,
49
- eventQueue = [],
50
- settings = {},
51
- initUserAttributes = {},
52
- initUserIdentities = [];
53
31
 
54
- self.name = name;
55
32
 
56
- function initForwarder(forwarderSettings, service, testMode, trackerId, userAttributes, userIdentities) {
57
- reportingService = service;
58
- settings = forwarderSettings;
59
- initUserAttributes = userAttributes;
60
- initUserIdentities = userIdentities;
33
+ var name = 'Taplytics',
34
+ moduleId = 129,
35
+ MessageType = {
36
+ SessionStart: 1,
37
+ SessionEnd: 2,
38
+ PageView: 3,
39
+ PageEvent: 4,
40
+ CrashReport: 5,
41
+ OptOut: 6,
42
+ Commerce: 16,
43
+ };
61
44
 
62
- try {
63
- if (!testMode && !window.Taplytics) {
64
- var taplyticsScript = document.createElement('script');
65
- taplyticsScript.type = 'text/javascript';
66
- taplyticsScript.async = true;
67
- taplyticsScript.src = getTaplyticsSourceLink();
68
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(taplyticsScript);
69
- taplyticsScript.onload = function() {
70
- isInitialized = true;
71
-
72
- // On load, if the clientsdk exists and there are events
73
- // in the eventQueue, process each event
74
- if (window.Taplytics && eventQueue.length > 0) {
75
- // Process any events that may have been queued up
76
- // while forwarder was being initialized.
77
- eventQueue.forEach(function(event) {
78
- processEvent(event);
79
- });
80
-
81
- eventQueue = [];
82
- }
83
- };
84
- }
85
- else {
45
+ var constructor = function() {
46
+ var self = this,
47
+ isInitialized = false,
48
+ reportingService,
49
+ eventQueue = [],
50
+ settings = {},
51
+ initUserAttributes = {},
52
+ initUserIdentities = [];
53
+
54
+ self.name = name;
55
+
56
+ function initForwarder(
57
+ forwarderSettings,
58
+ service,
59
+ testMode,
60
+ trackerId,
61
+ userAttributes,
62
+ userIdentities
63
+ ) {
64
+ reportingService = service;
65
+ settings = forwarderSettings;
66
+ initUserAttributes = userAttributes;
67
+ initUserIdentities = userIdentities;
68
+
69
+ try {
70
+ if (!testMode && !window.Taplytics) {
71
+ var taplyticsScript = document.createElement('script');
72
+ taplyticsScript.type = 'text/javascript';
73
+ taplyticsScript.async = true;
74
+ taplyticsScript.src = getTaplyticsSourceLink();
75
+ (
76
+ document.getElementsByTagName('head')[0] ||
77
+ document.getElementsByTagName('body')[0]
78
+ ).appendChild(taplyticsScript);
79
+ taplyticsScript.onload = function() {
86
80
  isInitialized = true;
87
- if (testMode) {
88
- Taplytics.src = getTaplyticsSourceLink();
81
+
82
+ // On load, if the clientsdk exists and there are events
83
+ // in the eventQueue, process each event
84
+ if (window.Taplytics && eventQueue.length > 0) {
85
+ // Process any events that may have been queued up
86
+ // while forwarder was being initialized.
87
+ eventQueue.forEach(function(event) {
88
+ processEvent(event);
89
+ });
90
+
91
+ eventQueue = [];
89
92
  }
93
+ };
94
+ } else {
95
+ isInitialized = true;
96
+ if (testMode) {
97
+ Taplytics.src = getTaplyticsSourceLink();
90
98
  }
91
-
92
- return 'Taplytics successfully loaded';
93
99
  }
94
- catch (e) {
95
- return 'Failed to initialize: ' + e;
96
- }
97
- }
98
100
 
99
- /**
100
- * Called whenever an event occurs. Used to log different types of events.
101
- * @param event
102
- * @returns {string}
103
- */
104
- function processEvent(event) {
105
- var reportEvent = false;
106
- if (isInitialized) {
107
- try {
108
- if (event.EventDataType === MessageType.PageView) {
109
- reportEvent = logPageView(event);
110
- } else if (event.EventDataType === MessageType.Commerce) {
111
- if (event.EventCategory === mParticle.CommerceEventType.ProductPurchase) {
112
- reportEvent = logPurchaseEvent(event);
113
- } else {
114
- reportEvent = logCommerceEvent(event);
115
- }
116
- } else if (event.EventDataType === MessageType.PageEvent) {
117
- reportEvent = logEvent(event);
118
- }
101
+ return 'Taplytics successfully loaded';
102
+ } catch (e) {
103
+ return 'Failed to initialize: ' + e;
104
+ }
105
+ }
119
106
 
120
- // leave the below alone
121
- if (reportEvent === true && reportingService) {
122
- reportingService(self, event);
123
- return 'Successfully sent to ' + name;
107
+ /**
108
+ * Called whenever an event occurs. Used to log different types of events.
109
+ * @param event
110
+ * @returns {string}
111
+ */
112
+ function processEvent(event) {
113
+ var reportEvent = false;
114
+ if (isInitialized) {
115
+ try {
116
+ if (event.EventDataType === MessageType.PageView) {
117
+ reportEvent = logPageView(event);
118
+ } else if (event.EventDataType === MessageType.Commerce) {
119
+ if (
120
+ event.EventCategory ===
121
+ mParticle.CommerceEventType.ProductPurchase
122
+ ) {
123
+ reportEvent = logPurchaseEvent(event);
124
124
  } else {
125
- return 'Error logging event or event type not supported - ' + reportEvent.error;
125
+ reportEvent = logCommerceEvent(event);
126
126
  }
127
+ } else if (event.EventDataType === MessageType.PageEvent) {
128
+ reportEvent = logEvent(event);
127
129
  }
128
- catch (e) {
129
- return 'Failed to send to: ' + name + ' ' + e;
130
+
131
+ // leave the below alone
132
+ if (reportEvent === true && reportingService) {
133
+ reportingService(self, event);
134
+ return 'Successfully sent to ' + name;
135
+ } else {
136
+ return (
137
+ 'Error logging event or event type not supported - ' +
138
+ reportEvent.error
139
+ );
130
140
  }
141
+ } catch (e) {
142
+ return 'Failed to send to: ' + name + ' ' + e;
131
143
  }
132
- else {
133
- eventQueue.push(event);
134
- }
135
-
136
- return 'Can\'t send to forwarder ' + name + ', not initialized. Event added to queue.';
144
+ } else {
145
+ eventQueue.push(event);
137
146
  }
138
147
 
139
- /**
140
- * Helper method to check if object is empty
141
- * @param obj
142
- * @returns {boolean}
143
- */
144
- function isEmpty(obj) {
145
- for (var prop in obj) { return false; }
146
- return true;
148
+ return (
149
+ "Can't send to forwarder " +
150
+ name +
151
+ ', not initialized. Event added to queue.'
152
+ );
153
+ }
154
+
155
+ /**
156
+ * Helper method to check if object is empty
157
+ * @param obj
158
+ * @returns {boolean}
159
+ */
160
+ function isEmpty(obj) {
161
+ for (var prop in obj) {
162
+ return false;
147
163
  }
164
+ return true;
165
+ }
148
166
 
149
- /**
150
- * Helper method to merge obj 2 into obj1
151
- * @param obj1
152
- * @param obj2
153
- */
154
- function mergeObjects(obj1, obj2) {
155
- var obj = {};
156
- for (var key in obj1) {
157
- if (obj1[key]) {
158
- obj[key] = obj1[key];
159
- }
167
+ /**
168
+ * Helper method to merge obj 2 into obj1
169
+ * @param obj1
170
+ * @param obj2
171
+ */
172
+ function mergeObjects(obj1, obj2) {
173
+ var obj = {};
174
+ for (var key in obj1) {
175
+ if (obj1[key]) {
176
+ obj[key] = obj1[key];
160
177
  }
161
- for (var key in obj2) {
162
- if (obj2[key]) {
163
- obj[key] = obj2[key];
164
- }
178
+ }
179
+ for (var key in obj2) {
180
+ if (obj2[key]) {
181
+ obj[key] = obj2[key];
165
182
  }
166
- return obj;
167
183
  }
184
+ return obj;
185
+ }
168
186
 
169
- /**
170
- * Helper method to clone an object
171
- * @param {*} obj
172
- */
173
- function clone(obj) {
174
- var copy = {};
175
- for (var key in obj) {
176
- if (obj.hasOwnProperty(key)) {
187
+ /**
188
+ * Helper method to clone an object
189
+ * @param {*} obj
190
+ */
191
+ function clone(obj) {
192
+ var copy = {};
193
+ for (var key in obj) {
194
+ if (obj.hasOwnProperty(key)) {
177
195
  copy[key] = obj[key];
178
- }
179
- }
180
- return copy;
181
- }
182
-
183
- /**
184
- * tracks Taplytics events
185
- * @param {*} event
186
- * @param {*} revenue
187
- * @param {*} attributes
188
- */
189
- function trackEvent(event, revenue, attributes) {
190
- if (revenue) {
191
- if (attributes && !isEmpty(attributes)) {
192
- Taplytics.track(event, revenue, attributes);
193
- } else {
194
- Taplytics.track(event, revenue);
195
- }
196
- } else {
197
- if (attributes && !isEmpty(attributes)) {
198
- Taplytics.track(event, null, attributes);
199
- } else {
200
- Taplytics.track(event);
201
- }
202
196
  }
203
197
  }
198
+ return copy;
199
+ }
204
200
 
205
- /**
206
- * Construct Taplytics src link to load the SDK
207
- * @returns {string}
208
- */
209
- function getTaplyticsSourceLink() {
210
- var token = settings.apiKey;
211
- var cookieDomain = settings.taplyticsOptionCookieDomain;
212
- var timeout = settings.taplyticsOptionTimeout;
213
-
214
- var src = 'https://js.taplytics.com/jssdk/' + token + '.min.js';
215
- var query = '';
216
-
217
- if (timeout) {
218
- query = query + 'timeout=' + timeout;
201
+ /**
202
+ * tracks Taplytics events
203
+ * @param {*} event
204
+ * @param {*} revenue
205
+ * @param {*} attributes
206
+ */
207
+ function trackEvent(event, revenue, attributes) {
208
+ if (revenue) {
209
+ if (attributes && !isEmpty(attributes)) {
210
+ Taplytics.track(event, revenue, attributes);
211
+ } else {
212
+ Taplytics.track(event, revenue);
219
213
  }
220
-
221
- if (cookieDomain) {
222
- query = query + (query ? '&' : '') + 'cookieDomain=' + cookieDomain;
214
+ } else {
215
+ if (attributes && !isEmpty(attributes)) {
216
+ Taplytics.track(event, null, attributes);
217
+ } else {
218
+ Taplytics.track(event);
223
219
  }
220
+ }
221
+ }
224
222
 
225
- var userBucketing = settings.taplyticsOptionUserBucketing;
223
+ /**
224
+ * Construct Taplytics src link to load the SDK
225
+ * @returns {string}
226
+ */
227
+ function getTaplyticsSourceLink() {
228
+ var token = settings.apiKey;
229
+ var cookieDomain = settings.taplyticsOptionCookieDomain;
230
+ var timeout = settings.taplyticsOptionTimeout;
226
231
 
227
- if (userBucketing === 'True') {
228
- query = query + (query ? '&' : '') + 'user_bucketing=true';
229
- }
232
+ var src = 'https://js.taplytics.com/jssdk/' + token + '.min.js';
233
+ var query = '';
230
234
 
231
- var user_attributes = initUserAttributes || {};
232
-
233
- if (initUserIdentities.length) {
234
- var identity = initUserIdentities[0];
235
- var type = identity.Type;
236
- var id = identity.Identity;
237
- switch (type) {
238
- case 1:
239
- user_attributes['user_id'] = id;
240
- break;
241
- case 7:
242
- user_attributes['email'] = id;
243
- break;
244
- }
245
- }
235
+ if (timeout) {
236
+ query = query + 'timeout=' + timeout;
237
+ }
246
238
 
247
- if (!isEmpty(user_attributes)) {
248
- user_attributes = encodeURIComponent(JSON.stringify(user_attributes));
249
- query = query + (query ? '&' : '') + 'user_attributes' + user_attributes;
250
- }
239
+ if (cookieDomain) {
240
+ query = query + (query ? '&' : '') + 'cookieDomain=' + cookieDomain;
241
+ }
251
242
 
252
- if (query) {
253
- src = src + '?' + query;
254
- }
243
+ var userBucketing = settings.taplyticsOptionUserBucketing;
255
244
 
256
- return src;
245
+ if (userBucketing === 'True') {
246
+ query = query + (query ? '&' : '') + 'user_bucketing=true';
257
247
  }
258
248
 
259
- /**
260
- * Logs page view event to Taplytics
261
- * @param event
262
- * @returns {*}
263
- */
264
- function logPageView(event) {
265
- // Details on the `event` object schema in the README
266
- try {
267
- if (event.EventAttributes) {
268
- Taplytics.page(event.EventName, event.EventAttributes);
269
- }
270
- else {
271
- trackEvent(event.EventName);
272
- }
273
- return true;
274
- }
275
- catch (e) {
276
- return { error: e };
249
+ var user_attributes = initUserAttributes || {};
250
+
251
+ if (initUserIdentities.length) {
252
+ var identity = initUserIdentities[0];
253
+ var type = identity.Type;
254
+ var id = identity.Identity;
255
+ switch (type) {
256
+ case 1:
257
+ user_attributes['user_id'] = id;
258
+ break;
259
+ case 7:
260
+ user_attributes['email'] = id;
261
+ break;
277
262
  }
278
263
  }
279
264
 
280
- /**
281
- * Logs purchase events as revenue events to Taplytics
282
- * @param event
283
- * @returns {*}
284
- */
285
- function logPurchaseEvent(event) {
286
- var reportEvent = false;
287
- if (event.ProductAction.ProductList) {
288
- try {
289
- var productList = event.ProductAction.ProductList;
290
- productList.forEach(function(product) {
291
- var product = product;
292
- var attributes = {};
293
- var productAttributes = product;
294
- if (product.Attributes) {
295
- productAttributes = mergeObjects(product.Attributes, product);
296
- }
297
-
298
- if (event.EventAttributes) {
299
- attributes['EventAttributes'] = event.EventAttributes;
300
- }
301
-
302
- if (event.CurrencyCode) {
303
- attributes['CurrencyCode'] = event.CurrencyCode;
304
- }
305
-
306
- attributes['ProductAttributes'] = productAttributes;
307
-
308
- Taplytics.track(event.EventName, parseFloat(product.TotalAmount), attributes);
309
- });
310
- return true;
311
- }
312
- catch (e) {
313
- return {error: e};
314
- }
315
- }
265
+ if (!isEmpty(user_attributes)) {
266
+ user_attributes = encodeURIComponent(
267
+ JSON.stringify(user_attributes)
268
+ );
269
+ query =
270
+ query +
271
+ (query ? '&' : '') +
272
+ 'user_attributes=' +
273
+ user_attributes;
274
+ }
316
275
 
317
- return reportEvent;
276
+ if (query) {
277
+ src = src + '?' + query;
318
278
  }
319
279
 
320
- /**
321
- * Logs all other commerce events as regular events with metadata to Taplytics
322
- * @param event
323
- * @returns {*}
324
- */
325
- function logCommerceEvent(event) {
326
- var reportEvent = false;
280
+ return src;
281
+ }
282
+
283
+ /**
284
+ * Logs page view event to Taplytics
285
+ * @param event
286
+ * @returns {*}
287
+ */
288
+ function logPageView(event) {
289
+ // Details on the `event` object schema in the README
290
+ try {
291
+ if (event.EventAttributes) {
292
+ Taplytics.page(event.EventName, event.EventAttributes);
293
+ } else {
294
+ trackEvent(event.EventName);
295
+ }
296
+ return true;
297
+ } catch (e) {
298
+ return { error: e };
299
+ }
300
+ }
327
301
 
328
- var attributes = {};
302
+ /**
303
+ * Logs purchase events as revenue events to Taplytics
304
+ * @param event
305
+ * @returns {*}
306
+ */
307
+ function logPurchaseEvent(event) {
308
+ var reportEvent = false;
309
+ if (event.ProductAction.ProductList) {
310
+ try {
311
+ var productList = event.ProductAction.ProductList;
312
+ productList.forEach(function(product) {
313
+ var product = product;
314
+ var attributes = {};
315
+ var productAttributes = product;
316
+ if (product.Attributes) {
317
+ productAttributes = mergeObjects(
318
+ product.Attributes,
319
+ product
320
+ );
321
+ }
329
322
 
330
- if (event) {
331
- try {
332
323
  if (event.EventAttributes) {
333
324
  attributes['EventAttributes'] = event.EventAttributes;
334
325
  }
@@ -336,217 +327,297 @@ function isObject(val) {
336
327
  if (event.CurrencyCode) {
337
328
  attributes['CurrencyCode'] = event.CurrencyCode;
338
329
  }
339
-
340
- if (event.EventCategory === mParticle.CommerceEventType.PromotionClick) {
341
- var promotionList = event.PromotionAction.PromotionList;
342
- if (promotionList) {
343
- promotionList.forEach(function(promotion) {
344
- attributes["Promotion"] = promotion;
345
- trackEvent(event.EventName, null, attributes);
346
- });
347
- }
348
- } else if (event.EventCategory === mParticle.CommerceEventType.ProductImpression) {
349
- var impressions = event.ProductImpressions;
350
- if (impressions) {
351
- impressions.forEach(function(impression) {
352
- var productList = impression.ProductList;
353
- var impressionName = impression.ProductImpressionList;
354
- if (productList) {
355
- productList.forEach(function(product) {
356
- var productAttributes = product;
357
- var attributeCopy = clone(attributes);
358
- if (product.Attributes) {
359
- productAttributes = mergeObjects(product, product.Attributes);
360
- }
361
-
362
- attributeCopy["ProductImpression"] = impressionName;
363
- attributeCopy["ProductImpressionProduct"] = productAttributes;
364
- trackEvent(event.EventName, null, attributeCopy);
365
- });
366
- }
367
- });
368
- }
369
- } else if (event.EventCategory === mParticle.CommerceEventType.ProductAddToCart ||
370
- event.EventCategory === mParticle.CommerceEventType.ProductRemoveFromCart ||
371
- event.ProductAction) {
372
-
373
- if (event.ProductAction.ProductList) {
374
- var productList = event.ProductAction.ProductList;
375
- productList.forEach(function(product) {
376
- var productAttributes = product;
377
- if (product.Attributes) {
378
- productAttributes = mergeObjects(product, product.Attributes);
379
- }
380
- attributes["ProductAttributes"] = productAttributes;
381
- if (event.ShoppingCart && event.ShoppingCart.ProductList) {
382
- attributes["ShoppingCart"] = event.ShoppingCart.ProductList;
383
- }
384
- trackEvent(event.EventName, null, attributes);
385
- });
386
- }
387
- } else {
388
- trackEvent(event.EventName, null, attributes);
389
- }
390
-
391
- return true;
392
- }
393
- catch (e) {
394
- return {error: e};
395
- }
396
- }
397
330
 
398
- return reportEvent;
399
- }
331
+ attributes['ProductAttributes'] = productAttributes;
400
332
 
401
- /**
402
- * Log regular Taplytics events
403
- * @param {*} event
404
- */
405
- function logEvent(event) {
406
- try {
407
- trackEvent(event.EventName, null, event.EventAttributes);
333
+ Taplytics.track(
334
+ event.EventName,
335
+ parseFloat(product.TotalAmount),
336
+ attributes
337
+ );
338
+ });
408
339
  return true;
409
- }
410
- catch (e) {
340
+ } catch (e) {
411
341
  return { error: e };
412
342
  }
413
343
  }
414
344
 
415
- /**
416
- * Identifies user id or email with Taplytics
417
- * @param id
418
- * @param type
419
- * @returns {string}
420
- */
421
- function setUserIdentity(id, type) {
422
- if (isInitialized) {
423
- try {
424
- // Some integrations have primary ids that they use
425
- // (ie. CustomerId, or Email), you may have special methods
426
- // to call for these. mParticle allows for several other
427
- // types of userIds to be set. To view all user identity
428
- // types, navigate to - https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101
429
- if (type === window.mParticle.IdentityType.CustomerId) {
430
- Taplytics.identify({
431
- user_id: id
345
+ return reportEvent;
346
+ }
347
+
348
+ /**
349
+ * Logs all other commerce events as regular events with metadata to Taplytics
350
+ * @param event
351
+ * @returns {*}
352
+ */
353
+ function logCommerceEvent(event) {
354
+ var reportEvent = false;
355
+
356
+ var attributes = {};
357
+
358
+ if (event) {
359
+ try {
360
+ if (event.EventAttributes) {
361
+ attributes['EventAttributes'] = event.EventAttributes;
362
+ }
363
+
364
+ if (event.CurrencyCode) {
365
+ attributes['CurrencyCode'] = event.CurrencyCode;
366
+ }
367
+
368
+ if (
369
+ event.EventCategory ===
370
+ mParticle.CommerceEventType.PromotionClick
371
+ ) {
372
+ var promotionList = event.PromotionAction.PromotionList;
373
+ if (promotionList) {
374
+ promotionList.forEach(function(promotion) {
375
+ attributes['Promotion'] = promotion;
376
+ trackEvent(event.EventName, null, attributes);
432
377
  });
433
378
  }
434
- else if (type === window.mParticle.IdentityType.Email) {
435
- Taplytics.identify({
436
- email: id
379
+ } else if (
380
+ event.EventCategory ===
381
+ mParticle.CommerceEventType.ProductImpression
382
+ ) {
383
+ var impressions = event.ProductImpressions;
384
+ if (impressions) {
385
+ impressions.forEach(function(impression) {
386
+ var productList = impression.ProductList;
387
+ var impressionName =
388
+ impression.ProductImpressionList;
389
+ if (productList) {
390
+ productList.forEach(function(product) {
391
+ var productAttributes = product;
392
+ var attributeCopy = clone(attributes);
393
+ if (product.Attributes) {
394
+ productAttributes = mergeObjects(
395
+ product,
396
+ product.Attributes
397
+ );
398
+ }
399
+
400
+ attributeCopy[
401
+ 'ProductImpression'
402
+ ] = impressionName;
403
+ attributeCopy[
404
+ 'ProductImpressionProduct'
405
+ ] = productAttributes;
406
+ trackEvent(
407
+ event.EventName,
408
+ null,
409
+ attributeCopy
410
+ );
411
+ });
412
+ }
437
413
  });
438
414
  }
415
+ } else if (
416
+ event.EventCategory ===
417
+ mParticle.CommerceEventType.ProductAddToCart ||
418
+ event.EventCategory ===
419
+ mParticle.CommerceEventType.ProductRemoveFromCart ||
420
+ event.ProductAction
421
+ ) {
422
+ if (event.ProductAction.ProductList) {
423
+ var productList = event.ProductAction.ProductList;
424
+ productList.forEach(function(product) {
425
+ var productAttributes = product;
426
+ if (product.Attributes) {
427
+ productAttributes = mergeObjects(
428
+ product,
429
+ product.Attributes
430
+ );
431
+ }
432
+ attributes['ProductAttributes'] = productAttributes;
433
+ if (
434
+ event.ShoppingCart &&
435
+ event.ShoppingCart.ProductList
436
+ ) {
437
+ attributes['ShoppingCart'] =
438
+ event.ShoppingCart.ProductList;
439
+ }
440
+ trackEvent(event.EventName, null, attributes);
441
+ });
442
+ }
443
+ } else {
444
+ trackEvent(event.EventName, null, attributes);
439
445
  }
440
- catch (e) {
441
- return 'Failed to call setUserIdentity on ' + name + ' ' + e;
442
- }
443
- }
444
- else {
445
- return 'Can\'t call setUserIdentity on forwarder ' + name + ', not initialized';
446
+
447
+ return true;
448
+ } catch (e) {
449
+ return { error: e };
446
450
  }
447
451
  }
448
452
 
449
- /**
450
- * Sets user attributes to Taplytics
451
- * @param key
452
- * @param value
453
- * @returns {string}
454
- */
455
- function setUserAttribute(key, value) {
456
- if (isInitialized) {
457
- try {
458
- var attributes = {};
459
- attributes[key] = value;
460
- Taplytics.identify(attributes);
461
- return 'Successfully called setUserAttribute API on ' + name;
462
- }
463
- catch (e) {
464
- return 'Failed to call SET setUserAttribute on ' + name + ' ' + e;
453
+ return reportEvent;
454
+ }
455
+
456
+ /**
457
+ * Log regular Taplytics events
458
+ * @param {*} event
459
+ */
460
+ function logEvent(event) {
461
+ try {
462
+ trackEvent(event.EventName, null, event.EventAttributes);
463
+ return true;
464
+ } catch (e) {
465
+ return { error: e };
466
+ }
467
+ }
468
+
469
+ /**
470
+ * Identifies user id or email with Taplytics
471
+ * @param id
472
+ * @param type
473
+ * @returns {string}
474
+ */
475
+ function setUserIdentity(id, type) {
476
+ if (isInitialized) {
477
+ try {
478
+ // Some integrations have primary ids that they use
479
+ // (ie. CustomerId, or Email), you may have special methods
480
+ // to call for these. mParticle allows for several other
481
+ // types of userIds to be set. To view all user identity
482
+ // types, navigate to - https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101
483
+ if (type === window.mParticle.IdentityType.CustomerId) {
484
+ Taplytics.identify({
485
+ user_id: id,
486
+ });
487
+ } else if (type === window.mParticle.IdentityType.Email) {
488
+ Taplytics.identify({
489
+ email: id,
490
+ });
465
491
  }
492
+ } catch (e) {
493
+ return 'Failed to call setUserIdentity on ' + name + ' ' + e;
466
494
  }
467
- else {
468
- return 'Can\'t call setUserAttribute on forwarder ' + name + ', not initialized';
469
- }
495
+ } else {
496
+ return (
497
+ "Can't call setUserIdentity on forwarder " +
498
+ name +
499
+ ', not initialized'
500
+ );
470
501
  }
502
+ }
471
503
 
472
- /**
473
- * Remove user attribute by setting it to null
474
- * @param key
475
- */
476
- function removeUserAttribute(key) {
477
- setUserAttribute(key, null);
504
+ /**
505
+ * Sets user attributes to Taplytics
506
+ * @param key
507
+ * @param value
508
+ * @returns {string}
509
+ */
510
+ function setUserAttribute(key, value) {
511
+ if (isInitialized) {
512
+ try {
513
+ var attributes = {};
514
+ attributes[key] = value;
515
+ Taplytics.identify(attributes);
516
+ return 'Successfully called setUserAttribute API on ' + name;
517
+ } catch (e) {
518
+ return (
519
+ 'Failed to call SET setUserAttribute on ' + name + ' ' + e
520
+ );
521
+ }
522
+ } else {
523
+ return (
524
+ "Can't call setUserAttribute on forwarder " +
525
+ name +
526
+ ', not initialized'
527
+ );
478
528
  }
529
+ }
479
530
 
480
- /**
481
- * V2 version of setting user identity
482
- * @param user
483
- * @returns {string}
484
- */
485
- function onUserIdentified(user) {
486
- if (isInitialized) {
487
- var identities = user.getUserIdentities().userIdentities;
488
- var attributes = {};
489
- if (identities.customerid) {
490
- attributes.user_id = identities.customerid;
491
- }
492
- if (identities.email) {
493
- attributes.email = identities.email;
494
- }
495
- if (!isEmpty(attributes)) {
496
- Taplytics.identify(attributes);
497
- }
531
+ /**
532
+ * Remove user attribute by setting it to null
533
+ * @param key
534
+ */
535
+ function removeUserAttribute(key) {
536
+ setUserAttribute(key, null);
537
+ }
538
+
539
+ /**
540
+ * V2 version of setting user identity
541
+ * @param user
542
+ * @returns {string}
543
+ */
544
+ function onUserIdentified(user) {
545
+ if (isInitialized) {
546
+ var identities = user.getUserIdentities().userIdentities;
547
+ var attributes = {};
548
+ if (identities.customerid) {
549
+ attributes.user_id = identities.customerid;
498
550
  }
499
- else {
500
- return 'Can\'t call onUserIdentified on forwarder ' + name + ', not initialized';
551
+ if (identities.email) {
552
+ attributes.email = identities.email;
501
553
  }
554
+ if (!isEmpty(attributes)) {
555
+ Taplytics.identify(attributes);
556
+ }
557
+ } else {
558
+ return (
559
+ "Can't call onUserIdentified on forwarder " +
560
+ name +
561
+ ', not initialized'
562
+ );
502
563
  }
564
+ }
503
565
 
504
- this.init = initForwarder;
505
- this.process = processEvent;
506
- this.setUserIdentity = setUserIdentity;
507
- this.setUserAttribute = setUserAttribute;
508
- this.removeUserAttribute = removeUserAttribute;
509
- this.onUserIdentified = onUserIdentified;
510
- };
566
+ this.init = initForwarder;
567
+ this.process = processEvent;
568
+ this.setUserIdentity = setUserIdentity;
569
+ this.setUserAttribute = setUserAttribute;
570
+ this.removeUserAttribute = removeUserAttribute;
571
+ this.onUserIdentified = onUserIdentified;
572
+ };
511
573
 
512
- function getId() {
513
- return moduleId;
514
- }
574
+ function getId() {
575
+ return moduleId;
576
+ }
515
577
 
516
- function register(config) {
517
- if (!config) {
518
- window.console.log('You must pass a config object to register the kit ' + name);
519
- return;
520
- }
578
+ function register(config) {
579
+ if (!config) {
580
+ console.log(
581
+ 'You must pass a config object to register the kit ' + name
582
+ );
583
+ return;
584
+ }
521
585
 
522
- if (!isObject(config)) {
523
- window.console.log('\'config\' must be an object. You passed in a ' + typeof config);
524
- return;
525
- }
586
+ if (!isObject(config)) {
587
+ console.log(
588
+ "'config' must be an object. You passed in a " + typeof config
589
+ );
590
+ return;
591
+ }
526
592
 
527
- if (isObject(config.kits)) {
528
- config.kits[name] = {
529
- constructor: constructor
530
- };
531
- } else {
532
- config.kits = {};
533
- config.kits[name] = {
534
- constructor: constructor
535
- };
536
- }
537
- window.console.log('Successfully registered ' + name + ' to your mParticle configuration');
593
+ if (isObject(config.kits)) {
594
+ config.kits[name] = {
595
+ constructor: constructor,
596
+ };
597
+ } else {
598
+ config.kits = {};
599
+ config.kits[name] = {
600
+ constructor: constructor,
601
+ };
538
602
  }
603
+ console.log(
604
+ 'Successfully registered ' + name + ' to your mParticle configuration'
605
+ );
606
+ }
539
607
 
608
+ if (typeof window !== 'undefined') {
540
609
  if (window && window.mParticle && window.mParticle.addForwarder) {
541
610
  window.mParticle.addForwarder({
542
611
  name: name,
543
612
  constructor: constructor,
544
- getId: getId
613
+ getId: getId,
545
614
  });
546
615
  }
547
- var TaplyticsKit = {
548
- register: register
549
- };
616
+ }
617
+
618
+ var TaplyticsKit = {
619
+ register: register,
620
+ };
550
621
  var TaplyticsKit_1 = TaplyticsKit.register;
551
622
 
552
623
  exports.default = TaplyticsKit;
package/package.json CHANGED
@@ -1,28 +1,41 @@
1
1
  {
2
- "name": "@mparticle/web-taplytics-kit",
3
- "version": "2.0.1",
4
- "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
- "description": "mParticle integration sdk for Taplytics",
6
- "main": "dist/TaplyticsKit.common.js",
7
- "browser": "dist/TaplyticsKit.common.js",
8
- "files": [
9
- "dist/TaplyticsKit.common.js"
10
- ],
11
- "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-taplytics",
12
- "scripts": {
13
- "build": "rollup --config rollup.config.js",
14
- "watch": "rollup --config rollup.config.js -w"
15
- },
16
- "devDependencies": {
17
- "should": "^7.1.0",
18
- "mocha": "^2.3.2",
19
- "rollup": "^1.13.1",
20
- "rollup-plugin-commonjs": "^10.0.0",
21
- "rollup-plugin-node-resolve": "^5.0.1"
22
- },
23
- "dependencies": {
24
- "isobject": "^4.0.0",
25
- "@mparticle/web-sdk": "^2.11.1"
26
- },
27
- "license": "Apache-2.0"
2
+ "name": "@mparticle/web-taplytics-kit",
3
+ "version": "3.1.0",
4
+ "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
+ "description": "mParticle integration sdk for Taplytics",
6
+ "main": "dist/TaplyticsKit.common.js",
7
+ "browser": "dist/TaplyticsKit.common.js",
8
+ "files": [
9
+ "dist/TaplyticsKit.common.js"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/mParticle/mparticle-web-sdk",
14
+ "directory": "kits/taplytics"
15
+ },
16
+ "scripts": {
17
+ "build": "rollup --config rollup.config.js",
18
+ "watch": "rollup --config rollup.config.js -w",
19
+ "test": "node test/boilerplate/test-karma.js"
20
+ },
21
+ "devDependencies": {
22
+ "chai": "^4.2.0",
23
+ "karma": "^5.1.0",
24
+ "karma-chai": "^0.1.0",
25
+ "karma-chrome-launcher": "^3.1.0",
26
+ "karma-firefox-launcher": "^1.3.0",
27
+ "karma-mocha": "^2.0.1",
28
+ "mocha": "^2.5.3",
29
+ "rollup": "^1.13.1",
30
+ "rollup-plugin-commonjs": "^10.0.0",
31
+ "rollup-plugin-node-resolve": "^5.0.1",
32
+ "shelljs": "^0.8.4",
33
+ "should": "^7.1.0",
34
+ "watchify": "^3.11.1"
35
+ },
36
+ "dependencies": {
37
+ "isobject": "^4.0.0",
38
+ "@mparticle/web-sdk": "^3.0.0"
39
+ },
40
+ "license": "Apache-2.0"
28
41
  }