Skip to content

Commit 272342c

Browse files
committed
address PR comments
1 parent 12da570 commit 272342c

5 files changed

Lines changed: 38 additions & 39 deletions

File tree

src/events.interfaces.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
Callback,
32
SDKEventAttrs,
43
SDKEventOptions,
54
TransactionAttributes,
@@ -16,6 +15,12 @@ import {
1615
import { valueof } from './utils';
1716
import { EventType, ProductActionType, PromotionActionType } from './types';
1817

18+
export type TrackingCallback = ((
19+
position?: GeolocationPosition | {
20+
coords: { latitude: number | string; longitude: number | string };
21+
}
22+
) => void) | null;
23+
1924
// Supports wrapping event handlers functions that will ideally return a specific type
2025
type EventHandlerFunction<T> = (element: HTMLLinkElement | HTMLFormElement) => T;
2126

@@ -79,6 +84,6 @@ export interface IEvents {
7984
attrs?: SDKEventAttrs,
8085
customFlags?: SDKEventCustomFlags
8186
): void;
82-
startTracking(callback: Callback): void;
87+
startTracking(callback: TrackingCallback): void;
8388
stopTracking(): void;
8489
}

src/events.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Types, { EventType, ProductActionType, PromotionActionType } from './types';
22
import Constants from './constants';
3-
import { IEvents } from './events.interfaces';
3+
import { IEvents, TrackingCallback } from './events.interfaces';
44
import { IMParticleWebSDKInstance } from './mp-instance';
55
import {
66
BaseEvent,
@@ -42,7 +42,7 @@ export default function Events(
4242
}
4343
};
4444

