-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
51 lines (47 loc) · 2.19 KB
/
index.js
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
/**
* Some events will always create very large numbers of flattened properties
* This is undesirable since a large enough number of properties for a particular team can slow down the property filter in the UI
* If the problem property has a unique enough name it can be added to the propertyDenyList
* If not (or if many properties for a given event are problematic) then the event can be added here
*/
const eventDenyList = ['$autocapture', 'organization usage report']
async function processEvent(event, { config }) {
try {
if (!eventDenyList.includes(event.event) && event.properties) {
event.properties = flattenProperties(event.properties, config.separator)
}
} catch (e) {
throw e
}
return event
}
const propertyDenyList = ['$elements', '$elements_chain', '$groups', '$active_feature_flags', '$heatmap_data', '$web_vitals_data']
const flattenProperties = (props, sep, nestedChain = []) => {
let newProps = {}
for (const [key, value] of Object.entries(props)) {
if (propertyDenyList.includes(key)) {
// Hide 'internal' properties used in event processing
} else if (key === '$set') {
newProps = { ...newProps, $set: { ...props[key], ...flattenProperties(props[key], sep) } }
} else if (key === '$set_once') {
newProps = { ...newProps, $set_once: { ...props[key], ...flattenProperties(props[key], sep) } }
} else if (key === '$group_set') {
newProps = { ...newProps, $group_set: { ...props[key], ...flattenProperties(props[key], sep) } }
} else if (Array.isArray(value)) {
newProps = { ...newProps, ...flattenProperties(props[key], sep, [...nestedChain, key]) }
} else if (value !== null && typeof value === 'object' && Object.keys(value).length > 0) {
newProps = { ...newProps, ...flattenProperties(props[key], sep, [...nestedChain, key]) }
} else {
if (nestedChain.length > 0) {
newProps[nestedChain.join(sep) + `${sep}${key}`] = value
}
}
}
if (nestedChain.length > 0) {
return { ...newProps }
}
return { ...props, ...newProps }
}
module.exports = {
processEvent
}