@mparticle/web-sdk 2.79.0 → 2.79.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/dist/mparticle.common.js +6 -6
- package/dist/mparticle.esm.js +6 -6
- package/dist/mparticle.js +35 -34
- package/dist/types/src/ecommerce.interfaces.d.ts +8 -7
- package/dist/types/src/events.d.ts +3 -18
- package/dist/types/src/events.interfaces.d.ts +2 -2
- package/dist/types/src/helpers.d.ts +3 -54
- package/dist/types/src/polyfill.d.ts +13 -14
- package/dist/types/src/sdkRuntimeModels.d.ts +10 -3
- package/dist/types/src/store.d.ts +1 -0
- package/package.json +1 -1
- package/src/ecommerce.interfaces.ts +12 -8
- package/src/events.interfaces.ts +3 -3
- package/src/{events.js → events.ts} +152 -82
- package/src/{helpers.js → helpers.ts} +117 -63
- package/src/{polyfill.js → polyfill.ts} +36 -36
- package/src/sdkRuntimeModels.ts +19 -3
- package/src/store.ts +1 -0
|
@@ -1,16 +1,53 @@
|
|
|
1
|
-
import 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
|
-
|
|
38
|
+
const Messages = Constants.Messages;
|
|
5
39
|
|
|
6
|
-
export default function Events(
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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(
|
|
115
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
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(
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
368
|
-
commerceEvent.EventAttributes =
|
|
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
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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:
|
|
418
|
-
|
|
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 {
|