45-
this.startTracking = function(callback: ((position?: GeolocationPosition | { coords: { latitude: number | string; longitude: number | string } }) => void) | null): void {
45+
this.startTracking = function(callback: TrackingCallback): void {
4646
if (mpInstance._Store.isTracking) {
4747
const position = {
4848
coords: {
@@ -81,7 +81,7 @@ export default function Events(
8181
}
8282

8383
function triggerCallback(
84-
callback: ((position?: GeolocationPosition | { coords: { latitude: number | string; longitude: number | string } }) => void) | null,
84+
callback: TrackingCallback,
8585
position?: GeolocationPosition | { coords: { latitude: number | string; longitude: number | string } }
8686
): void {
8787
if (callback) {
@@ -194,7 +194,7 @@ export default function Events(
194194
});
195195

196196
if (event) {
197-
// TODO(SDKE-1106): Remove `as Function` casts when ecommerce.js is migrated to TS
197+
// TODO(https://go/j/SDKE-1108): Remove `as Function` casts when ecommerce.js is migrated to TS
198198
event.EventCategory = (mpInstance._Ecommerce.convertProductActionToEventType as Function)(
199199
productActionType
200200
);
@@ -328,13 +328,14 @@ export default function Events(
328328
if (event) {
329329
event.EventName += 'Impression';
330330
event.EventCategory = Types.CommerceEventType.ProductImpression;
331-
const rawList = Array.isArray(impression)
331+
// https://go/j/SDKE-1199
332+
const impressionList: (SDKImpression | SDKProductImpression)[] = Array.isArray(impression)
332333
? impression
333334
: [impression];
334335

335336
event.ProductImpressions = [];
336337

337-
rawList.forEach(function(item) {
338+
impressionList.forEach(function(item) {
338339
if ('Name' in item) {
339340
const imp = item as SDKImpression;
340341
event.ProductImpressions.push({
@@ -412,14 +413,15 @@ export default function Events(
412413
data: ((element: HTMLLinkElement | HTMLFormElement) => SDKEventAttrs) | SDKEventAttrs,
413414
eventType: valueof<typeof EventType>
414415
): void {
415-
let elements: ArrayLike<Element> | Element[] = [],
416-
handler = (e: Event): void => {
417-
const el = element as DOMHandlerElement;
416+
let elements: ArrayLike<Element> | Element[] = [];
417+
let element: DOMHandlerElement;
418+
let elementIndex: number;
419+
const handler = (e: Event): void => {
418420
const timeoutHandler = function(): void {
419-
if (el.href) {
420-
globalThis.location.href = el.href;
421-
} else if (el.submit) {
422-
el.submit();
421+
if (element.href) {
422+
globalThis.location.href = element.href;
423+
} else if (element.submit) {
424+
element.submit();
423425
}
424426
};
425427

@@ -431,16 +433,16 @@ export default function Events(
431433
messageType: Types.MessageType.PageEvent,
432434
name:
433435
typeof eventName === 'function'
434-
? eventName(el as HTMLLinkElement)
436+
? eventName(element as HTMLLinkElement)
435437
: eventName,
436-
data: typeof data === 'function' ? data(el as HTMLLinkElement) : data,
438+
data: typeof data === 'function' ? data(element as HTMLLinkElement) : data,
437439
eventType: (eventType || Types.EventType.Other) as number,
438440
});
439441

440442
// TODO: Handle middle-clicks and special keys (ctrl, alt, etc)
441443
if (
442-
(el.href && el.target !== '_blank') ||
443-
el.submit
444+
(element.href && element.target !== '_blank') ||
445+
element.submit
444446
) {
445447
// Give xmlhttprequest enough time to execute before navigating a link or submitting form
446448

@@ -455,9 +457,7 @@ export default function Events(
455457
mpInstance._Store.SDKConfig.timeout
456458
);
457459
}
458-
},
459-
element: Element,
460-
i: number;
460+
};
461461

462462
if (!selector) {
463463
mpInstance.Logger.error("Can't bind event, selector is required");
@@ -480,16 +480,15 @@ export default function Events(
480480
', attaching event handlers'
481481
);
482482

483-
for (i = 0; i < elements.length; i++) {
484-
element = elements[i];
485-
const el = element as DOMHandlerElement;
483+
for (elementIndex = 0; elementIndex < elements.length; elementIndex++) {
484+
element = elements[elementIndex] as DOMHandlerElement;
486485

487-
if (el.addEventListener) {
488-
el.addEventListener(domEvent, handler, false);
489-
} else if (el.attachEvent) {
490-
el.attachEvent('on' + domEvent, handler);
486+
if (element.addEventListener) {
487+
element.addEventListener(domEvent, handler, false);
488+
} else if (element.attachEvent) {
489+
element.attachEvent('on' + domEvent, handler);
491490
} else {
492-
el['on' + domEvent] = handler;
491+
element['on' + domEvent] = handler;
493492
}
494493
}
495494
} else {

src/sdkRuntimeModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface SDKShoppingCart {
112112
}
113113

114114
export interface SDKPromotionAction {
115-
PromotionActionType: string | valueof<typeof PromotionActionType>;
115+
PromotionActionType: valueof<typeof PromotionActionType>;
116116
PromotionList?: SDKPromotion[];
117117
}
118118

test/src/config/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SDKInitConfig } from "../../../src/sdkRuntimeModels";
1+
import { IMParticleInstanceManager, SDKInitConfig } from "../../../src/sdkRuntimeModels";
22
import { MILLIS_IN_ONE_SEC, ONE_DAY_IN_SECONDS } from "../../../src/constants";
33

44
export const urls = {
@@ -15,7 +15,7 @@ export const urls = {
1515
export const MILLISECONDS_IN_ONE_DAY = ONE_DAY_IN_SECONDS * MILLIS_IN_ONE_SEC
1616
export const MILLISECONDS_IN_ONE_DAY_PLUS_ONE_SECOND = MILLISECONDS_IN_ONE_DAY + 1;
1717

18-
export const mParticle = window.mParticle;
18+
export const mParticle = window.mParticle as IMParticleInstanceManager;
1919

2020
export const apiKey = 'test_key';
2121
export const testMPID = 'testMPID';

test/src/tests-event-logging.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import {
66
urls,
77
apiKey,
88
testMPID,
9+
mParticle,
910
MPConfig,
1011
MessageType,
1112
} from './config/constants';
12-
import { IMParticleInstanceManager } from '../../src/sdkRuntimeModels';
1313

1414
declare global {
15-
interface Window {
16-
mParticle: IMParticleInstanceManager;
17-
}
1815
namespace Should {
1916
interface Assertion {
2017
not: Assertion;
@@ -30,8 +27,6 @@ declare global {
3027
}
3128
}
3229

33-
const mParticle = window.mParticle as IMParticleInstanceManager;
34-
3530
const { findEventFromRequest, findBatch, getIdentityEvent, waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils;
3631

3732
describe('event logging', function() {

0 commit comments

Comments
 (0)