@mparticle/web-sdk 2.14.0 → 2.15.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.
@@ -87,6 +87,7 @@ export default function mParticleInstance(instanceName) {
87
87
  this.Identity = this._Identity.IdentityAPI;
88
88
  this.generateHash = this._Helpers.generateHash;
89
89
  this.getDeviceId = this._Persistence.getDeviceId;
90
+
90
91
  if (typeof window !== 'undefined') {
91
92
  if (window.mParticle && window.mParticle.config) {
92
93
  if (window.mParticle.config.hasOwnProperty('rq')) {
@@ -143,9 +144,13 @@ export default function mParticleInstance(instanceName) {
143
144
  * @method reset
144
145
  */
145
146
  this.reset = function(instance) {
146
- instance._Persistence.resetPersistence();
147
- if (instance._Store) {
148
- delete instance._Store;
147
+ try {
148
+ instance._Persistence.resetPersistence();
149
+ if (instance._Store) {
150
+ delete instance._Store;
151
+ }
152
+ } catch (error) {
153
+ console.error('Cannot reset mParticle', error);
149
154
  }
150
155
  };
151
156
 
@@ -177,7 +182,7 @@ export default function mParticleInstance(instanceName) {
177
182
  * @param {Function} function A function to be called after mParticle is initialized
178
183
  */
179
184
  this.ready = function(f) {
180
- if (self._Store.isInitialized && typeof f === 'function') {
185
+ if (self.isInitialized() && typeof f === 'function') {
181
186
  f();
182
187
  } else {
183
188
  self._preInit.readyQueue.push(f);
@@ -197,9 +202,36 @@ export default function mParticleInstance(instanceName) {
197
202
  * @param {String} version version number
198
203
  */
199
204
  this.setAppVersion = function(version) {
205
+ var queued = queueIfNotInitialized(function() {
206
+ self.setAppVersion(version);
207
+ }, self);
208
+
209
+ if (queued) return;
210
+
200
211
  self._Store.SDKConfig.appVersion = version;
201
212
  self._Persistence.update();
202
213
  };
214
+ /**
215
+ * Sets the device id
216
+ * @method setDeviceId
217
+ * @param {String} name device ID (UUIDv4-formatted string)
218
+ */
219
+ this.setDeviceId = function(guid) {
220
+ var queued = queueIfNotInitialized(function() {
221
+ self.setDeviceId(guid);
222
+ }, self);
223
+ if (queued) return;
224
+ this._Persistence.setDeviceId(guid);
225
+ };
226
+ /**
227
+ * Returns a boolean for whether or not the SDKhas been fully initialized
228
+ * @method isInitialized
229
+ * @return {Boolean} a boolean for whether or not the SDK has been fully initialized
230
+ */
231
+ this.isInitialized = function() {
232
+ return self._Store ? self._Store.isInitialized : false;
233
+ };
234
+
203
235
  /**
204
236
  * Gets the app name
205
237
  * @method getAppName
@@ -214,6 +246,12 @@ export default function mParticleInstance(instanceName) {
214
246
  * @param {String} name App Name
215
247
  */
216
248
  this.setAppName = function(name) {
249
+ var queued = queueIfNotInitialized(function() {
250
+ self.setAppName(name);
251
+ }, self);
252
+
253
+ if (queued) return;
254
+
217
255
  self._Store.SDKConfig.appName = name;
218
256
  };
219
257
  /**
@@ -254,6 +292,12 @@ export default function mParticleInstance(instanceName) {
254
292
  * @param {Number} longitude longitude digit
255
293
  */
256
294
  this.setPosition = function(lat, lng) {
295
+ var queued = queueIfNotInitialized(function() {
296
+ self.setPosition(lat, lng);
297
+ }, self);
298
+
299
+ if (queued) return;
300
+
257
301
  self._SessionManager.resetSessionTimer();
258
302
  if (typeof lat === 'number' && typeof lng === 'number') {
259
303
  self._Store.currentPosition = {
@@ -288,12 +332,12 @@ export default function mParticleInstance(instanceName) {
288
332
  * @param {Object} [eventOptions] For Event-level Configuration Options
289
333
  */
290
334
  this.logBaseEvent = function(event, eventOptions) {
291
- if (!self._Store.isInitialized) {
292
- self.ready(function() {
293
- self.logBaseEvent(event, eventOptions);
294
- });
295
- return;
296
- }
335
+ var queued = queueIfNotInitialized(function() {
336
+ self.logBaseEvent(event, eventOptions);
337
+ }, self);
338
+
339
+ if (queued) return;
340
+
297
341
  self._SessionManager.resetSessionTimer();
298
342
  if (typeof event.name !== 'string') {
299
343
  self.Logger.error(Messages.ErrorMessages.EventNameInvalidType);
@@ -327,18 +371,18 @@ export default function mParticleInstance(instanceName) {
327
371
  customFlags,
328
372
  eventOptions
329
373
  ) {
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
- }
374
+ var queued = queueIfNotInitialized(function() {
375
+ self.logEvent(
376
+ eventName,
377
+ eventType,
378
+ eventInfo,
379
+ customFlags,
380
+ eventOptions
381
+ );
382
+ }, self);
383
+
384
+ if (queued) return;
385
+
342
386
  self._SessionManager.resetSessionTimer();
343
387
  if (typeof eventName !== 'string') {
344
388
  self.Logger.error(Messages.ErrorMessages.EventNameInvalidType);
@@ -383,12 +427,12 @@ export default function mParticleInstance(instanceName) {
383
427
  * @param {Object} [attrs] Custom attrs to be passed along with the error event; values must be string, number, or boolean
384
428
  */
385
429
  this.logError = function(error, attrs) {
386
- if (!self._Store.isInitialized) {
387
- self.ready(function() {
388
- self.logError(error, attrs);
389
- });
390
- return;
391
- }
430
+ var queued = queueIfNotInitialized(function() {
431
+ self.logError(error, attrs);
432
+ }, self);
433
+
434
+ if (queued) return;
435
+
392
436
  self._SessionManager.resetSessionTimer();
393
437
  if (!error) {
394
438
  return;
@@ -463,12 +507,12 @@ export default function mParticleInstance(instanceName) {
463
507
  * @param {Object} [eventOptions] For Event-level Configuration Options
464
508
  */
465
509
  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
- }
510
+ var queued = queueIfNotInitialized(function() {
511
+ self.logPageView(eventName, attrs, customFlags, eventOptions);
512
+ }, self);
513
+
514
+ if (queued) return;
515
+
472
516
  self._SessionManager.resetSessionTimer();
473
517
 
474
518
  if (self._Helpers.canLog()) {
@@ -640,12 +684,11 @@ export default function mParticleInstance(instanceName) {
640
684
  * @param {String} code The currency code
641
685
  */
642
686
  setCurrencyCode: function(code) {
643
- if (!self._Store.isInitialized) {
644
- self.ready(function() {
645
- self.eCommerce.setCurrencyCode(code);
646
- });
647
- return;
648
- }
687
+ var queued = queueIfNotInitialized(function() {
688
+ self.eCommerce.setCurrencyCode(code);
689
+ }, self);
690
+
691
+ if (queued) return;
649
692
 
650
693
  if (typeof code !== 'string') {
651
694
  self.Logger.error('Code must be a string');
@@ -763,6 +806,7 @@ export default function mParticleInstance(instanceName) {
763
806
  self.Logger.warning(
764
807
  'mParticle.logCheckout is deprecated, please use mParticle.logProductAction instead'
765
808
  );
809
+
766
810
  if (!self._Store.isInitialized) {
767
811
  self.ready(function() {
768
812
  self.eCommerce.logCheckout(
@@ -772,8 +816,10 @@ export default function mParticleInstance(instanceName) {
772
816
  customFlags
773
817
  );
774
818
  });
819
+
775
820
  return;
776
821
  }
822
+
777
823
  self._SessionManager.resetSessionTimer();
778
824
  self._Events.logCheckoutEvent(step, option, attrs, customFlags);
779
825
  },
@@ -796,19 +842,19 @@ export default function mParticleInstance(instanceName) {
796
842
  transactionAttributes,
797
843
  eventOptions
798
844
  ) {
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
- }
845
+ var queued = queueIfNotInitialized(function() {
846
+ self.eCommerce.logProductAction(
847
+ productActionType,
848
+ product,
849
+ attrs,
850
+ customFlags,
851
+ transactionAttributes,
852
+ eventOptions
853
+ );
854
+ }, self);
855
+
856
+ if (queued) return;
857
+
812
858
  self._SessionManager.resetSessionTimer();
813
859
  self._Events.logProductActionEvent(
814
860
  productActionType,
@@ -881,18 +927,18 @@ export default function mParticleInstance(instanceName) {
881
927
  customFlags,
882
928
  eventOptions
883
929
  ) {
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
- }
930
+ var queued = queueIfNotInitialized(function() {
931
+ self.eCommerce.logPromotion(
932
+ type,
933
+ promotion,
934
+ attrs,
935
+ customFlags,
936
+ eventOptions
937
+ );
938
+ }, self);
939
+
940
+ if (queued) return;
941
+
896
942
  self._SessionManager.resetSessionTimer();
897
943
  self._Events.logPromotionEvent(
898
944
  type,
@@ -912,17 +958,17 @@ export default function mParticleInstance(instanceName) {
912
958
  * @param {Object} [eventOptions] For Event-level Configuration Options
913
959
  */
914
960
  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
- }
961
+ var queued = queueIfNotInitialized(function() {
962
+ self.eCommerce.logImpression(
963
+ impression,
964
+ attrs,
965
+ customFlags,
966
+ eventOptions
967
+ );
968
+ }, self);
969
+
970
+ if (queued) return;
971
+
926
972
  self._SessionManager.resetSessionTimer();
927
973
  self._Events.logImpressionEvent(
928
974
  impression,
@@ -983,12 +1029,12 @@ export default function mParticleInstance(instanceName) {
983
1029
  * @param {String or Number} value value for session attribute
984
1030
  */
985
1031
  this.setSessionAttribute = function(key, value) {
986
- if (!self._Store.isInitialized) {
987
- self.ready(function() {
988
- self.setSessionAttribute(key, value);
989
- });
990
- return;
991
- }
1032
+ var queued = queueIfNotInitialized(function() {
1033
+ self.setSessionAttribute(key, value);
1034
+ }, self);
1035
+
1036
+ if (queued) return;
1037
+
992
1038
  // Logs to cookie
993
1039
  // And logs to in-memory object
994
1040
  // Example: mParticle.setSessionAttribute('location', '33431');
@@ -1034,12 +1080,12 @@ export default function mParticleInstance(instanceName) {
1034
1080
  * @param {Boolean} isOptingOut boolean to opt out or not. When set to true, opt out of logging.
1035
1081
  */
1036
1082
  this.setOptOut = function(isOptingOut) {
1037
- if (!self._Store.isInitialized) {
1038
- self.ready(function() {
1039
- self.setOptOut(isOptingOut);
1040
- });
1041
- return;
1042
- }
1083
+ var queued = queueIfNotInitialized(function() {
1084
+ self.setOptOut(isOptingOut);
1085
+ }, self);
1086
+
1087
+ if (queued) return;
1088
+
1043
1089
  self._SessionManager.resetSessionTimer();
1044
1090
  self._Store.isEnabled = !isOptingOut;
1045
1091
 
@@ -1072,12 +1118,11 @@ export default function mParticleInstance(instanceName) {
1072
1118
  * also pass a null or empty map here to remove all of the attributes.
1073
1119
  */
1074
1120
  this.setIntegrationAttribute = function(integrationId, attrs) {
1075
- if (!self._Store.isInitialized) {
1076
- self.ready(function() {
1077
- self.setIntegrationAttribute(integrationId, attrs);
1078
- });
1079
- return;
1080
- }
1121
+ var queued = queueIfNotInitialized(function() {
1122
+ self.setIntegrationAttribute(integrationId, attrs);
1123
+ }, self);
1124
+
1125
+ if (queued) return;
1081
1126
 
1082
1127
  if (typeof integrationId !== 'number') {
1083
1128
  self.Logger.error('integrationId must be a number');
@@ -1464,3 +1509,13 @@ function processPreloadedItem(readyQueueItem, mpInstance) {
1464
1509
  }
1465
1510
  }
1466
1511
  }
1512
+
1513
+ function queueIfNotInitialized(func, self) {
1514
+ if (!self.isInitialized()) {
1515
+ self.ready(function() {
1516
+ func();
1517
+ });
1518
+ return true;
1519
+ }
1520
+ return false;
1521
+ }
@@ -97,6 +97,12 @@ function mParticle() {
97
97
  this.getDeviceId = function() {
98
98
  return self.getInstance().getDeviceId();
99
99
  };
100
+ this.setDeviceId = function(guid) {
101
+ return self.getInstance().setDeviceId(guid);
102
+ };
103
+ this.isInitialized = function() {
104
+ return self.getInstance().isInitialized();
105
+ };
100
106
  this.startNewSession = function() {
101
107
  self.getInstance().startNewSession();
102
108
  };
@@ -1148,6 +1148,11 @@ export default function _Persistence(mpInstance) {
1148
1148
  return mpInstance._Store.deviceId;
1149
1149
  };
1150
1150
 
1151
+ this.setDeviceId = function(guid) {
1152
+ mpInstance._Store.deviceId = guid;
1153
+ self.update();
1154
+ };
1155
+
1151
1156
  this.resetPersistence = function() {
1152
1157
  removeLocalStorage(StorageNames.localStorageName);
1153
1158
  removeLocalStorage(StorageNames.localStorageNameV3);
@@ -34,6 +34,7 @@ export interface SDKEvent {
34
34
  UserAttributeChanges?: SDKUserAttributeChangeData;
35
35
  CurrencyCode: string;
36
36
  DataPlan?: SDKDataPlan;
37
+ LaunchReferral?: string;
37
38
  }
38
39
 
39
40
  export interface SDKGeoLocation {
@@ -14,7 +14,6 @@ import {
14
14
  import * as EventsApi from '@mparticle/event-models';
15
15
  import Types from './types';
16
16
 
17
-
18
17
  export function convertEvents(
19
18
  mpid: string,
20
19
  sdkEvents: SDKEvent[],
@@ -436,6 +435,7 @@ export function convertAST(
436
435
  .applicationInitialized,
437
436
  is_first_run: sdkEvent.IsFirstRun,
438
437
  is_upgrade: false,
438
+ launch_referral: sdkEvent.LaunchReferral,
439
439
  };
440
440
  astEventData = Object.assign(astEventData, commonEventData);
441
441
  return {
@@ -218,6 +218,7 @@ export default function ServerModel(mpInstance) {
218
218
 
219
219
  if (eventObject.EventDataType === MessageType.AppStateTransition) {
220
220
  eventObject.IsFirstRun = mpInstance._Store.isFirstRun;
221
+ eventObject.LaunchReferral = window.location.href || null;
221
222
  }
222
223
 
223
224
  eventObject.CurrencyCode = mpInstance._Store.currencyCode;
@@ -287,7 +288,7 @@ export default function ServerModel(mpInstance) {
287
288
  dto.fr = event.IsFirstRun;
288
289
  dto.iu = false;
289
290
  dto.at = ApplicationTransitionType.AppInit;
290
- dto.lr = window.location.href || null;
291
+ dto.lr = event.LaunchReferral;
291
292
  dto.attrs = null;
292
293
  }
293
294
 
package/src/store.js CHANGED
@@ -74,6 +74,9 @@ export default function Store(config, mpInstance) {
74
74
  this.SDKConfig = createSDKConfig(config);
75
75
  // Set configuration to default settings
76
76
  if (config) {
77
+ if (config.deviceId) {
78
+ this.deviceId = config.deviceId;
79
+ }
77
80
  if (config.hasOwnProperty('isDevelopmentMode')) {
78
81
  this.SDKConfig.isDevelopmentMode = mpInstance._Helpers.returnConvertedBoolean(
79
82
  config.isDevelopmentMode