@mparticle/web-sdk 2.26.6 → 2.26.8

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.
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';
@@ -25,13 +25,17 @@ import {
25
25
  isEmpty,
26
26
  isNumber,
27
27
  isObject,
28
+ moveElementToEnd,
28
29
  parseNumber,
29
30
  returnConvertedBoolean,
30
31
  } from './utils';
31
- import { SDKConsentState } from './consent';
32
+ import { IMinifiedConsentJSONObject, SDKConsentState } from './consent';
32
33
  import { Kit, MPForwarder } from './forwarders.interfaces';
33
- import { IPersistenceMinified } from './persistence.interfaces';
34
34
  import { IdentityCallback } from './identity-user-interfaces';
35
+ import {
36
+ IGlobalStoreV2MinifiedKeys,
37
+ IPersistenceMinified,
38
+ } from './persistence.interfaces';
35
39
 
36
40
  // This represents the runtime configuration of the SDK AFTER
37
41
  // initialization has been complete and all settings and
@@ -179,6 +183,12 @@ export interface IStore {
179
183
 
180
184
  persistenceData?: IPersistenceMinified;
181
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
+
182
192
  getDeviceId?(): string;
183
193
  setDeviceId?(deviceId: string): void;
184
194
  getFirstSeenTime?(mpid: MPID): number;
@@ -186,9 +196,11 @@ export interface IStore {
186
196
  getLastSeenTime?(mpid: MPID): number;
187
197
  setLastSeenTime?(mpid: MPID, time?: number): void;
188
198
 
199
+ addMpidToSessionHistory?(mpid: MPID, previousMpid?: MPID): void;
189
200
  hasInvalidIdentifyRequest?: () => boolean;
190
201
  nullifySession?: () => void;
191
202
  processConfig(config: SDKInitConfig): void;
203
+ syncPersistenceData?: () => void;
192
204
  }
193
205
 
194
206
  // TODO: Merge this with SDKStoreApi in sdkRuntimeModels
@@ -251,24 +263,8 @@ export default function Store(
251
263
 
252
264
  // Placeholder for in-memory persistence model
253
265
  persistenceData: {
254
- cu: null,
255
- gs: {
256
- sid: null,
257
- ie: null,
258
- sa: null,
259
- ss: null,
260
- dt: null,
261
- av: null,
262
- cgid: null,
263
- das: null,
264
- ia: null,
265
- c: null,
266
- csm: null,
267
- les: null,
268
- ssd: null,
269
- },
270
- l: null,
271
- },
266
+ gs: {} as IGlobalStoreV2MinifiedKeys,
267
+ } as IPersistenceMinified,
272
268
  };
273
269
 
274
270
  for (var key in defaultStore) {
@@ -477,6 +473,53 @@ export default function Store(
477
473
  }
478
474
  }
479
475
 
