@mparticle/web-sdk 2.19.2 → 2.20.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.
package/src/identity.js CHANGED
@@ -904,7 +904,8 @@ export default function Identity(mpInstance) {
904
904
  self.IdentityAPI.getCurrentUser().getUserIdentities(),
905
905
  mpInstance._APIClient.prepareForwardingStats
906
906
  );
907
- mpInstance._Forwarders.callSetUserAttributeOnForwarders(
907
+ mpInstance._Forwarders.handleForwarderUserAttributes(
908
+ 'setUserAttribute',
908
909
  key,
909
910
  newValue
910
911
  );
@@ -990,9 +991,10 @@ export default function Identity(mpInstance) {
990
991
  self.IdentityAPI.getCurrentUser().getUserIdentities(),
991
992
  mpInstance._APIClient.prepareForwardingStats
992
993
  );
993
- mpInstance._Forwarders.applyToForwarders(
994
+ mpInstance._Forwarders.handleForwarderUserAttributes(
994
995
  'removeUserAttribute',
995
- key
996
+ key,
997
+ null
996
998
  );
997
999
  }
998
1000
  },
@@ -1098,7 +1100,8 @@ export default function Identity(mpInstance) {
1098
1100
  self.IdentityAPI.getCurrentUser().getUserIdentities(),
1099
1101
  mpInstance._APIClient.prepareForwardingStats
1100
1102
  );
