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
52 lines (48 loc) · 1.44 KB
/
Copy pathcore.ts
File metadata and controls
52 lines (48 loc) · 1.44 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
import { buildSelfDescribingEvent } from '@snowplow/tracker-core';
import { CONSENT_SCHEMA, CMP_VISIBLE_SCHEMA } from './schemata';
import { CmpVisible, Consent } from './types';
/**
* Build a Consent Action Event
* for tracking consent actions
*
* @param event - Contains properties of Consent action events
* @returns PayloadBuilder to be sent to {@link @snowplow/tracker-core#TrackerCore.track}
*/
export function buildConsentEvent(event: Consent) {
return buildSelfDescribingEvent({
event: {
schema: CONSENT_SCHEMA,
data: removeEmptyProperties({ ...event }),
},
});
}
/**
* Build a CMP Visible Event
* for tracking loadtime of CMP banner
*
* @param event - Contains properties of CMP Visible events
* @returns PayloadBuilder to be sent to {@link @snowplow/tracker-core#TrackerCore.track}
*/
export function buildCmpVisibleEvent(event: CmpVisible) {
return buildSelfDescribingEvent({
event: {
schema: CMP_VISIBLE_SCHEMA,
data: removeEmptyProperties({ ...event }),
},
});
}
/**
* 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 && typeof event[k] !== 'undefined') {
ret[k] = event[k];
}
}
return ret;
}