@mparticle/web-sdk 2.20.0 → 2.20.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-sdk",
3
- "version": "2.20.0",
3
+ "version": "2.20.1",
4
4
  "description": "mParticle core SDK for web applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -78,7 +78,7 @@
78
78
  "@mparticle/event-models": "^1.1.6",
79
79
  "@rollup/plugin-json": "^4.1.0",
80
80
  "@rollup/plugin-typescript": "^8.3.3",
81
- "@rollup/plugin-babel": "5.3.1",
81
+ "@rollup/plugin-babel": "6.0.3",
82
82
  "@rollup/plugin-commonjs": "22.0.2",
83
83
  "@rollup/plugin-node-resolve": "13.3.0",
84
84
  "@semantic-release/changelog": "^5.0.1",
@@ -93,8 +93,8 @@
93
93
  "browserify": "^17.0.0",
94
94
  "chai": "^4.2.0",
95
95
  "cross-env": "^7.0.3",
96
- "eslint": "^7.32.0",
97
- "eslint-config-prettier": "8.3.0",
96
+ "eslint": "^8.36.0",
97
+ "eslint-config-prettier": "8.8.0",
98
98
  "eslint-plugin-prettier": "3.4.1",
99
99
  "fetch-mock": "^7.5.1",
100
100
  "gts": "^3.1.0",
@@ -116,7 +116,7 @@
116
116
  "should": "^7.1.0",
117
117
  "sinon": "^11.0.0",
118
118
  "tslib": "^2.1.0",
119
- "typescript": "^4.2.4",
119
+ "typescript": "^5.0.2",
120
120
  "uglify-js": "^3.4.9",
121
121
  "webpack": "^5.36.2",
122
122
  "webpack-cli": "^4.6.0"
package/src/apiClient.ts CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  SDKEvent,
9
9
  } from './sdkRuntimeModels';
10
10
  import KitBlocker from './kitBlocking';
11
- import { Dictionary } from './utils';
11
+ import { Dictionary, getRampNumber } from './utils';
12
12
  import { IUploadObject } from './serverModel';
13
13
 
14
14
  const Messages = Constants.Messages;
@@ -67,9 +67,7 @@ export default function APIClient(
67
67
  return false;
68
68
  }
69
69
 
70
- const rampNumber = mpInstance._Helpers.getRampNumber(
71
- mpInstance._Store.deviceId
72
- );
70
+ const rampNumber = getRampNumber(mpInstance._Store.deviceId);
73
71
  return eventsV3Percentage >= rampNumber;
74
72
  };
75
73
 
