@mparticle/web-sdk 2.25.3 → 2.26.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.
@@ -1,6 +1,11 @@
1
1
  import * as EventsApi from '@mparticle/event-models';
2
2
  import { DataPlanVersion } from '@mparticle/data-planning-models';
3
- import { MPConfiguration, IdentityApiData } from '@mparticle/web-sdk';
3
+ import {
4
+ MPConfiguration,
5
+ IdentityApiData,
6
+ MPID,
7
+ Callback,
8
+ } from '@mparticle/web-sdk';
4
9
  import { IStore } from './store';
5
10
  import Validators from './validators';
6
11
  import { Dictionary, valueof } from './utils';
@@ -246,19 +251,27 @@ export interface SDKIdentityApi {
246
251
 
247
252
  export interface SDKHelpersApi {
248
253
  canLog?(): boolean;
249
- createServiceUrl(arg0: string, arg1: string): void;
254
+ createServiceUrl(url: string, devToken?: string): void;
250
255
  createXHR?(cb: () => void): XMLHttpRequest;
251
256
  extend?(...args: any[]);
252
257
  parseNumber?(value: string | number): number;
253
258
  generateUniqueId();
254
259
  generateHash?(value: string): string;
255
- getFeatureFlag?(feature: string); // TODO: Feature Constants should be converted to enum
260
+ // https://go.mparticle.com/work/SQDSDKS-6317
261
+ getFeatureFlag?(feature: string): boolean | string; // TODO: Feature Constants should be converted to enum
256
262
  isDelayedByIntegration?(
257
263
  delayedIntegrations: Dictionary<boolean>,
258
264
  timeoutStart: number,
259
265
  now: number
260
266
  ): boolean;
261
267
  isObject?(item: any);
268
+ invokeCallback?(
269
+ callback: Callback,
270
+ code: string,
271
+ body: string,
272
+ mParticleUser?: MParticleUser,
273
+ previousMpid?: MPID
274
+ ): void;
262
275
  returnConvertedBoolean(data: string | boolean | number): boolean;
263
276
  sanitizeAttributes?(
264
277
  attrs: Dictionary<string>,
package/src/store.ts CHANGED
@@ -61,6 +61,7 @@ export interface SDKConfig {
61
61
  aliasUrl?: string;
62
62
  configUrl?: string;
63
63
  identityUrl?: string;
64
+ userAudienceUrl?: string;
64
65
  isIOS?: boolean;
65
66
  maxProducts: number;
66
67
  requestConfig?: boolean;
@@ -169,6 +170,11 @@ export interface IStore {
169
170
  getDeviceId?(): string;
170
171
  setDeviceId?(deviceId: string): void;
171
172
 
173
+ getFirstSeenTime?(mpid: MPID): number;
174
+ setFirstSeenTime?(mpid: MPID, time?: number): void;
175
+ getLastSeenTime?(mpid: MPID): number;
176
+ setLastSeenTime?(mpid: MPID, time?: number): void;
177
+
172
178
  nullifySession?: () => void;
173
179
  }
174
180
 
@@ -260,19 +266,16 @@ export default function Store(
260
266
  this.SDKConfig.flags = {};
261
267
  }
262
268
 
263
- this.SDKConfig.flags = processFlags(
264
- config,
265
- this.SDKConfig as SDKConfig
266
- );
269
+ this.SDKConfig.flags = processFlags(config, this
270
+ .SDKConfig as SDKConfig);
267
271
 
268
272
  if (config.deviceId) {
269
273
  this.deviceId = config.deviceId;
270
274
  }
271
275
  if (config.hasOwnProperty('isDevelopmentMode')) {
272
- this.SDKConfig.isDevelopmentMode =
273
- mpInstance._Helpers.returnConvertedBoolean(
274
- config.isDevelopmentMode
275
- );
276
+ this.SDKConfig.isDevelopmentMode = mpInstance._Helpers.returnConvertedBoolean(
277
+ config.isDevelopmentMode
278
+ );
276
279
  } else {
277
280
  this.SDKConfig.isDevelopmentMode = false;
278
281
  }
@@ -464,6 +467,78 @@ export default function Store(
464
467
  this.sessionAttributes = {};
465
468
  mpInstance._Persistence.update();
466
469
  };
470
+
471
+ this.getFirstSeenTime = (mpid: MPID) => {
472
+ if (!mpid) {
473
+ return null;
474
+ }
475
+
476
+ if (
477
+ this.persistenceData &&
478
+ this.persistenceData[mpid] &&
479
+ this.persistenceData[mpid].fst
480
+ ) {
481
+ return this.persistenceData[mpid].fst;
482
+ } else {
483
+ return null;
484
+ }
485
+ };
486
+
487
+ this.setFirstSeenTime = (mpid: MPID, _time?: number) => {
488
+ if (!mpid) {
489
+ return;
490
+ }
491
+
492
+ const time = _time || new Date().getTime();
493
+
494
+ if (this.persistenceData) {
495
+ if (!this.persistenceData[mpid]) {
496
+ this.persistenceData[mpid] = {};
497
+ }
498
+ if (!this.persistenceData[mpid].fst) {
499
+ this.persistenceData[mpid].fst = time;
500
+ mpInstance._Persistence.savePersistence(this.persistenceData);
501
+ }
502
+ }
503
+ };
504
+
505
+ this.getLastSeenTime = (mpid: MPID) => {
506
+ if (!mpid) {
507
+ return null;
508
+ }
509
+ // // https://go.mparticle.com/work/SQDSDKS-6315
510
+ const currentUser = mpInstance.Identity.getCurrentUser();
511
+ if (mpid === currentUser?.getMPID()) {
512
+ // if the mpid is the current user, its last seen time is the current time
513
+ return new Date().getTime();
514
+ } else if (
515
+ this.persistenceData &&
516
+ this.persistenceData[mpid] &&
517
+ this.persistenceData[mpid].lst
518
+ ) {
519
+ return this.persistenceData[mpid].lst;
520
+ } else {
521
+ return null;
522
+ }
523
+ };
524
+
525
+ this.setLastSeenTime = (mpid: MPID, _time?: number) => {
526
+ if (!mpid) {
527
+ return;
528
+ }
529
+
530
+ const time = _time || new Date().getTime();
531
+
532
+ if (this.persistenceData) {
533
+ if (!this.persistenceData[mpid]) {
534
+ this.persistenceData[mpid] = {};
535
+ }
536
+ if (!this.persistenceData[mpid].lst) {
537
+ this.persistenceData[mpid].lst = time;
538
+ mpInstance._Persistence.savePersistence(this.persistenceData);
539
+ }
540
+ }
541
+ };
467
542
  }
468
543
 
469
544
  export function processFlags(
@@ -477,6 +552,7 @@ export function processFlags(
477
552
  OfflineStorage,
478
553
  DirectUrlRouting,
479
554
  CacheIdentity,
555
+ AudienceAPI,
480
556
  } = Constants.FeatureFlags;
481
557
 
482
558
  if (!config.flags) {
@@ -492,6 +568,7 @@ export function processFlags(
492
568
  flags[OfflineStorage] = config.flags[OfflineStorage] || '0';
493
569
  flags[DirectUrlRouting] = config.flags[DirectUrlRouting] === 'True';
494
570
  flags[CacheIdentity] = config.flags[CacheIdentity] === 'True';
571
+ flags[AudienceAPI] = config.flags[AudienceAPI] === 'True';
495
572
 
496
573
  return flags;
497
574
  }
@@ -0,0 +1,59 @@
1
+ export interface fetchPayload {
2
+ method: string;
3
+ headers: {
4
+ Accept: string;
5
+ 'Content-Type'?: string;
6
+ };
7
+ body?: string;
8
+ }
9
+
10
+ export abstract class AsyncUploader {
11
+ url: string;
12
+ public abstract upload(
13
+ fetchPayload: fetchPayload,
14
+ url?: string
15
+ ): Promise<Response>;
16
+
17
+ constructor(url: string) {
18
+ this.url = url;
19
+ }
20
+ }
21
+
22
+ export class FetchUploader extends AsyncUploader {
23
+ public async upload(
24
+ fetchPayload: fetchPayload,
25
+ _url?: string
26
+ ): Promise<Response> {
27
+ const url = _url || this.url;
28
+ return await fetch(url, fetchPayload);
29
+ }
30
+ }
31
+
32
+ export class XHRUploader extends AsyncUploader {
33
+ public async upload(fetchPayload: fetchPayload): Promise<Response> {
34
+ const response: Response = await this.makeRequest(
35
+ this.url,
36
+ fetchPayload.body
37
+ );
38
+ return response;
39
+ }
40
+
41
+ private async makeRequest(url: string, data: string): Promise<Response> {
42
+ const xhr: XMLHttpRequest = new XMLHttpRequest();
43
+ return new Promise((resolve, reject) => {
44
+ xhr.onreadystatechange = () => {
45
+ if (xhr.readyState !== 4) return;
46
+
47
+ // Process the response
48
+ if (xhr.status >= 200 && xhr.status < 300) {
49
+ resolve((xhr as unknown) as Response);
50
+ } else {
51
+ reject(xhr);
52
+ }
53
+ };
54
+
55
+ xhr.open('post', url);
56
+ xhr.send(data);
57
+ });
58
+ }
59
+ }