@mparticle/web-sdk 2.14.1 → 2.14.2

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.
@@ -143,9 +143,13 @@ export default function mParticleInstance(instanceName) {
143
143
  * @method reset
144
144
  */
145
145
  this.reset = function(instance) {
146
- instance._Persistence.resetPersistence();
147
- if (instance._Store) {
148
- delete instance._Store;
146
+ try {
147
+ instance._Persistence.resetPersistence();
148
+ if (instance._Store) {
149
+ delete instance._Store;
150
+ }
151
+ } catch (error) {
152
+ console.error('Cannot reset mParticle', error);
149
153
  }
150
154
  };
151
155
 
@@ -177,7 +181,7 @@ export default function mParticleInstance(instanceName) {
177
181
  * @param {Function} function A function to be called after mParticle is initialized
178
182
  */
179
183
  this.ready = function(f) {
180
- if (self._Store.isInitialized && typeof f === 'function') {
184
+ if (self.isInitialized() && typeof f === 'function') {
181
185
  f();
182
186
  } else {
183
187
  self._preInit.readyQueue.push(f);
@@ -197,6 +201,12 @@ export default function mParticleInstance(instanceName) {
197
201
  * @param {String} version version number
198
202
  */
199
203
  this.setAppVersion = function(version) {
204
+ var queued = queueIfNotInitialized(function() {
205
+ self.setAppVersion(version);
206
+ }, self);
207
+
208
+ if (queued) return;
209
+
200
210
  self._Store.SDKConfig.appVersion = version;
201
211
  self._Persistence.update();
202
212
  };
@@ -214,6 +224,12 @@ export default function mParticleInstance(instanceName) {
214
224
  * @param {String} name App Name
215
225
  */
216
226
  this.setAppName = function(name) {
227
+ var queued = queueIfNotInitialized(function() {
228
+ self.setAppName(name);
229
+ }, self);
230
+
231
+ if (queued) return;
232
+
217
233
  self._Store.SDKConfig.appName = name;
218
234
  };
219
235
  /**
@@ -254,6 +270,12 @@ export default function mParticleInstance(instanceName) {
254
270
  * @param {Number} longitude longitude digit
255
271
  */
256
272
  this.setPosition = function(lat, lng) {
273
+ var queued = queueIfNotInitialized(function() {
274
+ self.setPosition(lat, lng);
275
+ }, self);
276
+
277
+ if (queued) return;
278
+
257
279
  self._SessionManager.resetSessionTimer();
258
280
  if (typeof lat === 'number' && typeof lng === 'number') {
259
281
  self._Store.currentPosition = {
@@ -288,12 +310,12 @@ export default function mParticleInstance(instanceName) {
288
310
  * @param {Object} [eventOptions] For Event-level Configuration Options
289
311
  */
290
312
  this.logBaseEvent = function(event, eventOptions) {
291
- if (!self._Store.isInitialized) {
292
- self.ready(function() {
293
- self.logBaseEvent(event, eventOptions);
294
- });
295
- return;
296
- }
313
+ var queued = queueIfNotInitialized(function() {
314
+ self.logBaseEvent(event, eventOptions);
315
+ }, self);
316
+
317
+ if (queued) return;
318
+
297
319
  self._SessionManager.resetSessionTimer();
298
320
  if (typeof event.name !== 'string') {
299
321
  self.Logger.error(Messages.ErrorMessages.EventNameInvalidType);
@@ -327,18 +349,18 @@ export default function mParticleInstance(instanceName) {
327
349
  customFlags,
328
350
  eventOptions
329
351
  ) {
330
- if (!self._Store.isInitialized) {
331
- self.ready(function() {
332
- self.logEvent(
333
- eventName,
334
- eventType,
335
- eventInfo,
336
- customFlags,
337
- eventOptions
338
- );
339
- });
340
- return;
341
- }
352
+ var queued = queueIfNotInitialized(function() {
353
+ self.logEvent(
354
+ eventName,
355
+ eventType,
356
+ eventInfo,
357
+ customFlags,
358
+ eventOptions
359
+ );
360
+ }, self);
361
+
362
+ if (queued) return;
363
+
342
364
  self._SessionManager.resetSessionTimer();
343
365
  if (typeof eventName !== 'string') {
344
366
  self.Logger.error(Messages.ErrorMessages.EventNameInvalidType);
@@ -383,12 +405,12 @@ export default function mParticleInstance(instanceName) {
383
405
  * @param {Object} [attrs] Custom attrs to be passed along with the error event; values must be string, number, or boolean
384
406
  */
385
407
  this.logError = function(error, attrs) {
386
- if (!self._Store.isInitialized) {
387
- self.ready(function() {
388
- self.logError(error, attrs);
389
- });
390
- return;
391
- }
408
+ var queued = queueIfNotInitialized(function() {
409
+ self.logError(error, attrs);
410
+ }, self);
411
+
412
+ if (queued) return;
413
+
392
414
  self._SessionManager.resetSessionTimer();
393
415
  if (!error) {
394
416
  return;
@@ -463,12 +485,12 @@ export default function mParticleInstance(instanceName) {
463
485
  * @param {Object} [eventOptions] For Event-level Configuration Options
464
486
  */
465
487
  this.logPageView = function(eventName, attrs, customFlags, eventOptions) {
466
- if (!self._Store.isInitialized) {
467
- self.ready(function() {
468
- self.logPageView(eventName, attrs, customFlags, eventOptions);
469
- });
470
- return;
471
- }
488
+ var queued = queueIfNotInitialized(function() {
489
+ self.logPageView(eventName, attrs, customFlags, eventOptions);
490
+ }, self);
491
+
492
+ if (queued) return;
493
+
472
494
  self._SessionManager.resetSessionTimer();
473
495
 
474
496
  if (self._Helpers.canLog()) {
@@ -640,12 +662,11 @@ export default function mParticleInstance(instanceName) {
640
662
  * @param {String} code The currency code
641
663
  */
642
664
  setCurrencyCode: function(code) {
643
- if (!self._Store.isInitialized) {
644
- self.ready(function() {
645
- self.eCommerce.setCurrencyCode(code);
646
- });
647
- return;
648
- }
665
+ var queued = queueIfNotInitialized(function() {
666
+ self.eCommerce.setCurrencyCode(code);
667
+ }, self);
668
+
669
+ if (queued) return;
649
670
 
650
671
  if (typeof code !== 'string') {
651
672
  self.Logger.error('Code must be a string');
@@ -763,6 +784,7 @@ export default function mParticleInstance(instanceName) {
763
784
  self.Logger.warning(
764
785
  'mParticle.logCheckout is deprecated, please use mParticle.logProductAction instead'
765
786
  );
787
+
766
788
  if (!self._Store.isInitialized) {
767
789
  self.ready(function() {
768
790
  self.eCommerce.logCheckout(
@@ -772,8 +794,10 @@ export default function mParticleInstance(instanceName) {
772
794
  customFlags
773
795
  );
774
796
  });
797
+
775
798
  return;
776
799
  }
800
+
777
801
  self._SessionManager.resetSessionTimer();
778
802
  self._Events.logCheckoutEvent(step, option, attrs, customFlags);
779
803
  },
@@ -796,19 +820,19 @@ export default function mParticleInstance(instanceName) {
796
820
  transactionAttributes,
797
821
  eventOptions
798
822
  ) {
799
- if (!self._Store.isInitialized) {
800
- self.ready(function() {
801
- self.eCommerce.logProductAction(
802
- productActionType,
803
- product,
804
- attrs,
805
- customFlags,
806
- transactionAttributes,
807
- eventOptions
808
- );
809
- });
810
- return;
811
- }
823
+ var queued = queueIfNotInitialized(function() {
824
+ self.eCommerce.logProductAction(
825
+ productActionType,
826
+ product,
827
+ attrs,
828
+ customFlags,
829
+ transactionAttributes,
830
+ eventOptions
831
+ );
832
+ }, self);
833
+
834
+ if (queued) return;
835
+
812
836
  self._SessionManager.resetSessionTimer();
813
837
  self._Events.logProductActionEvent(
814
838
  productActionType,
@@ -881,18 +905,18 @@ export default function mParticleInstance(instanceName) {
881
905
  customFlags,
882
906
  eventOptions
883
907
  ) {
884
- if (!self._Store.isInitialized) {
885
- self.ready(function() {
886
- self.eCommerce.logPromotion(
887
- type,
888
- promotion,
889
- attrs,
890
- customFlags,
891
- eventOptions
892
- );
893
- });
894
- return;
895
- }
908
+ var queued = queueIfNotInitialized(function() {
909
+ self.eCommerce.logPromotion(
910
+ type,
911
+ promotion,
912
+ attrs,
913
+ customFlags,
914
+ eventOptions
915
+ );
916
+ }, self);
917
+
918
+ if (queued) return;
919
+
896
920
  self._SessionManager.resetSessionTimer();
897
921
  self._Events.logPromotionEvent(
898
922
  type,
@@ -912,17 +936,17 @@ export default function mParticleInstance(instanceName) {
912
936
  * @param {Object} [eventOptions] For Event-level Configuration Options
913
937
  */
914
938
  logImpression: function(impression, attrs, customFlags, eventOptions) {
915
- if (!self._Store.isInitialized) {
916
- self.ready(function() {
917
- self.eCommerce.logImpression(
918
- impression,
919
- attrs,
920
- customFlags,
921
- eventOptions
922
- );
923
- });
924
- return;
925
- }
939
+ var queued = queueIfNotInitialized(function() {
940
+ self.eCommerce.logImpression(
941
+ impression,
942
+ attrs,
943
+ customFlags,
944
+ eventOptions
945
+ );
946
+ }, self);
947
+
948
+ if (queued) return;
949
+
926
950
  self._SessionManager.resetSessionTimer();
927
951
  self._Events.logImpressionEvent(
928
952
  impression,
@@ -983,12 +1007,12 @@ export default function mParticleInstance(instanceName) {
983
1007
  * @param {String or Number} value value for session attribute
984
1008
  */
985
1009
  this.setSessionAttribute = function(key, value) {
986
- if (!self._Store.isInitialized) {
987
- self.ready(function() {
988
- self.setSessionAttribute(key, value);
989
- });
990
- return;
991
- }
1010
+ var queued = queueIfNotInitialized(function() {
1011
+ self.setSessionAttribute(key, value);
1012
+ }, self);
1013
+
1014
+ if (queued) return;
1015
+
992
1016
  // Logs to cookie
993
1017
  // And logs to in-memory object
994
1018
  // Example: mParticle.setSessionAttribute('location', '33431');
@@ -1034,12 +1058,12 @@ export default function mParticleInstance(instanceName) {
1034
1058
  * @param {Boolean} isOptingOut boolean to opt out or not. When set to true, opt out of logging.
1035
1059
  */
1036
1060
  this.setOptOut = function(isOptingOut) {
1037
- if (!self._Store.isInitialized) {
1038
- self.ready(function() {
1039
- self.setOptOut(isOptingOut);
1040
- });
1041
- return;
1042
- }
1061
+ var queued = queueIfNotInitialized(function() {
1062
+ self.setOptOut(isOptingOut);
1063
+ }, self);
1064
+
1065
+ if (queued) return;
1066
+
1043
1067
  self._SessionManager.resetSessionTimer();
1044
1068
  self._Store.isEnabled = !isOptingOut;
1045
1069
 
@@ -1072,12 +1096,11 @@ export default function mParticleInstance(instanceName) {
1072
1096
  * also pass a null or empty map here to remove all of the attributes.
1073
1097
  */
1074
1098
  this.setIntegrationAttribute = function(integrationId, attrs) {
1075
- if (!self._Store.isInitialized) {
1076
- self.ready(function() {
1077
- self.setIntegrationAttribute(integrationId, attrs);
1078
- });
1079
- return;
1080
- }
1099
+ var queued = queueIfNotInitialized(function() {
1100
+ self.setIntegrationAttribute(integrationId, attrs);
1101
+ }, self);
1102
+
1103
+ if (queued) return;
1081
1104
 
1082
1105
  if (typeof integrationId !== 'number') {
1083
1106
  self.Logger.error('integrationId must be a number');
@@ -1163,6 +1186,9 @@ export default function mParticleInstance(instanceName) {
1163
1186
  this._setIntegrationDelay = function(module, boolean) {
1164
1187
  self._preInit.integrationDelays[module] = boolean;
1165
1188
  };
1189
+ this.isInitialized = function() {
1190
+ return self._Store ? self._Store.isInitialized : false;
1191
+ };
1166
1192
  }
1167
1193
 
1168
1194
  // Some (server) config settings need to be returned before they are set on SDKConfig in a self hosted environment
@@ -1464,3 +1490,13 @@ function processPreloadedItem(readyQueueItem, mpInstance) {
1464
1490
  }
1465
1491
  }
1466
1492
  }
1493
+
1494
+ function queueIfNotInitialized(func, self) {
1495
+ if (!self.isInitialized()) {
1496
+ self.ready(function() {
1497
+ func();
1498
+ });
1499
+ return true;
1500
+ }
1501
+ return false;
1502
+ }