@@ -29,8 +29,9 @@ export interface IKitConfigs {
29
29
  eventNameFilters: number[];
30
30
  eventTypeFilters: number[];
31
31
  attributeFilters: number[];
32
- filteringEventAttributeValue: Dictionary;
33
- filteringConsentRuleValues: Dictionary;
32
+ filteringEventAttributeValue: IFilteringAttributeValue;
33
+ filteringUserAttributeValue: IFilteringAttributeValue;
34
+ filteringConsentRuleValues: IFilteringConsentRulesValue;
34
35
  consentRegulationFilters: number[];
35
36
  consentRegulationPurposeFilters: number[];
36
37
  messageTypeFilters: number[];
@@ -39,6 +40,22 @@ export interface IKitConfigs {
39
40
  excludeAnonymousUser: boolean;
40
41
  }
41
42
 
43
+ export interface IFilteringAttributeValue {
44
+ eventAttributeName: string;
45
+ eventAttributeValue: string;
46
+ includeOnMatch: boolean
47
+ }
48
+ export interface IFilteringConsentRulesValue {
49
+ includeOnMatch: boolean;
50
+ values: IConsentRuleValue[];
51
+ }
52
+
53
+ export interface IConsentRuleValue {
54
+ consentPurpose: string;
55
+ hasConsented: boolean
56
+ }
57
+
58
+
42
59
  export interface IPixelConfig {
43
60
  name: string;
44
61
  moduleId: number;
@@ -0,0 +1,61 @@
1
+ import { MParticleUser, SDKEvent, SDKEventCustomFlags, SDKUserIdentity } from './sdkRuntimeModels';
2
+ import { Dictionary } from './utils';
3
+ import { IKitConfigs } from './configAPIClient';
4
+ import { UserAttributes } from './persistence.interfaces';
5
+ import { IdentityApiData } from '@mparticle/web-sdk';
6
+
7
+ // The state of the kit when accessed via window.KitName via CDN
8
+ // or imported as an NPM package
9
+ export interface UnregisteredKit {
10
+ register(config): void;
11
+ }
12
+
13
+ // The state of the kit after being added to forwarderConstructors in the CDN
14
+ // or after registered to SDKConfig.kits via NPM
15
+ export interface RegisteredKit {
16
+ constructor(): void;
17
+ name: string;
18
+ getId(): number;
19
+ }
20
+
21
+ // The state of the kit after being configured. This is what the kit looks like when acted on.
22
+ export interface ConfiguredKit
23
+ extends Omit<IKitConfigs, 'isDebugString' | 'hasDebugString'> {
24
+ common: Dictionary<unknown>;
25
+ init(
26
+ settings: Dictionary<unknown>,
27
+ service: forwardingStatsCallback,
28
+ testMode: boolean,
29
+ trackerId: string | null,
30
+ userAttributes: UserAttributes,
31
+ userIdentities: SDKUserIdentity,
32
+ appVersion: string,
33
+ appName: string,
34
+ customFlags: SDKEventCustomFlags,
35
+ clientId: string): string;
36
+ onIdentifyComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
37
+ onLoginComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
38
+ onLogoutComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
39
+ onModifyComplete(user: MParticleUser, filteredIdentityRequest: IdentityApiData): string | KitMappedMethodFailure;
40
+ onUserIdentified(user: MParticleUser): string | KitMappedMethodFailure;
41
+ process(event: SDKEvent): string;
42
+ setOptOut(isOptingOut: boolean): string | KitMappedMethodFailure;
43
+ removeUserAttribute(key: string): string;
44
+ setUserAttribute(key: string, value:string): string;
45
+
46
+ // TODO: Convert type to enum during Identity migration
47
+ // https://go.mparticle.com/work/SQDSDKS-5218
48
+ setUserIdentity(id: UserIdentityId, type: UserIdentityType): void;
49
+
50
+ // TODO: https://go.mparticle.com/work/SQDSDKS-5156
51
+ isSandbox: boolean;
52
+ hasSandbox: boolean;
53
+ }
54
+ export interface KitMappedMethodFailure {
55
+ error: string
56
+ }
57
+
58
+ export type UserIdentityId = string;
59
+ export type UserIdentityType = number;
60
+
61
+ export type forwardingStatsCallback = (forwarder: ConfiguredKit, event: SDKEvent) => void;
package/src/forwarders.js CHANGED
@@ -650,7 +650,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
650
650
  newForwarder.id = config.moduleId;
651
651
 
652
652
  // TODO: isSandbox, hasSandbox is never used in any kit or in core SDK.
653
- // isVisibleInvestigate is only used in 1 place. It is always true if
653
+ // isVisible. Investigate is only used in 1 place. It is always true if
654
654
  // it is sent to JS. Investigate further to determine if these can be removed.
655
655
  // https://go.mparticle.com/work/SQDSDKS-5156
656
656
  newForwarder.isSandbox = config.isDebug || config.isSandbox;
package/src/helpers.js CHANGED
@@ -26,17 +26,6 @@ export default function Helpers(mpInstance) {
26
26
  return null;
27
27
  };
28
28
 
29
- /**
30
- * Returns a value between 1-100 inclusive.
31
- */
32
- this.getRampNumber = function(deviceId) {
33
- if (!deviceId) {
34
- return 100;
35
- }
36
- var hash = self.generateHash(deviceId);
37
- return Math.abs(hash % 100) + 1;
38
- };
39
-
40
29
  this.invokeCallback = function(
41
30
  callback,
42
31
  code,
@@ -284,39 +273,6 @@ export default function Helpers(mpInstance) {
284
273
  return xhr;
285
274
  };
286
275
 
287
- function generateRandomValue(a) {
288
- var randomValue;
289
- if (window.crypto && window.crypto.getRandomValues) {
290
- randomValue = window.crypto.getRandomValues(new Uint8Array(1)); // eslint-disable-line no-undef
291
- }
292
- if (randomValue) {
293
- return (a ^ (randomValue[0] % 16 >> (a / 4))).toString(16);
294
- }
295
-
296
- return (a ^ ((Math.random() * 16) >> (a / 4))).toString(16);
297
- }
298
-
299
- this.generateUniqueId = function(a) {
300
- // https://gist.github.com/jed/982883
301
- // Added support for crypto for better random
302
-
303
- return a // if the placeholder was passed, return
304
- ? generateRandomValue(a) // a random number
305
- : // or otherwise a concatenated string:
306
- (
307
- [1e7] + // 10000000 +
308
- -1e3 + // -1000 +
309
- -4e3 + // -4000 +
310
- -8e3 + // -80000000 +
311
- -1e11
312
- ) // -100000000000,
313
- .replace(
314
- // replacing
315
- /[018]/g, // zeroes, ones, and eights with
316
- self.generateUniqueId // random hex digits
317
- );
318
- };
319
-
320
276
  this.filterUserIdentities = function(userIdentitiesObject, filterList) {
321
277
  var filteredUserIdentities = [];
322
278
 
@@ -403,37 +359,6 @@ export default function Helpers(mpInstance) {
403
359
  return false;
404
360
  };
405
361
 
406
- this.generateHash = function(name) {
407
- var hash = 0,
408
- i = 0,
409
- character;
410
-
411
- if (name === undefined || name === null) {
412
- return 0;
413
- }
414
-
415
- name = name.toString().toLowerCase();
416
-
417
- if (Array.prototype.reduce) {
418
- return name.split('').reduce(function(a, b) {
419
- a = (a << 5) - a + b.charCodeAt(0);
420
- return a & a;
421
- }, 0);
422
- }
423
-
424
- if (name.length === 0) {
425
- return hash;
426
- }
427
-
428
- for (i = 0; i < name.length; i++) {
429
- character = name.charCodeAt(i);
430
- hash = (hash << 5) - hash + character;
431
- hash = hash & hash;
432
- }
433
-
434
- return hash;
435
- };
436
-
437
362
  this.sanitizeAttributes = function(attrs, name) {
438
363
  if (!attrs || !self.isObject(attrs)) {
439
364
  return null;
@@ -501,6 +426,8 @@ export default function Helpers(mpInstance) {
501
426
  }
502
427
  };
503
428
 
429
+ // TODO: Refactor SDK to directly use these methods
430
+ // https://go.mparticle.com/work/SQDSDKS-5239
504
431
  // Utility Functions
505
432
  this.converted = utils.converted;
506
433
  this.findKeyInObject = utils.findKeyInObject;
@@ -510,6 +437,8 @@ export default function Helpers(mpInstance) {
510
437
  this.decoded = utils.decoded;
511
438
  this.returnConvertedBoolean = utils.returnConvertedBoolean;
512
439
  this.parseStringOrNumber = utils.parseStringOrNumber;
440
+ this.generateHash = utils.generateHash;
441
+ this.generateUniqueId = utils.generateUniqueId;
513
442
 
514
443
  // Imported Validators
515
444
  this.Validators = Validators;
@@ -232,7 +232,6 @@ export interface SDKHelpersApi {
232
232
  generateUniqueId();
233
233
  generateHash?(value: string): string;
234
234
  getFeatureFlag?(feature: string); // TODO: Feature Constants should be converted to enum
235
- getRampNumber?(deviceId: string): number;
236
235
  isDelayedByIntegration?(
237
236
  delayedIntegrations: Dictionary<boolean>,
238
237
  timeoutStart: number,
package/src/utils.ts CHANGED
@@ -36,6 +36,81 @@ const findKeyInObject = (obj: any, key: string): string => {
36
36
  return null;
37
37
  };
38
38
 
39
+ function generateHash(name: string): number {
40
+ let hash: number = 0;
41
+ let character: number;
42
+
43
+ if (name === undefined || name === null) {
44
+ return 0;
45
+ }
46
+
47
+ name = name.toString().toLowerCase();
48
+
49
+ if (Array.prototype.reduce) {
50
+ return name.split('').reduce(function (a: number, b: string) {
51
+ a = (a << 5) - a + b.charCodeAt(0);
52
+ return a & a;
53
+ }, 0);
54
+ }
55
+
56
+ if (name.length === 0) {
57
+ return hash;
58
+ }
59
+
60
+ for (let i = 0; i < name.length; i++) {
61
+ character = name.charCodeAt(i);
62
+ hash = (hash << 5) - hash + character;
63
+ hash = hash & hash;
64
+ }
65
+
66
+ return hash;
67
+ }
68
+
69
+ const generateRandomValue = (value?: string): string => {
70
+ let randomValue: string;
71
+ let a: number;
72
+
73
+ if (window.crypto && window.crypto.getRandomValues) {
74
+ // @ts-ignore
75
+ randomValue = window.crypto.getRandomValues(new Uint8Array(1)); // eslint-disable-line no-undef
76
+ }
77
+ if (randomValue) {
78
+ // @ts-ignore
79
+ return (a ^ (randomValue[0] % 16 >> (a / 4))).toString(16);
80
+ }
81
+
82
+ return (a ^ ((Math.random() * 16) >> (a / 4))).toString(16);
83
+ };
84
+
85
+ const generateUniqueId = (a: string = ''): string =>
86
+ // https://gist.github.com/jed/982883
87
+ // Added support for crypto for better random
88
+
89
+ a // if the placeholder was passed, return
90
+ ? generateRandomValue(a) // if the placeholder was passed, return
91
+ : // [1e7] -> // 10000000 +
92
+ // -1e3 -> // -1000 +
93
+ // -4e3 -> // -4000 +
94
+ // -8e3 -> // -80000000 +
95
+ // -1e11 -> //-100000000000,
96
+ `${1e7}-${1e3}-${4e3}-${8e3}-${1e11}`.replace(
97
+ /[018]/g, // zeroes, ones, and eights with
98
+ generateUniqueId // random hex digits
99
+ );
100
+
101
+ /**
102
+ * Returns a value between 1-100 inclusive.
103
+ */
104
+ const getRampNumber = (value?: string): number => {
105
+ if (!value) {
106
+ return 100;
107
+ }
108
+
109
+ const hash = generateHash(value);
110
+
111
+ return Math.abs(hash % 100) + 1;
112
+ };
113
+
39
114
  const isObject = (value: any): boolean => {
40
115
  var objType = Object.prototype.toString.call(value);
41
116
  return objType === '[object Object]' || objType === '[object Error]';
@@ -76,10 +151,7 @@ const decoded = (s: string): string =>
76
151
 
77
152
  const converted = (s: string): string => {
78
153
  if (s.indexOf('"') === 0) {
79
- s = s
80
- .slice(1, -1)
81
- .replace(/\\"/g, '"')
82
- .replace(/\\\\/g, '\\');
154
+ s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
83
155
  }
84
156
 
85
157
  return s;
@@ -104,6 +176,9 @@ export {
104
176
  converted,
105
177
  decoded,
106
178
  findKeyInObject,
179
+ generateHash,
180
+ generateUniqueId,
181
+ getRampNumber,
107
182
  inArray,
108
183
  isObject,
109
184
  isStringOrNumber,