476
+ this._getFromPersistence = <T>(mpid: MPID, key: string): T | null => {
477
+ if (!mpid) {
478
+ return null;
479
+ }
480
+
481
+ this.syncPersistenceData();
482
+
483
+ if (
484
+ this.persistenceData &&
485
+ this.persistenceData[mpid] &&
486
+ this.persistenceData[mpid][key]
487
+ ) {
488
+ return this.persistenceData[mpid][key] as T;
489
+ } else {
490
+ return null;
491
+ }
492
+ };
493
+
494
+ this._setPersistence = <T>(mpid: MPID, key: string, value: T): void => {
495
+ if (!mpid) {
496
+ return;
497
+ }
498
+
499
+ this.syncPersistenceData();
500
+
501
+ if (this.persistenceData) {
502
+ if (this.persistenceData[mpid]) {
503
+ this.persistenceData[mpid][key] = value;
504
+ } else {
505
+ this.persistenceData[mpid] = {
506
+ [key]: value,
507
+ };
508
+ }
509
+
510
+ // Clear out persistence attributes that are empty
511
+ // so that we don't upload empty or undefined values
512
+ if (
513
+ isObject(this.persistenceData[mpid][key]) &&
514
+ isEmpty(this.persistenceData[mpid][key])
515
+ ) {
516
+ delete this.persistenceData[mpid][key];
517
+ }
518
+
519
+ mpInstance._Persistence.savePersistence(this.persistenceData);
520
+ }
521
+ };
522
+
480
523
  this.hasInvalidIdentifyRequest = (): boolean => {
481
524
  const { identifyRequest } = this.SDKConfig;
482
525
  return (
@@ -485,6 +528,39 @@ export default function Store(
485
528
  isEmpty(identifyRequest.userIdentities)) ||
486
529
  !identifyRequest
487
530
  );
531
+ }
532
+
533
+
534
+
535
+ this.getConsentState = (mpid: MPID): ConsentState => {
536
+ const {
537
+ fromMinifiedJsonObject,
538
+ } = mpInstance._Consent.ConsentSerialization;
539
+
540
+ const serializedConsentState = this._getFromPersistence<
541
+ IMinifiedConsentJSONObject
542
+ >(mpid, 'con');
543
+
544
+ if (!isEmpty(serializedConsentState)) {
545
+ return fromMinifiedJsonObject(serializedConsentState);
546
+ }
547
+
548
+ return null;
549
+ };
550
+
551
+ this.setConsentState = (mpid: MPID, consentState: ConsentState) => {
552
+ const {
553
+ toMinifiedJsonObject,
554
+ } = mpInstance._Consent.ConsentSerialization;
555
+
556
+ // If ConsentState is null, we assume the intent is to clear out the consent state
557
+ if (consentState || consentState === null) {
558
+ this._setPersistence(
559
+ mpid,
560
+ 'con',
561
+ toMinifiedJsonObject(consentState)
562
+ );
563
+ }
488
564
  };
489
565
 
490
566
  this.getDeviceId = () => this.deviceId;
@@ -494,21 +570,9 @@ export default function Store(
494
570
  mpInstance._Persistence.update();
495
571
  };
496
572
 
497
- this.getFirstSeenTime = (mpid: MPID) => {
498
- if (!mpid) {
499
- return null;
500
- }
501
573
 
502
- if (
503
- this.persistenceData &&
504
- this.persistenceData[mpid] &&
505
- this.persistenceData[mpid].fst
506
- ) {
507
- return this.persistenceData[mpid].fst;
508
- } else {
509
- return null;
510
- }
511
- };
574
+ this.getFirstSeenTime = (mpid: MPID) =>
575
+ this._getFromPersistence<number>(mpid, 'fst');
512
576
 
