@mparticle/web-sdk 2.79.0 → 2.79.2

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.
@@ -186,7 +186,7 @@ export interface MParticleWebSDK {
186
186
  setOptOut(isOptingOut: boolean): void;
187
187
  startTrackingLocation(callback?: Callback): void;
188
188
  stopTrackingLocation(): void;
189
- generateHash(value: string): string;
189
+ generateHash(value: string): number;
190
190
  setIntegrationAttribute(integrationModuleId: number, attrs: IntegrationAttribute): void;
191
191
  getIntegrationAttributes(integrationModuleId: number): IntegrationAttribute;
192
192
  captureTiming(metricName: string): void;
@@ -262,9 +262,16 @@ export interface SDKHelpersApi {
262
262
  findKeyInObject?(obj: any, key: string): string;
263
263
  parseNumber?(value: string | number): number;
264
264
  generateUniqueId(): any;
265
- generateHash?(value: string): string;
265
+ generateHash?(value: string): number;
266
266
  getFeatureFlag?(feature: string): boolean | string;
267
- invokeAliasCallback(aliasCallback: IAliasCallback, number: number, errorMessage: string): void;
267
+ decoded?(s: string): string;
268
+ parseStringOrNumber?(value: string | number): string | number | null;
269
+ inArray?(items: any[], value: any): boolean;
270
+ converted?(s: string): string;
271
+ filterUserIdentitiesForForwarders?(userIdentities: Dictionary, filterList: number[]): Dictionary;
272
+ filterUserAttributes?(userAttributes: Dictionary, filterList: number[]): Dictionary;
273
+ filterUserIdentities?(userIdentitiesObject: Dictionary<string>, filterList: number[]): ISDKUserIdentity[];
274
+ invokeAliasCallback(aliasCallback: IAliasCallback, number: number, errorMessage?: string): void;
268
275
  isDelayedByIntegration?(delayedIntegrations: Dictionary<boolean>, timeoutStart: number, now: number): boolean;
269
276
  isEventType?(type: valueof<typeof EventType>): boolean;
270
277
  isObject?(item: any): any;
@@ -47,6 +47,7 @@ export interface SDKConfig {
47
47
  maxProducts: number;
48
48
  requestConfig?: boolean;
49
49
  sessionTimeout?: number;
50
+ timeout?: number;
50
51
  useNativeSdk?: boolean;
51
52
  useCookieStorage?: boolean;
52
53
  v1SecureServiceUrl?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-sdk",
3
- "version": "2.79.0",
3
+ "version": "2.79.2",
4
4
  "description": "mParticle core SDK for web applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -13,15 +13,13 @@ import { valueof } from './utils';
13
13
  import {
14
14
  ProductActionType,
15
15
  PromotionActionType,
16
- CommerceEventType,
17
- EventType,
18
16
  } from './types';
19
17
  import {
20
18
  SDKEvent,
21
19
  SDKEventCustomFlags,
22
20
  SDKImpression,
23
21
  SDKProduct,
24
- SDKProductImpression,
22
+ SDKProductAction,
25
23
  SDKPromotion,
26
24
  } from './sdkRuntimeModels';
27
25
 
@@ -71,7 +69,7 @@ export interface SDKECommerceAPI extends IECommerceShared {
71
69
  customFlags?: SDKEventCustomFlags
72
70
  ): void;
73
71
  logImpression(
74
- impression: SDKProductImpression,
72
+ impression: SDKImpression | SDKImpression[],
75
73
  attrs?: SDKEventAttrs,
76
74
  customFlags?: SDKEventCustomFlags,
77
75
  eventOptions?: SDKEventOptions
@@ -155,17 +153,23 @@ interface ExtractedTransactionId {
155
153
 
156
154
  // Used for the private `_Ecommerce` namespace
157
155
  export interface IECommerce extends IECommerceShared {
158
- buildProductList(event: SDKEvent, product: Product | Product[]): Product[];
156
+ buildProductList(
157
+ event: SDKEvent,
158
+ product: SDKProduct | SDKProduct[]
159
+ ): SDKProduct[];
159
160
  convertProductActionToEventType(
160
161
  productActionType: valueof<typeof ProductActionType>
161
162
  ): // https://go.mparticle.com/work/SQDSDKS-4801
162
- typeof CommerceEventType | typeof EventType | null;
163
+ number | null;
163
164
  convertPromotionActionToEventType(
164
165
  promotionActionType: valueof<typeof PromotionActionType>
165
- ): typeof CommerceEventType | null;
166
+ ): number | null;
167
+ calculateProductActionTotalAmount(
168
+ productAction: SDKProductAction
169
+ ): SDKProductAction;
166
170
  convertTransactionAttributesToProductAction(
167
171
  transactionAttributes: TransactionAttributes,
168
- productAction: ProductAction
172
+ productAction: SDKProductAction
169
173
  ): void;
170
174
  createCommerceEventObject(
171
175
  customFlags: SDKEventCustomFlags,
@@ -8,8 +8,8 @@ import {
8
8
  BaseEvent,
9
9
  SDKEvent,
10
10
  SDKEventCustomFlags,
11
+ SDKImpression,
11
12
  SDKProduct,
12
- SDKProductImpression,
13
13
  SDKPromotion,
14
14
  } from './sdkRuntimeModels';
15
15
  import { valueof } from './utils';
@@ -45,11 +45,11 @@ export interface IEvents {
45
45
  ): void;
46
46
  logEvent(event: BaseEvent, eventOptions?: SDKEventOptions): void;
47
47
  logImpressionEvent(
48
- impression: SDKProductImpression,
48
+ impression: SDKImpression | SDKImpression[],
49
49
  attrs?: SDKEventAttrs,
50
50
  customFlags?: SDKEventCustomFlags,
51
51
  eventOptions?: SDKEventOptions
52
- );
52
+ ): void;
53
53
  logOptOut(): void;
54
54
  logPageView(): void;
55
55
  logProductActionEvent(
@@ -1,16 +1,53 @@
1
- import Types from './types';
1
+ import Types, {
2
+ EventType,
3
+ ProductActionType,
4
+ PromotionActionType,
5
+ } from './types';
2
6
  import Constants from './constants';
7
+ import { IEvents } from './events.interfaces';
8
+ import { IMParticleWebSDKInstance } from './mp-instance';
9
+ import {
10
+ BaseEvent,
11
+ SDKEvent,
12
+ SDKEventCustomFlags,
13
+ SDKImpression,
14
+ SDKProduct,
15
+ SDKProductActionType,
16
+ SDKPromotion,
17
+ } from './sdkRuntimeModels';
18
+ import {
19
+ SDKEventAttrs,
20
+ SDKEventOptions,
21
+ TransactionAttributes,
22
+ } from '@mparticle/web-sdk';
23
+ import { valueof } from './utils';
24
+
25
+ interface DOMHandlerElement extends HTMLElement {
26
+ href?: string;
27
+ target?: string;
28
+ submit?: () => void;
29
+ attachEvent?: (event: string, handler: EventListener) => void;
30
+ }
31
+
32
+ type TrackingCallback = (
33
+ location?: {
34
+ coords: { latitude: number | string; longitude: number | string };
35
+ }
36
+ ) => void;
3
37
 
4
- var Messages = Constants.Messages;
38
+ const Messages = Constants.Messages;
5
39
 
6
- export default function Events(mpInstance) {
7
- var self = this;
8
- this.logEvent = function(event, options) {
40
+ export default function Events(
41
+ this: IEvents,
42
+ mpInstance: IMParticleWebSDKInstance
43
+ ): void {
44
+ const self = this;
45
+ this.logEvent = function(event: BaseEvent, options?: SDKEventOptions): void {
9
46
  mpInstance.Logger.verbose(
10
47
  Messages.InformationMessages.StartingLogEvent + ': ' + event.name
11
48
  );
12
49
  if (mpInstance._Helpers.canLog()) {
13
- var uploadObject = mpInstance._ServerModel.createEventObject(event);
50
+ const uploadObject = mpInstance._ServerModel.createEventObject(event);
14
51
  mpInstance._APIClient.sendEventToServer(uploadObject, options);
15
52
  } else {
16
53
  mpInstance.Logger.verbose(
@@ -19,7 +56,7 @@ export default function Events(mpInstance) {
19
56
  }
20
57
  };
21
58
 
22
- this.startTracking = function(callback) {
59
+ this.startTracking = function(callback: TrackingCallback): void {
23
60
  if (!mpInstance._Store.isTracking) {
24
61
  if ('geolocation' in navigator) {
25
62
  mpInstance._Store.watchPositionId = navigator.geolocation.watchPosition(
@@ -28,7 +65,7 @@ export default function Events(mpInstance) {
28
65
  );
29
66
  }
30
67
  } else {
31
- var position = {
68
+ const position = {
32
69
  coords: {
33
70
  latitude: mpInstance._Store.currentPosition.lat,
34
71
  longitude: mpInstance._Store.currentPosition.lng,
@@ -37,7 +74,7 @@ export default function Events(mpInstance) {
37
74
  triggerCallback(callback, position);
38
75
  }
39
76
 
40
- function successTracking(position) {
77
+ function successTracking(position: GeolocationPosition): void {
41
78
  mpInstance._Store.currentPosition = {
42
79
  lat: position.coords.latitude,
43
80
  lng: position.coords.longitude,
@@ -50,14 +87,22 @@ export default function Events(mpInstance) {
50
87
  mpInstance._Store.isTracking = true;
51
88
  }
52
89
 
53
- function errorTracking() {
90
+ function errorTracking(): void {
54
91
  triggerCallback(callback);
55
92
  // prevents callback from being fired multiple times
56
93
  callback = null;
57
94
  mpInstance._Store.isTracking = false;
58
95
  }
59
96
 
60
- function triggerCallback(callback, position) {
97
+ function triggerCallback(
98
+ callback: TrackingCallback | null,
99
+ position?: {
100
+ coords: {
101
+ latitude: number | string;
102
+ longitude: number | string;
103
+ };
104
+ } | GeolocationPosition
105
+ ): void {
61
106
  if (callback) {
62
107
  try {
63
108
  if (position) {
@@ -69,13 +114,13 @@ export default function Events(mpInstance) {
69
114
  mpInstance.Logger.error(
70
115
  'Error invoking the callback passed to startTrackingLocation.'
71
116
  );
72
- mpInstance.Logger.error(e);
117
+ mpInstance.Logger.error(e as string);
73
118
  }
74
119
  }
75
120
  }
76
121
  };
77
122
 
78
- this.stopTracking = function() {
123
+ this.stopTracking = function(): void {
79
124
  if (mpInstance._Store.isTracking) {
80
125
  navigator.geolocation.clearWatch(mpInstance._Store.watchPositionId);
81
126
  mpInstance._Store.currentPosition = null;
@@ -83,23 +128,23 @@ export default function Events(mpInstance) {
83
128
  }
84
129
  };
85
130
 
86
- this.logOptOut = function() {
131
+ this.logOptOut = function(): void {
87
132
  mpInstance.Logger.verbose(
88
133
  Messages.InformationMessages.StartingLogOptOut
89
134
  );
90
135
 
91
- var event = mpInstance._ServerModel.createEventObject({
136
+ const event = mpInstance._ServerModel.createEventObject({
92
137
  messageType: Types.MessageType.OptOut,
93
138
  eventType: Types.EventType.Other,
94
139
  });
95
140
  mpInstance._APIClient.sendEventToServer(event);
96
141
  };
97
142
 
98
- this.logAST = function() {
143
+ this.logAST = function(): void {
99
144
  self.logEvent({ messageType: Types.MessageType.AppStateTransition });
100
145
  };
101
146
 
102
- this.logPageView = function() {
147
+ this.logPageView = function(): void {
103
148
  self.logEvent({
104
149
  messageType: Types.MessageType.PageView,
105
150
  name: 'PageView',
@@ -111,8 +156,13 @@ export default function Events(mpInstance) {
111
156
  });
112
157
  };
113
158
 
114
- this.logCheckoutEvent = function(step, option, attrs, customFlags) {
115
- var event = mpInstance._Ecommerce.createCommerceEventObject(
159
+ this.logCheckoutEvent = function(
160
+ step: number,
161
+ option?: string,
162
+ attrs?: SDKEventAttrs,
163
+ customFlags?: SDKEventCustomFlags
164
+ ): void {
165
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
116
166
  customFlags
117
167
  );
118
168
 
@@ -122,7 +172,8 @@ export default function Events(mpInstance) {
122
172
  );
123
173
  event.EventCategory = Types.CommerceEventType.ProductCheckout;
124
174
  event.ProductAction = {
125
- ProductActionType: Types.ProductActionType.Checkout,
175
+ ProductActionType: Types.ProductActionType
176
+ .Checkout as SDKProductActionType,
126
177
  CheckoutStep: step,
127
178
  CheckoutOptions: option,
128
179
  ProductList: [],
@@ -133,19 +184,19 @@ export default function Events(mpInstance) {
133
184
  };
134
185
 
135
186
  this.logProductActionEvent = function(
136
- productActionType,
137
- product,
138
- customAttrs,
139
- customFlags,
140
- transactionAttributes,
141
- options
142
- ) {
143
- var event = mpInstance._Ecommerce.createCommerceEventObject(
187
+ productActionType: valueof<typeof ProductActionType>,
188
+ product: SDKProduct | SDKProduct[],
189
+ customAttrs?: SDKEventAttrs,
190
+ customFlags?: SDKEventCustomFlags,
191
+ transactionAttributes?: TransactionAttributes,
192
+ options?: SDKEventOptions
193
+ ): void {
194
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
144
195
  customFlags,
145
196
  options
146
197
  );
147
198
 
148
- var productList = Array.isArray(product) ? product : [product];
199
+ const productList = Array.isArray(product) ? product : [product];
149
200
 
150
201
  productList.forEach(function(product) {
151
202
  if (product.TotalAmount) {
@@ -182,15 +233,21 @@ export default function Events(mpInstance) {
182
233
  productActionType
183
234
  );
184
235
  event.ProductAction = {
185
- ProductActionType: productActionType,
236
+ ProductActionType: productActionType as SDKProductActionType,
186
237
  ProductList: productList,
187
238
  };
188
239
 
189
- if (Types.ProductActionType.isRoktCommerceType(productActionType)) {
240
+ if (
241
+ Types.ProductActionType.isRoktCommerceType(
242
+ productActionType as number
243
+ )
244
+ ) {
190
245
  event.CustomFlags = event.CustomFlags || {};
191
246
  event.CustomFlags[
192
247
  'Rokt.CommerceEventType'
193
- ] = Types.ProductActionType.getExpansionName(productActionType);
248
+ ] = Types.ProductActionType.getExpansionName(
249
+ productActionType as number
250
+ );
194
251
  }
195
252
 
196
253
  if (mpInstance._Helpers.isObject(transactionAttributes)) {
@@ -205,12 +262,12 @@ export default function Events(mpInstance) {
205
262
  };
206
263
 
207
264
  this.logPurchaseEvent = function(
208
- transactionAttributes,
209
- product,
210
- attrs,
211
- customFlags
212
- ) {
213
- var event = mpInstance._Ecommerce.createCommerceEventObject(
265
+ transactionAttributes: TransactionAttributes,
266
+ product: SDKProduct | SDKProduct[],
267
+ attrs?: SDKEventAttrs,
268
+ customFlags?: SDKEventCustomFlags
269
+ ): void {
270
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
214
271
  customFlags
215
272
  );
216
273
 
@@ -220,7 +277,8 @@ export default function Events(mpInstance) {
220
277
  );
221
278
  event.EventCategory = Types.CommerceEventType.ProductPurchase;
222
279
  event.ProductAction = {
223
- ProductActionType: Types.ProductActionType.Purchase,
280
+ ProductActionType: Types.ProductActionType
281
+ .Purchase as SDKProductActionType,
224
282
  };
225
283
  event.ProductAction.ProductList = mpInstance._Ecommerce.buildProductList(
226
284
  event,
@@ -237,17 +295,17 @@ export default function Events(mpInstance) {
237
295
  };
238
296
 
239
297
  this.logRefundEvent = function(
240
- transactionAttributes,
241
- product,
242
- attrs,
243
- customFlags
244
- ) {
298
+ transactionAttributes: TransactionAttributes,
299
+ product: SDKProduct | SDKProduct[],
300
+ attrs?: SDKEventAttrs,
301
+ customFlags?: SDKEventCustomFlags
302
+ ): void {
245
303
  if (!transactionAttributes) {
246
304
  mpInstance.Logger.error(Messages.ErrorMessages.TransactionRequired);
247
305
  return;
248
306
  }
249
307
 
250
- var event = mpInstance._Ecommerce.createCommerceEventObject(
308
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
251
309
  customFlags
252
310
  );
253
311
 
@@ -257,7 +315,8 @@ export default function Events(mpInstance) {
257
315
  );
258
316
  event.EventCategory = Types.CommerceEventType.ProductRefund;
259
317
  event.ProductAction = {
260
- ProductActionType: Types.ProductActionType.Refund,
318
+ ProductActionType: Types.ProductActionType
319
+ .Refund as SDKProductActionType,
261
320
  };
262
321
  event.ProductAction.ProductList = mpInstance._Ecommerce.buildProductList(
263
322
  event,
@@ -274,13 +333,13 @@ export default function Events(mpInstance) {
274
333
  };
275
334
 
276
335
  this.logPromotionEvent = function(
277
- promotionType,
278
- promotion,
279
- attrs,
280
- customFlags,
281
- eventOptions
282
- ) {
283
- var event = mpInstance._Ecommerce.createCommerceEventObject(
336
+ promotionType: valueof<typeof PromotionActionType>,
337
+ promotion: SDKPromotion,
338
+ attrs?: SDKEventAttrs,
339
+ customFlags?: SDKEventCustomFlags,
340
+ eventOptions?: SDKEventOptions
341
+ ): void {
342
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
284
343
  customFlags
285
344
  );
286
345
 
@@ -292,7 +351,7 @@ export default function Events(mpInstance) {
292
351
  promotionType
293
352
  );
294
353
  event.PromotionAction = {
295
- PromotionActionType: promotionType,
354
+ PromotionActionType: promotionType as unknown as string,
296
355
  PromotionList: Array.isArray(promotion)
297
356
  ? promotion
298
357
  : [promotion],
@@ -303,12 +362,12 @@ export default function Events(mpInstance) {
303
362
  };
304
363
 
305
364
  this.logImpressionEvent = function(
306
- impression,
307
- attrs,
308
- customFlags,
309
- options
310
- ) {
311
- var event = mpInstance._Ecommerce.createCommerceEventObject(
365
+ impression: SDKImpression | SDKImpression[],
366
+ attrs?: SDKEventAttrs,
367
+ customFlags?: SDKEventCustomFlags,
368
+ options?: SDKEventOptions
369
+ ): void {
370
+ const event = mpInstance._Ecommerce.createCommerceEventObject(
312
371
  customFlags
313
372
  );
314
373
 
@@ -334,7 +393,11 @@ export default function Events(mpInstance) {
334
393
  }
335
394
  };
336
395
 
337
- this.logCommerceEvent = function(commerceEvent, attrs, options) {
396
+ this.logCommerceEvent = function(
397
+ commerceEvent: SDKEvent,
398
+ attrs?: SDKEventAttrs,
399
+ options?: SDKEventOptions
400
+ ): void {
338
401
  mpInstance.Logger.verbose(
339
402
  Messages.InformationMessages.StartingLogCommerceEvent
340
403
  );
@@ -353,7 +416,7 @@ export default function Events(mpInstance) {
353
416
  return;
354
417
  }
355
418
 
356
- attrs = mpInstance._Helpers.sanitizeAttributes(
419
+ const sanitizedAttrs = mpInstance._Helpers.sanitizeAttributes(
357
420
  attrs,
358
421
  commerceEvent.EventName
359
422
  );
@@ -364,8 +427,8 @@ export default function Events(mpInstance) {
364
427
  commerceEvent.ShoppingCart = {};
365
428
  }
366
429
 
367
- if (attrs) {
368
- commerceEvent.EventAttributes = attrs;
430
+ if (sanitizedAttrs) {
431
+ commerceEvent.EventAttributes = sanitizedAttrs;
369
432
  }
370
433
 
371
434
  // When no transaction total (Revenue) was provided, derive it from
@@ -388,15 +451,19 @@ export default function Events(mpInstance) {
388
451
  };
389
452
 
390
453
  this.addEventHandler = function(
391
- domEvent,
392
- selector,
393
- eventName,
394
- data,
395
- eventType
396
- ) {
397
- var elements = [],
398
- handler = function(e) {
399
- var timeoutHandler = function() {
454
+ domEvent: string,
455
+ selector: string | Node,
456
+ eventName:
457
+ | ((element: HTMLLinkElement | HTMLFormElement) => string)
458
+ | string,
459
+ data:
460
+ | ((element: HTMLLinkElement | HTMLFormElement) => SDKEventAttrs)
461
+ | SDKEventAttrs,
462
+ eventType: valueof<typeof EventType>
463
+ ): void {
464
+ let elements: ArrayLike<Element> | Element[] = [],
465
+ handler = function(e: Event): void {
466
+ const timeoutHandler = function(): void {
400
467
  if (element.href) {
401
468
  window.location.href = element.href;
402
469
  } else if (element.submit) {
@@ -412,10 +479,13 @@ export default function Events(mpInstance) {
412
479
  messageType: Types.MessageType.PageEvent,
413
480
  name:
414
481
  typeof eventName === 'function'
415
- ? eventName(element)
482
+ ? eventName(element as HTMLLinkElement | HTMLFormElement)
416
483
  : eventName,
417
- data: typeof data === 'function' ? data(element) : data,
418
- eventType: eventType || Types.EventType.Other,
484
+ data:
485
+ typeof data === 'function'
486
+ ? data(element as HTMLLinkElement | HTMLFormElement)
487
+ : data,
488
+ eventType: (eventType || Types.EventType.Other) as number,
419
489
  });
420
490
 
421
491
  // TODO: Handle middle-clicks and special keys (ctrl, alt, etc)
@@ -428,7 +498,7 @@ export default function Events(mpInstance) {
428
498
  if (e.preventDefault) {
429
499
  e.preventDefault();
430
500
  } else {
431
- e.returnValue = false;
501
+ (e as any).returnValue = false;
432
502
  }
433
503
 
434
504
  setTimeout(
@@ -437,8 +507,8 @@ export default function Events(mpInstance) {
437
507
  );
438
508
  }
439
509
  },
440
- element,
441
- i;
510
+ element: DOMHandlerElement,
511
+ i: number;
442
512
 
443
513
  if (!selector) {
444
514
  mpInstance.Logger.error("Can't bind event, selector is required");
@@ -449,7 +519,7 @@ export default function Events(mpInstance) {
449
519
  if (typeof selector === 'string') {
450
520
  elements = document.querySelectorAll(selector);
451
521
  } else if (selector.nodeType) {
452
- elements = [selector];
522
+ elements = [selector as Element];
453
523
  }
454
524
 
455
525
  if (elements.length) {
@@ -462,7 +532,7 @@ export default function Events(mpInstance) {
462
532
  );
463
533
 
464
534
  for (i = 0; i < elements.length; i++) {
465
- element = elements[i];
535
+ element = elements[i] as DOMHandlerElement;
466
536
 
467
537
  if (element.addEventListener) {
468
538
  // Modern browsers
@@ -472,7 +542,7 @@ export default function Events(mpInstance) {
472
542
  element.attachEvent('on' + domEvent, handler);
473
543
  } else {
474
544
  // All other browsers
475
- element['on' + domEvent] = handler;
545
+ (element as any)['on' + domEvent] = handler;
476
546
  }
477
547
  }
478
548
  } else {