1101
- mpInstance._Forwarders.callSetUserAttributeOnForwarders(
1103
+ mpInstance._Forwarders.handleForwarderUserAttributes(
1104
+ 'setUserAttribute',
1102
1105
  key,
1103
1106
  arrayCopy
1104
1107
  );
@@ -1127,9 +1130,10 @@ export default function Identity(mpInstance) {
1127
1130
  if (userAttributes) {
1128
1131
  for (var prop in userAttributes) {
1129
1132
  if (userAttributes.hasOwnProperty(prop)) {
1130
- mpInstance._Forwarders.applyToForwarders(
1133
+ mpInstance._Forwarders.handleForwarderUserAttributes(
1131
1134
  'removeUserAttribute',
1132
- prop
1135
+ prop,
1136
+ null
1133
1137
  );
1134
1138
  }
1135
1139
  this.removeUserAttribute(prop);
@@ -40,6 +40,7 @@ export default class _BatchValidator {
40
40
  _SessionManager: null,
41
41
  _Store: {
42
42
  sessionId: 'mockSessionId',
43
+ sideloadedKits: [],
43
44
  devToken: 'test_dev_token',
44
45
  isFirstRun: true,
45
46
  isEnabled: true,
@@ -1421,6 +1421,8 @@ function completeSDKInitialization(apiKey, config, mpInstance) {
1421
1421
  mpInstance._APIClient.prepareForwardingStats
1422
1422
  );
1423
1423
 
1424
+ mpInstance._Forwarders.processPixelConfigs(config);
1425
+
1424
1426
  mpInstance._SessionManager.initialize();
1425
1427
  mpInstance._Events.logAST();
1426
1428
 
@@ -0,0 +1,138 @@
1
+ import { Context } from '@mparticle/event-models';
2
+ import {
3
+ AllUserAttributes,
4
+ ConsentState,
5
+ IdentityApiData,
6
+ MPID,
7
+ Product,
8
+ UserIdentities,
9
+ } from '@mparticle/web-sdk';
10
+ import { ForwardingStatsData } from './apiClient';
11
+ import {
12
+ IntegrationAttributes,
13
+ ServerSettings,
14
+ SessionAttributes,
15
+ } from './store';
16
+ import { Dictionary } from './utils';
17
+
18
+ export type CookieSyncDate = Dictionary<string>;
19
+ export type UploadsTable = Dictionary<any>;
20
+ export interface iForwardingStatsBatches {
21
+ uploadsTable: UploadsTable;
22
+ forwardingStatsEventQueue: ForwardingStatsData[];
23
+ }
24
+
25
+ // TODO: Migrate this to @types/mparticle__web-sdk
26
+ // https://go.mparticle.com/work/SQDSDKS-5196
27
+ export type UserAttributes = AllUserAttributes;
28
+
29
+ export interface IGlobalStoreV2MinifiedKeys {
30
+ sid: string; // Session ID
31
+ ie: boolean; // Is Enabled
32
+ sa: SessionAttributes;
33
+ ss: ServerSettings;
34
+ dt: string; // Dev Token
35
+ av: string; // App Version
36
+ cgid: string; // Client Generated ID
37
+ das: string; // Device ID/ Device Application String
38
+ ia: IntegrationAttributes;
39
+ c: Context;
40
+ csm: MPID[]; // Current Session MPIDs
41
+ les: number; // Last Event Sent Timestamp
42
+ ssd: number; // Session Start Date
43
+ }
44
+
45
+ export interface IPersistenceMinified extends Dictionary {
46
+ cu: MPID; // Current User MPID
47
+ gs: IGlobalStoreV2MinifiedKeys;
48
+ l: false; // IsLoggedIn
49
+
50
+ // Persistence Minified can also store optional dictionaries with
51
+ // an idex of MPID
52
+ // [mpid: MPID]: Dictionary<any>;
53
+
54
+ // For Example:
55
+ // {
56
+ // cu: '123345',
57
+ // gs: {
58
+ // sid: '123',
59
+ // ie: false,
60
+ // sa: {},
61
+ // ss: {},
62
+ // dt: '',
63
+ // av: '',
64
+ // cgid: '123',
65
+ // das: 'das',
66
+ // ia: {},
67
+ // c: null,
68
+ // csm: ['123'],
69
+ // les: 0,
70
+ // ssd: 0,
71
+ // },
72
+ // l: false,
73
+ // MPID1: {
74
+ // csd: [],
75
+ // ui: {
76
+ // customerid: '12346',
77
+ // },
78
+ // },
79
+ // };
80
+ }
81
+
82
+ export interface IPersistence {
83
+ useLocalStorage(): boolean;
84
+ initializeStorage(): void;
85
+ update(): void;
86
+ storeProductsInMemory(products: Product[], mpid: MPID): void;
87
+ storeDataInMemory(obj: IPersistenceMinified, currentMPID: MPID): void;
88
+ determineLocalStorageAvailability(storage: Storage): boolean;
89
+ getUserProductsFromLS(mpid: MPID): Product[];
90
+ getAllUserProductsFromLS(): Product[];
91
+ setLocalStorage(): void;
92
+ getLocalStorage(): IPersistenceMinified | null;
93
+ expireCookies(cookieName: string): void;
94
+ getCookie(): IPersistenceMinified | null;
95
+ setCookie(): void;
96
+ reduceAndEncodePersistence(
97
+ persistence: IPersistenceMinified,
98
+ expires: string,
99
+ domain: string,
100
+ maxCookieSize: number
101
+ ): void;
102
+ findPrevCookiesBasedOnUI(identityApiData: IdentityApiData): void;
103
+ encodePersistence(persistance: IPersistenceMinified): string;
104
+ decodePersistence(persistance: IPersistenceMinified): string;
105
+ replaceCommasWithPipes(string: string): string;
106
+ replacePipesWithCommas(string: string): string;
107
+ replaceApostrophesWithQuotes(string: string): string;
108
+ replaceQuotesWithApostrophes(string: string): string;
109
+ createCookieString(string: string): string;
110
+ revertCookieString(string: string): string;
111
+ getCookieDomain(): string;
112
+ getDomain(doc: string, locationHostname: string): string;
113
+ getUserIdentities(mpid: MPID): UserIdentities;
114
+ getAllUserAttributes(mpid: MPID): AllUserAttributes;
115
+ getCartProducts(mpid: MPID): Product[];
116
+ setCartProducts(allProducts: Product[]): void;
117
+ saveUserIdentitiesToPersistence(
118
+ mpid: MPID,
119
+ userIdentities: UserIdentities
120
+ ): void;
121
+ saveUserAttributesToPersistence(
122
+ mpid: MPID,
123
+ userAttributes: UserAttributes
124
+ ): void;
125
+ saveUserCookieSyncDatesToPersistence(mpid: MPID, csd: CookieSyncDate): void;
126
+ saveUserConsentStateToCookies(mpid, consentState: ConsentState): void;
127
+ savePersistence(persistance: IPersistenceMinified): void;
128
+ getPersistence(): IPersistenceMinified;
129
+ getConsentState(mpid: MPID): ConsentState | null;
130
+ getFirstSeenTime(mpid: MPID): string | null;
131
+ setFirstSeenTime(mpid: MPID, time: number): void;
132
+ getLastSeenTime(mpid: MPID): number | null;
133
+ setLastSeenTime(mpid: MPID, time: number): void;
134
+ getDeviceId(): string;
135
+ setDeviceId(guid: string): void;
136
+ resetPersistence(): void;
137
+ forwardingStatsBatches: iForwardingStatsBatches;
138
+ }
@@ -959,6 +959,9 @@ export default function _Persistence(mpInstance) {
959
959
  if (userAttributes) {
960
960
  if (persistence) {
961
961
  if (persistence[mpid]) {
962
+ // TODO: Investigate why setting this to UI still shows up as UA
963
+ // when running `mParticle.getInstance()._Persistence.getLocalStorage()`
964
+ // https://go.mparticle.com/work/SQDSDKS-5195
962
965
  persistence[mpid].ui = userAttributes;
963
966
  } else {
964
967
  persistence[mpid] = {
@@ -7,6 +7,7 @@ import { Dictionary } from './utils';
7
7
  import { IServerModel } from './serverModel';
8
8
  import { IKitConfigs } from './configAPIClient';
9
9
  import { SDKConsentApi, SDKConsentState } from './consent';
10
+ import { IPersistence } from './persistence.interfaces';
10
11
 
11
12
  // TODO: Resolve this with version in @mparticle/web-sdk
12
13
  export type SDKEventCustomFlags = Dictionary<any>;
@@ -139,7 +140,7 @@ export interface MParticleWebSDK {
139
140
  _Consent: SDKConsentApi;
140
141
  Consent: SDKConsentApi;
141
142
  _NativeSdkHelpers: any; // TODO: Set up API
142
- _Persistence: any; // TODO: Set up Persistence API
143
+ _Persistence: IPersistence;
143
144
  _preInit: any; // TODO: Set up API
144
145
  _resetForTests(MPConfig?: SDKInitConfig): void;
145
146
  init(apiKey: string, config: SDKInitConfig, instanceName?: string): void;
@@ -186,6 +187,7 @@ export interface SDKInitConfig
186
187
 
187
188
  kitConfigs?: IKitConfigs[];
188
189
  kits?: Dictionary<Kit>;
190
+ sideloadedKits?: MPForwarder[];
189
191
  dataPlanOptions?: KitBlockerOptions;
190
192
  flags?: Dictionary;
191
193
 
package/src/store.ts CHANGED
@@ -54,7 +54,7 @@ export interface SDKConfig {
54
54
  identifyRequest: IdentifyRequest;
55
55
  identityCallback: IdentityCallback;
56
56
  integrationDelayTimeout: number;
57
-
57
+ sideloadedKits: MPForwarder[];
58
58
  aliasMaxWindow: number;
59
59
  deviceId?: string;
60
60
  forceHttps?: boolean;
@@ -101,6 +101,8 @@ function createSDKConfig(config: SDKInitConfig): SDKConfig {
101
101
  export type PixelConfiguration = Dictionary;
102
102
  export type MigrationData = Dictionary;
103
103
  export type ServerSettings = Dictionary;
104
+ export type SessionAttributes = Dictionary;
105
+ export type IntegrationAttributes = Dictionary<Dictionary<string>>;
104
106
 
105
107
  type WrapperSDKTypes = 'flutter' | 'none';
106
108
  interface WrapperSDKInfo {
@@ -112,7 +114,7 @@ interface WrapperSDKInfo {
112
114
  // Temporary Interface until Store can be refactored as a class
113
115
  export interface IStore {
114
116
  isEnabled: boolean;
115
- sessionAttributes: Dictionary;
117
+ sessionAttributes: SessionAttributes;
116
118
  currentSessionMPIDs: MPID[];
117
119
  consentState: SDKConsentState | null;
118
120
  sessionId: string | null;
@@ -140,13 +142,14 @@ export interface IStore {
140
142
  identifyCalled: boolean;
141
143
  isLoggedIn: boolean;
142
144
  cookieSyncDates: Dictionary<number>;
143
- integrationAttributes: Dictionary<Dictionary<string>>;
145
+ integrationAttributes: IntegrationAttributes;
144
146
  requireDelay: boolean;
145
147
  isLocalStorageAvailable: boolean | null;
146
148
  storageName: string | null;
147
149
  prodStorageName: string | null;
148
150
  activeForwarders: MPForwarder[];
149
151
  kits: Dictionary<MPForwarder>;
152
+ sideloadedKits: MPForwarder[];
150
153
  configuredForwarders: MPForwarder[];
151
154
  pixelConfigurations: PixelConfiguration[];
152
155
  integrationDelayTimeoutStart: number; // UNIX Timestamp
@@ -197,6 +200,7 @@ export default function Store(
197
200
  prodStorageName: null,
198
201
  activeForwarders: [],
199
202
  kits: {},
203
+ sideloadedKits: [],
200
204
  configuredForwarders: [],
201
205
  pixelConfigurations: [],
202
206
  wrapperSDKInfo: {
@@ -259,6 +263,8 @@ export default function Store(
259
263
 
260
264
  this.SDKConfig.kits = config.kits || {};
261
265
 
266
+ this.SDKConfig.sideloadedKits = config.sideloadedKits || [];
267
+
262
268
  if (config.hasOwnProperty('isIOS')) {
263
269
  this.SDKConfig.isIOS = config.isIOS;
264
270
  } else {
package/src/vault.ts CHANGED
@@ -6,12 +6,10 @@ interface IVaultOptions {
6
6
  offlineStorageEnabled?: boolean;
7
7
  }
8
8
 
9
- export default class Vault<StorableItem extends Dictionary> {
10
- public contents: Dictionary<StorableItem>;
9
+ export default class Vault<StorableItem> {
10
+ public contents: StorableItem;
11
11
  private readonly _storageKey: string;
12
- private readonly _itemKey: keyof StorableItem;
13
12
  private logger?: Logger;
14
- private offlineStorageEnabled: boolean = false;
15
13
 
16
14
  /**
17
15
  *
@@ -19,16 +17,9 @@ export default class Vault<StorableItem extends Dictionary> {
19
17
  * @param {string} itemKey an element within your StorableItem to use as a key
20
18
  * @param {IVaultOptions} options A Dictionary of IVaultOptions
21
19
  */
22
- constructor(
23
- storageKey: string,
24
- itemKey: keyof StorableItem,
25
- options?: IVaultOptions
26
- ) {
20
+ constructor(storageKey: string, options?: IVaultOptions) {
27
21
  this._storageKey = storageKey;
28
- this._itemKey = itemKey;
29
- this.contents = this.getItems() || {};
30
-
31
- this.offlineStorageEnabled = options?.offlineStorageEnabled;
22
+ this.contents = this.getFromLocalStorage();
32
23
 
33
24
  // Add a fake logger in case one is not provided or needed
34
25
  this.logger = options?.logger || {
@@ -39,51 +30,16 @@ export default class Vault<StorableItem extends Dictionary> {
39
30
  }
40
31
 
41
32
  /**
42
- * Stores a single Item using `itemId` as an index
33
+ * Stores a StorableItem to Local Storage
43
34
  * @method storeItem
44
- * @param item {StorableItem} a Dictonary with key to store
45
- */
46
- public storeItem(item: StorableItem): void {
47
- this.contents = this.getItems();
48
-
49
- if (item[this._itemKey]) {
50
- this.contents[item[this._itemKey]] = item;
51
- this.logger.verbose(
52
- `Saved items to vault with key: ${item[this._itemKey]}`
53
- );
54
- }
55
-
56
- this.saveItems(this.contents);
57
- }
58
-
59
- /**
60
- * Stores Items using `itemId` as an index
61
- * @method storeItems
62
- * @param {StorableItem[]} an Array of StorableItems
63
- */
64
- public storeItems(items: StorableItem[]): void {
65
- items.forEach((item) => this.storeItem(item));
66
- }
67
-
68
- /**
69
- * Removes a single StorableItem based on `indexId`
70
- * @method removeItem
71
- * @param {String} indexId
35
+ * @param item {StorableItem}
72
36
  */
73
- public removeItem(indexId: string): void {
74
- this.logger.verbose(`Removing from vault: ${indexId}`);
75
- this.contents = this.getItems() || {};
37
+ public store(item: StorableItem): void {
38
+ this.contents = item;
76
39
 
77
- try {
78
- delete this.contents[indexId];
40
+ this.logger.verbose(`Saved to local storage: ${item}`);
79
41
 
80
- this.saveItems(this.contents);
81
- } catch (error) {
82
- this.logger.error(
83
- `Unable to remove item without a matching ID ${indexId}`
84
- );
85
- this.logger.error(error as string);
86
- }
42
+ this.saveToLocalStorage(this.contents);
87
43
  }
88
44
 
89
45
  /**
@@ -91,10 +47,10 @@ export default class Vault<StorableItem extends Dictionary> {
91
47
  * @method retrieveItems
92
48
  * @returns {StorableItem[]} an array of Items
93
49
  */
94
- public retrieveItems(): StorableItem[] {
95
- this.contents = this.getItems();
50
+ public retrieve(): StorableItem {
51
+ this.contents = this.getFromLocalStorage();
96
52
 
97
- return Object.keys(this.contents).map((item) => this.contents[item]);
53
+ return this.contents;
98
54
  }
99
55
 
100
56
  /**
@@ -102,14 +58,11 @@ export default class Vault<StorableItem extends Dictionary> {
102
58
  * @method purge
103
59
  */
104
60
  public purge(): void {
105
- this.contents = {};
106
- this.removeItems();
61
+ this.contents = null;
62
+ this.removeFromLocalStorage();
107
63
  }
108
64
 
109
- private saveItems(items: Dictionary<StorableItem>): void {
110
- if (!this.offlineStorageEnabled) {
111
- return;
112
- }
65
+ private saveToLocalStorage(items: StorableItem): void {
113
66
  try {
114
67
  window.localStorage.setItem(
115
68
  this._storageKey,
@@ -121,21 +74,15 @@ export default class Vault<StorableItem extends Dictionary> {
121
74
  }
122
75
  }
123
76
 
124
- private getItems(): Dictionary<StorableItem> {
125
- if (!this.offlineStorageEnabled) {
126
- return this.contents;
127
- }
77
+ private getFromLocalStorage(): StorableItem | null {
128
78
  // TODO: Handle cases where Local Storage is unavailable
129
79
  // https://go.mparticle.com/work/SQDSDKS-5022
130
- const itemString = window.localStorage.getItem(this._storageKey);
80
+ const item: string = window.localStorage.getItem(this._storageKey);
131
81
 
132
- return itemString ? JSON.parse(itemString) : {};
82
+ return item ? JSON.parse(item) : null;
133
83
  }
134
84
 
135
- private removeItems(): void {
136
- if (!this.offlineStorageEnabled) {
137
- return;
138
- }
85
+ private removeFromLocalStorage(): void {
139
86
  window.localStorage.removeItem(this._storageKey);
140
87
  }
141
- }
88
+ }