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
65 lines (58 loc) · 1.74 KB
/
Copy pathcore.ts
File metadata and controls
65 lines (58 loc) · 1.74 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
import { SelfDescribingJson } from '@snowplow/tracker-core';
import {
getMediaEventSchema,
MEDIA_AD_BREAK_SCHEMA,
MEDIA_AD_SCHEMA,
MEDIA_PLAYER_SCHEMA,
MEDIA_SESSION_SCHEMA,
} from './schemata';
import { MediaPlayer, MediaAd, MediaAdBreak, MediaSession, MediaEvent } from './types';
export function buildMediaPlayerEvent(event: MediaEvent): SelfDescribingJson {
return {
schema: getMediaEventSchema(event.type),
data: removeEmptyProperties(event.eventBody ?? {}),
};
}
export function buildMediaPlayerEntity(mediaPlayer: MediaPlayer): SelfDescribingJson {
const player = { ...mediaPlayer };
if (player.label === '') delete player.label;
if (player.playerType === '') delete player.playerType;
if (player.quality === '') delete player.quality;
return {
schema: MEDIA_PLAYER_SCHEMA,
data: removeEmptyProperties(player),
};
}
export function buildMediaSessionEntity(session: MediaSession): SelfDescribingJson {
return {
schema: MEDIA_SESSION_SCHEMA,
data: removeEmptyProperties(session),
};
}
export function buildMediaAdEntity(ad: MediaAd): SelfDescribingJson {
return {
schema: MEDIA_AD_SCHEMA,
data: removeEmptyProperties(ad),
};
}
export function buildMediaAdBreakEntity(adBreak: MediaAdBreak): SelfDescribingJson {
return {
schema: MEDIA_AD_BREAK_SCHEMA,
data: removeEmptyProperties(adBreak),
};
}
/**
* Returns a copy of a JSON with undefined and null properties removed
*
* @param event - Object to clean
* @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) {
ret[k] = event[k];
}
}
return ret;
}