@mparticle/web-sdk 2.26.7 → 2.26.9

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.
@@ -892,16 +892,6 @@ export default function _Persistence(mpInstance) {
892
892
  }
893
893
  };
894
894
 
895
- this.getAllUserAttributes = function(mpid) {
896
- var persistence = self.getPersistence();
897
-
898
- if (persistence && persistence[mpid] && persistence[mpid].ua) {
899
- return persistence[mpid].ua;
900
- } else {
901
- return {};
902
- }
903
- };
904
-
905
895
  this.getCartProducts = function(mpid) {
906
896
  var allCartProducts,
907
897
  cartProductsString = localStorage.getItem(
@@ -937,6 +927,7 @@ export default function _Persistence(mpInstance) {
937
927
  );
938
928
  }
939
929
  };
930
+
940
931
  this.saveUserIdentitiesToPersistence = function(mpid, userIdentities) {
941
932
  if (userIdentities) {
942
933
  var persistence = self.getPersistence();
@@ -953,25 +944,6 @@ export default function _Persistence(mpInstance) {
953
944
  }
954
945
  };
955
946
 
956
- this.saveUserAttributesToPersistence = function(mpid, userAttributes) {
957
- var persistence = self.getPersistence();
958
- if (userAttributes) {
959
- if (persistence) {
960
- if (persistence[mpid]) {
961
- // TODO: Investigate why setting this to UI still shows up as UA
962
- // when running `mParticle.getInstance()._Persistence.getLocalStorage()`
963
- // https://go.mparticle.com/work/SQDSDKS-5195
964
- persistence[mpid].ui = userAttributes;
965
- } else {
966
- persistence[mpid] = {
967
- ui: userAttributes,
968
- };
969
- }
970
- }
971
- self.savePersistence(persistence);
972
- }
973
- };
974
-
975
947
  this.saveUserCookieSyncDatesToPersistence = function(mpid, csd) {
976
948
  if (csd) {
977
949
  var persistence = self.getPersistence();
@@ -988,30 +960,6 @@ export default function _Persistence(mpInstance) {
988
960
  }
989
961
  };
990
962
 
991
- this.saveUserConsentStateToCookies = function(mpid, consentState) {
992
- //it's currently not supported to set persistence
993
- //for any MPID that's not the current one.
994
- if (consentState || consentState === null) {
995
- var persistence = self.getPersistence();
996
- if (persistence) {
997
- if (persistence[mpid]) {
998
- persistence[
999
- mpid
1000
- ].con = mpInstance._Consent.ConsentSerialization.toMinifiedJsonObject(
1001
- consentState
1002
- );
1003
- } else {
1004
- persistence[mpid] = {
1005
- con: mpInstance._Consent.ConsentSerialization.toMinifiedJsonObject(
1006
- consentState
1007
- ),
1008
- };
1009
- }
1010
- self.savePersistence(persistence);
1011
- }
1012
- }
1013
- };
1014
-
1015
963
  this.swapCurrentUser = function(
1016
964
  previousMPID,
1017
965
  currentMPID,
@@ -1080,18 +1028,6 @@ export default function _Persistence(mpInstance) {
1080
1028
  return persistence;
1081
1029
  };
1082
1030
 
1083
- this.getConsentState = function(mpid) {
1084
- var persistence = self.getPersistence();
1085
-
1086
- if (persistence && persistence[mpid] && persistence[mpid].con) {
1087
- return mpInstance._Consent.ConsentSerialization.fromMinifiedJsonObject(
1088
- persistence[mpid].con
1089
- );
1090
- } else {
1091
- return null;
1092
- }
1093
- };
1094
-
1095
1031
  this.getFirstSeenTime = function(mpid) {
1096
1032
  if (!mpid) {
1097
1033
  return null;
@@ -23,6 +23,7 @@ import {
23
23
  IMParticleUser,
24
24
  ISDKUserIdentity,
25
25
  IdentityCallback,
26
+ ISDKUserAttributes,
26
27
  } from './identity-user-interfaces';
27
28
  import { IIdentityType } from './types.interfaces';
28
29
 
@@ -34,10 +35,7 @@ export interface SDKEvent {
34
35
  IsFirstRun: boolean;
35
36
  EventName: string;
36
37
  EventCategory: number;
37
-
38
- // https://go.mparticle.com/work/SQDSDKS-5196
39
- UserAttributes?: { [key: string]: string | string[] | null };
40
-
38
+ UserAttributes?: ISDKUserAttributes;
41
39
  UserIdentities?: ISDKUserIdentity[];
42
40
  SourceMessageId: string;
43
41
  MPID: string;
package/src/store.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { Batch } from '@mparticle/event-models';
2
- import { Context } from '@mparticle/event-models';
1
+ import { Batch, Context } from '@mparticle/event-models';
3
2
  import {
4
3
  DataPlanConfig,
5
4
  MPID,
6
5
  IdentifyRequest,
7
6
  SDKEventCustomFlags,
7
+ ConsentState,
8
8
  } from '@mparticle/web-sdk';
9
9
  import { IKitConfigs } from './configAPIClient';
10
10
  import Constants from './constants';
@@ -29,10 +29,13 @@ import {
29
29
  parseNumber,
30
30
  returnConvertedBoolean,
31
31
  } from './utils';
32
- import { SDKConsentState } from './consent';
32
+ import { IMinifiedConsentJSONObject, SDKConsentState } from './consent';
33
33
  import { Kit, MPForwarder } from './forwarders.interfaces';
34
- import { IPersistenceMinified } from './persistence.interfaces';
35
- import { IdentityCallback } from './identity-user-interfaces';
34
+ import { IdentityCallback, UserAttributes } from './identity-user-interfaces';
35
+ import {
36
+ IGlobalStoreV2MinifiedKeys,
37
+ IPersistenceMinified,
38
+ } from './persistence.interfaces';
36
39
 
37
40
  // This represents the runtime configuration of the SDK AFTER
38
41
  // initialization has been complete and all settings and
@@ -180,17 +183,26 @@ export interface IStore {
180
183
 
181
184
  persistenceData?: IPersistenceMinified;
182
185
 
186
+ getConsentState?(mpid: MPID): ConsentState | null;
187
+ setConsentState?(mpid: MPID, consentState: ConsentState): void;
188
+
189
+ _getFromPersistence?<T>(mpid: MPID, key: string): T;
190
+ _setPersistence?<T>(mpid: MPID, key: string, value: T): void;
191
+
183
192
  getDeviceId?(): string;
184
193
  setDeviceId?(deviceId: string): void;
185
194
  getFirstSeenTime?(mpid: MPID): number;
186
195
  setFirstSeenTime?(mpid: MPID, time?: number): void;
187
196
  getLastSeenTime?(mpid: MPID): number;
188
197
  setLastSeenTime?(mpid: MPID, time?: number): void;
198
+ getUserAttributes?(mpid: MPID): UserAttributes;
199
+ setUserAttributes?(mpid: MPID, attributes: UserAttributes): void;
189
200
 
190
201
  addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
191
202
  hasInvalidIdentifyRequest?: () => boolean;
192
203
  nullifySession?: () => void;
193
204
  processConfig(config: SDKInitConfig): void;
205
+ syncPersistenceData?: () => void;
194
206
  }
195
207
 
196
208
  // TODO: Merge this with SDKStoreApi in sdkRuntimeModels
@@ -253,24 +265,8 @@ export default function Store(
253
265
 
254
266
  // Placeholder for in-memory persistence model
255
267
  persistenceData: {
256
- cu: null,
257
- gs: {
258
- sid: null,
259
- ie: null,
260
- sa: null,
261
- ss: null,
262
- dt: null,
263
- av: null,
264
- cgid: null,
265
- das: null,
266
- ia: null,
267
- c: null,
268
- csm: null,
269
- les: null,
270
- ssd: null,
271
- },
272
- l: null,
273
- },
268
+ gs: {} as IGlobalStoreV2MinifiedKeys,
269
+ } as IPersistenceMinified,
274
270
  };
275
271
 
276
272
  for (var key in defaultStore) {
@@ -479,6 +475,53 @@ export default function Store(
479
475
  }
480
476
  }
481
477
 
478
+ this._getFromPersistence = <T>(mpid: MPID, key: string): T | null => {
479
+ if (!mpid) {
480
+ return null;
481
+ }
482
+
483
+ this.syncPersistenceData();
484
+
485
+ if (
486
+ this.persistenceData &&
487
+ this.persistenceData[mpid] &&
488
+ this.persistenceData[mpid][key]
489
+ ) {
490
+ return this.persistenceData[mpid][key] as T;
491
+ } else {
492
+ return null;
493
+ }
494
+ };
495
+
496
+ this._setPersistence = <T>(mpid: MPID, key: string, value: T): void => {
497
+ if (!mpid) {
498
+ return;
499
+ }
500
+
501
+ this.syncPersistenceData();
502
+
503
+ if (this.persistenceData) {
504
+ if (this.persistenceData[mpid]) {
505
+ this.persistenceData[mpid][key] = value;
506
+ } else {
507
+ this.persistenceData[mpid] = {
508
+ [key]: value,
509
+ };
510
+ }
511
+
512
+ // Clear out persistence attributes that are empty
513
+ // so that we don't upload empty or undefined values
514
+ if (
515
+ isObject(this.persistenceData[mpid][key]) &&
516
+ isEmpty(this.persistenceData[mpid][key])
517
+ ) {
518
+ delete this.persistenceData[mpid][key];
519
+ }
520
+
521
+ mpInstance._Persistence.savePersistence(this.persistenceData);
522
+ }
523
+ };
524
+
482
525
  this.hasInvalidIdentifyRequest = (): boolean => {
483
526
  const { identifyRequest } = this.SDKConfig;
484
527
  return (
@@ -487,47 +530,52 @@ export default function Store(
487
530
  isEmpty(identifyRequest.userIdentities)) ||
488
531
  !identifyRequest
489
532
  );
490
- };
533
+ }
491
534
 
492
- this.getDeviceId = () => this.deviceId;
493
- this.setDeviceId = (deviceId: string) => {
494
- this.deviceId = deviceId;
495
- this.persistenceData.gs.das = deviceId;
496
- mpInstance._Persistence.update();
497
- };
535
+
498
536
 
499
- this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
500
- const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);
537
+ this.getConsentState = (mpid: MPID): ConsentState => {
538
+ const {
539
+ fromMinifiedJsonObject,
540
+ } = mpInstance._Consent.ConsentSerialization;
541
+
542
+ const serializedConsentState = this._getFromPersistence<
543
+ IMinifiedConsentJSONObject
544
+ >(mpid, 'con');
501
545
 
502
- if (mpid && previousMPID !== mpid && indexOfMPID < 0) {
503
- this.currentSessionMPIDs.push(mpid);
504
- return;
546
+ if (!isEmpty(serializedConsentState)) {
547
+ return fromMinifiedJsonObject(serializedConsentState);
505
548
  }
506
549
 
507
- if (indexOfMPID >= 0) {
508
- this.currentSessionMPIDs = moveElementToEnd(
509
- this.currentSessionMPIDs,
510
- indexOfMPID
511
- );
512
- }
550
+ return null;
513
551
  };
514
552
 
515
- this.getFirstSeenTime = (mpid: MPID) => {
516
- if (!mpid) {
517
- return null;
553
+ this.setConsentState = (mpid: MPID, consentState: ConsentState) => {
554
+ const {
555
+ toMinifiedJsonObject,
556
+ } = mpInstance._Consent.ConsentSerialization;
557
+
558
+ // If ConsentState is null, we assume the intent is to clear out the consent state
559
+ if (consentState || consentState === null) {
560
+ this._setPersistence(
561
+ mpid,
562
+ 'con',
563
+ toMinifiedJsonObject(consentState)
564
+ );
518
565
  }
566
+ };
519
567
 
520
- if (
521
- this.persistenceData &&
522
- this.persistenceData[mpid] &&
523
- this.persistenceData[mpid].fst
524
- ) {
525
- return this.persistenceData[mpid].fst;
526
- } else {
527
- return null;
528
- }
568
+ this.getDeviceId = () => this.deviceId;
569
+ this.setDeviceId = (deviceId: string) => {
570
+ this.deviceId = deviceId;
571
+ this.persistenceData.gs.das = deviceId;
572
+ mpInstance._Persistence.update();
529
573
  };
530
574
 
575
+
576
+ this.getFirstSeenTime = (mpid: MPID) =>
577
+ this._getFromPersistence<number>(mpid, 'fst');
578
+
531
579
  this.setFirstSeenTime = (mpid: MPID, _time?: number) => {
532
580
  if (!mpid) {
533
581
  return;
@@ -535,35 +583,20 @@ export default function Store(
535
583
 
536
584
  const time = _time || new Date().getTime();
537
585
 
538
- if (this.persistenceData) {
539
- if (!this.persistenceData[mpid]) {
540
- this.persistenceData[mpid] = {};
541
- }
542
- if (!this.persistenceData[mpid].fst) {
543
- this.persistenceData[mpid].fst = time;
544
- mpInstance._Persistence.savePersistence(this.persistenceData);
545
- }
546
- }
586
+ this._setPersistence(mpid, 'fst', time);
547
587
  };
548
588
 
549
- this.getLastSeenTime = (mpid: MPID) => {
589
+ this.getLastSeenTime = (mpid: MPID): number => {
550
590
  if (!mpid) {
551
591
  return null;
552
592
  }
553
- // // https://go.mparticle.com/work/SQDSDKS-6315
593
+ // https://go.mparticle.com/work/SQDSDKS-6315
554
594
  const currentUser = mpInstance.Identity.getCurrentUser();
555
595
  if (mpid === currentUser?.getMPID()) {
556
596
  // if the mpid is the current user, its last seen time is the current time
557
597
  return new Date().getTime();
558
- } else if (
559
- this.persistenceData &&
560
- this.persistenceData[mpid] &&
561
- this.persistenceData[mpid].lst
562
- ) {
563
- return this.persistenceData[mpid].lst;
564
- } else {
565
- return null;
566
598
  }
599
+ return this._getFromPersistence<number>(mpid, 'lst');
567
600
  };
568
601
 
569
602
  this.setLastSeenTime = (mpid: MPID, _time?: number) => {
@@ -573,14 +606,40 @@ export default function Store(
573
606
 
574
607
  const time = _time || new Date().getTime();
575
608
 
576
- if (this.persistenceData) {
577
- if (!this.persistenceData[mpid]) {
578
- this.persistenceData[mpid] = {};
579
- }
580
- if (!this.persistenceData[mpid].lst) {
581
- this.persistenceData[mpid].lst = time;
582
- mpInstance._Persistence.savePersistence(this.persistenceData);
583
- }
609
+ this._setPersistence(mpid, 'lst', time);
610
+ };
611
+
612
+ this.syncPersistenceData = () => {
613
+ const persistenceData = mpInstance._Persistence.getPersistence();
614
+
615
+ this.persistenceData = mpInstance._Helpers.extend(
616
+ {},
617
+ this.persistenceData,
618
+ persistenceData,
619
+ );
620
+ };
621
+
622
+ this.getUserAttributes = (mpid: MPID): UserAttributes =>
623
+ this._getFromPersistence(mpid, 'ua') || {};
624
+
625
+ this.setUserAttributes = (
626
+ mpid: MPID,
627
+ userAttributes: UserAttributes
628
+ ): void => this._setPersistence(mpid, 'ua', userAttributes);
629
+
630
+ this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
631
+ const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);
632
+
633
+ if (mpid && previousMPID !== mpid && indexOfMPID < 0) {
634
+ this.currentSessionMPIDs.push(mpid);
635
+ return;
636
+ }
637
+
638
+ if (indexOfMPID >= 0) {
639
+ this.currentSessionMPIDs = moveElementToEnd(
640
+ this.currentSessionMPIDs,
641
+ indexOfMPID
642
+ );
584
643
  }
585
644
  };
586
645