forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ts
More file actions
70 lines (63 loc) · 2 KB
/
core.ts
File metadata and controls
70 lines (63 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { buildSelfDescribingEvent } from '@snowplow/tracker-core';
import { LIST_ITEM_VIEW_EVENT_SCHEMA, SCREEN_END_EVENT_SCHEMA, SCREEN_ENTITY_SCHEMA, SCREEN_SUMMARY_ENTITY_SCHEMA, SCREEN_VIEW_EVENT_SCHEMA, SCROLL_CHANGED_EVENT_SCHEMA } from './schemata';
import { ListItemViewProps, ScreenProps, ScreenSummaryProps, ScreenViewProps, ScrollChangedProps } from './types';
export function buildScreenViewEvent(event: ScreenViewProps) {
return buildSelfDescribingEvent({
event: {
schema: SCREEN_VIEW_EVENT_SCHEMA,
data: removeEmptyProperties({ ...event }),
},
});
}
export function buildScreenEndEvent() {
return buildSelfDescribingEvent({
event: {
schema: SCREEN_END_EVENT_SCHEMA,
data: {},
},
});
}
export function buildListItemViewEvent(event: ListItemViewProps) {
return buildSelfDescribingEvent({
event: {
schema: LIST_ITEM_VIEW_EVENT_SCHEMA,
data: removeEmptyProperties({ ...event }),
},
});
}
export function buildScrollChangedEvent(event: ScrollChangedProps) {
return buildSelfDescribingEvent({
event: {
schema: SCROLL_CHANGED_EVENT_SCHEMA,
data: removeEmptyProperties({ ...event }),
},
});
}
export function buildScreenEntity(entity: ScreenProps) {
return {
schema: SCREEN_ENTITY_SCHEMA,
data: removeEmptyProperties({ ...entity }),
};
}
export function buildScreenSummaryEntity(entity: ScreenSummaryProps) {
return {
schema: SCREEN_SUMMARY_ENTITY_SCHEMA,
data: removeEmptyProperties({ ...entity }),
};
}
/**
* Returns a copy of a JSON with undefined and null properties removed
*
* @param event - Object to clean
* @param exemptFields - Set of fields which should not be removed even if empty
* @returns A cleaned copy of eventJson
*/
function removeEmptyProperties(event: Record<string, unknown>): Record<string, unknown> {
const ret: Record<string, unknown> = {};
for (const k in event) {
if (event[k] !== null && typeof event[k] !== 'undefined') {
ret[k] = event[k];
}
}
return ret;
}