513
577
  this.setFirstSeenTime = (mpid: MPID, _time?: number) => {
514
578
  if (!mpid) {
@@ -517,35 +581,20 @@ export default function Store(
517
581
 
518
582
  const time = _time || new Date().getTime();
519
583
 
520
- if (this.persistenceData) {
521
- if (!this.persistenceData[mpid]) {
522
- this.persistenceData[mpid] = {};
523
- }
524
- if (!this.persistenceData[mpid].fst) {
525
- this.persistenceData[mpid].fst = time;
526
- mpInstance._Persistence.savePersistence(this.persistenceData);
527
- }
528
- }
584
+ this._setPersistence(mpid, 'fst', time);
529
585
  };
530
586
 
531
- this.getLastSeenTime = (mpid: MPID) => {
587
+ this.getLastSeenTime = (mpid: MPID): number => {
532
588
  if (!mpid) {
533
589
  return null;
534
590
  }
535
- // // https://go.mparticle.com/work/SQDSDKS-6315
591
+ // https://go.mparticle.com/work/SQDSDKS-6315
536
592
  const currentUser = mpInstance.Identity.getCurrentUser();
537
593
  if (mpid === currentUser?.getMPID()) {
538
594
  // if the mpid is the current user, its last seen time is the current time
539
595
  return new Date().getTime();
540
- } else if (
541
- this.persistenceData &&
542
- this.persistenceData[mpid] &&
543
- this.persistenceData[mpid].lst
544
- ) {
545
- return this.persistenceData[mpid].lst;
546
- } else {
547
- return null;
548
596
  }
597
+ return this._getFromPersistence<number>(mpid, 'lst');
549
598
  };
550
599
 
551
600
  this.setLastSeenTime = (mpid: MPID, _time?: number) => {
@@ -555,14 +604,32 @@ export default function Store(
555
604
 
556
605
  const time = _time || new Date().getTime();
557
606
 
558
- if (this.persistenceData) {
559
- if (!this.persistenceData[mpid]) {
560
- this.persistenceData[mpid] = {};
561
- }
562
- if (!this.persistenceData[mpid].lst) {
563
- this.persistenceData[mpid].lst = time;
564
- mpInstance._Persistence.savePersistence(this.persistenceData);
565
- }
607
+ this._setPersistence(mpid, 'lst', time);
608
+ };
609
+
610
+ this.syncPersistenceData = () => {
611
+ const persistenceData = mpInstance._Persistence.getPersistence();
612
+
613
+ this.persistenceData = mpInstance._Helpers.extend(
614
+ {},
615
+ this.persistenceData,
616
+ persistenceData,
617
+ );
618
+ };
619
+
620
+ this.addMpidToSessionHistory = (mpid: MPID, previousMPID?: MPID): void => {
621
+ const indexOfMPID = this.currentSessionMPIDs.indexOf(mpid);
622
+
623
+ if (mpid && previousMPID !== mpid && indexOfMPID < 0) {
624
+ this.currentSessionMPIDs.push(mpid);
625
+ return;
626
+ }
627
+
628
+ if (indexOfMPID >= 0) {
629
+ this.currentSessionMPIDs = moveElementToEnd(
630
+ this.currentSessionMPIDs,
631
+ indexOfMPID
632
+ );
566
633
  }
567
634
  };
568
635
 
@@ -0,0 +1,66 @@
1
+ import { IdentityType } from './types.interfaces';
2
+ import { parseNumber } from './utils';
3
+
4
+ export interface IIdentitiesByType {
5
+ [key: number]: string;
6
+ }
7
+
8
+ export function getNewIdentitiesByName(newIdentitiesByType: IIdentitiesByType) {
9
+ const newIdentitiesByName = {};
10
+
11
+ for (var key in newIdentitiesByType) {
12
+ const identityNameKey = getIdentityName(parseNumber(key));
13
+ newIdentitiesByName[identityNameKey] = newIdentitiesByType[key];
14
+ }
15
+
16
+ return newIdentitiesByName;
17
+ }
18
+
19
+ export function getIdentityName(identityType: IdentityType): string | null {
20
+ switch (identityType) {
21
+ case IdentityType.Other:
22
+ return 'other';
23
+ case IdentityType.CustomerId:
24
+ return 'customerid';
25
+ case IdentityType.Facebook:
26
+ return 'facebook';
27
+ case IdentityType.Twitter:
28
+ return 'twitter';
29
+ case IdentityType.Google:
30
+ return 'google';
31
+ case IdentityType.Microsoft:
32
+ return 'microsoft';
33
+ case IdentityType.Yahoo:
34
+ return 'yahoo';
35
+ case IdentityType.Email:
36
+ return 'email';
37
+ case IdentityType.FacebookCustomAudienceId:
38
+ return 'facebookcustomaudienceid';
39
+ case IdentityType.Other2:
40
+ return 'other2';
41
+ case IdentityType.Other3:
42
+ return 'other3';
43
+ case IdentityType.Other4:
44
+ return 'other4';
45
+ case IdentityType.Other5:
46
+ return 'other5';
47
+ case IdentityType.Other6:
48
+ return 'other6';
49
+ case IdentityType.Other7:
50
+ return 'other7';
51
+ case IdentityType.Other8:
52
+ return 'other8';
53
+ case IdentityType.Other9:
54
+ return 'other9';
55
+ case IdentityType.Other10:
56
+ return 'other10';
57
+ case IdentityType.MobileNumber:
58
+ return 'mobile_number';
59
+ case IdentityType.PhoneNumber2:
60
+ return 'phone_number_2';
61
+ case IdentityType.PhoneNumber3:
62
+ return 'phone_number_3';
63
+ default:
64
+ return null;
65
+ }
66
+ }
package/src/types.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { getIdentityName } from './type-utils';
2
+
1
3
  var MessageType = {
2
4
  SessionStart: 1,
3
5
  SessionEnd: 2,
@@ -209,50 +211,7 @@ IdentityType.getIdentityType = function(identityName) {
209
211
  };
210
212
 
211
213
  IdentityType.getIdentityName = function(identityType) {
212
- switch (identityType) {
213
- case IdentityType.Other:
214
- return 'other';
215
- case IdentityType.CustomerId:
216
- return 'customerid';
217
- case IdentityType.Facebook:
218
- return 'facebook';
219
- case IdentityType.Twitter:
220
- return 'twitter';
221
- case IdentityType.Google:
222
- return 'google';
223
- case IdentityType.Microsoft:
224
- return 'microsoft';
225
- case IdentityType.Yahoo:
226
- return 'yahoo';
227
- case IdentityType.Email:
228
- return 'email';
229
- case IdentityType.FacebookCustomAudienceId:
230
- return 'facebookcustomaudienceid';
231
- case IdentityType.Other2:
232
- return 'other2';
233
- case IdentityType.Other3:
234
- return 'other3';
235
- case IdentityType.Other4:
236
- return 'other4';
237
- case IdentityType.Other5:
238
- return 'other5';
239
- case IdentityType.Other6:
240
- return 'other6';
241
- case IdentityType.Other7:
242
- return 'other7';
243
- case IdentityType.Other8:
244
- return 'other8';
245
- case IdentityType.Other9:
246
- return 'other9';
247
- case IdentityType.Other10:
248
- return 'other10';
249
- case IdentityType.MobileNumber:
250
- return 'mobile_number';
251
- case IdentityType.PhoneNumber2:
252
- return 'phone_number_2';
253
- case IdentityType.PhoneNumber3:
254
- return 'phone_number_3';
255
- }
214
+ return getIdentityName(identityType);
256
215
  };
257
216
 
258
217
  var ProductActionType = {
@@ -0,0 +1,25 @@
1
+ import { IMParticleUser, IdentityResultBody } from './identity-user-interfaces';
2
+
3
+ export function hasMPIDAndUserLoginChanged(
4
+ previousUser: IMParticleUser,
5
+ newUser: IMParticleUser
6
+ ): boolean {
7
+ return (
8
+ !previousUser ||
9
+ newUser.getMPID() !== previousUser.getMPID() ||
10
+ previousUser.isLoggedIn() !== newUser.isLoggedIn()
11
+ );
12
+ }
13
+
14
+ // https://go.mparticle.com/work/SQDSDKS-6504
15
+ export function hasMPIDChanged(
16
+ prevUser: IMParticleUser,
17
+ identityApiResult: IdentityResultBody
18
+ ): boolean {
19
+ return (
20
+ !prevUser ||
21
+ (prevUser.getMPID() &&
22
+ identityApiResult.mpid &&
23
+ identityApiResult.mpid !== prevUser.getMPID())
24
+ );
25
+ }
package/src/utils.ts CHANGED
@@ -229,6 +229,9 @@ const mergeObjects = <T extends object>(...objects: T[]): T => {
229
229
  return Object.assign({}, ...objects);
230
230
  };
231
231
 
232
+ const moveElementToEnd = <T>(array: T[], index: number): T[] =>
233
+ array.slice(0, index).concat(array.slice(index + 1), array[index]);
234
+
232
235
  export {
233
236
  createCookieString,
234
237
  revertCookieString,
@@ -258,4 +261,5 @@ export {
258
261
  isEmpty,
259
262
  isValidCustomFlagProperty,
260
263
  mergeObjects,
264
+ moveElementToEnd,
261
265
  };