-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMetricsEventBuilder.ts
168 lines (145 loc) · 4.1 KB
/
MetricsEventBuilder.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
IMetaMetricsEvent,
ITrackingEvent,
JsonMap,
isTrackingEvent,
} from './MetaMetrics.types';
/**
* the event tracking object to be produced by MetricsEventBuilder
*/
class TrackingEvent implements ITrackingEvent {
readonly #name: string;
#properties: JsonMap;
#sensitiveProperties: JsonMap;
#saveDataRecording: boolean;
constructor(event: IMetaMetricsEvent) {
this.#name = event.category;
this.#properties = {};
this.#sensitiveProperties = {};
this.#saveDataRecording = true;
}
get name(): string {
return this.#name;
}
get properties(): JsonMap {
return this.#properties;
}
set properties(properties: JsonMap) {
this.#properties = properties;
}
get sensitiveProperties(): JsonMap {
return this.#sensitiveProperties;
}
set sensitiveProperties(sensitiveProperties: JsonMap) {
this.#sensitiveProperties = sensitiveProperties;
}
get saveDataRecording(): boolean {
return this.#saveDataRecording;
}
set saveDataRecording(saveDataRecording: boolean) {
this.#saveDataRecording = saveDataRecording;
}
get isAnonymous(): boolean {
return !!(
this.#sensitiveProperties && Object.keys(this.#sensitiveProperties).length
);
}
get hasProperties(): boolean {
return !!(
(this.#properties && Object.keys(this.#properties).length) ||
(this.#sensitiveProperties &&
Object.keys(this.#sensitiveProperties).length)
);
}
}
/**
* MetricsEventBuilder
*
* This class is used to build events for tracking
* It allows to add properties and sensitive properties to the event
* and to track the event
* @example
* import { useMetrics } from '../../../components/hooks/useMetrics';
*
* const { trackEvent, createEventBuilder } = useMetrics();
*
* trackEvent(createEventBuilder(MetaMetricsEvents.MY_EVENT)
* .addProperties({ normalProp: 'value' })
* .addSensitiveProperties({ sensitiveProp: 'value' })
* .build());
*/
class MetricsEventBuilder {
readonly #trackingEvent: ITrackingEvent;
constructor(event: IMetaMetricsEvent | ITrackingEvent) {
if (isTrackingEvent(event)) {
this.#trackingEvent = event;
return;
}
this.#trackingEvent = new TrackingEvent(event);
}
/**
* Create a new event builder
* @param event the event for the builder to build
*/
static createEventBuilder(event: IMetaMetricsEvent | ITrackingEvent) {
return new MetricsEventBuilder(event);
}
/**
* Add regular properties (non-anonymous) to the event
* @param properties a map of properties to add to the event
*/
addProperties(properties: JsonMap) {
this.#trackingEvent.properties = {
...this.#trackingEvent.properties,
...properties,
};
return this;
}
/**
* Add sensitive properties (anonymous) to the event
* @param properties a map of properties to add to the event
*/
addSensitiveProperties(properties: JsonMap) {
this.#trackingEvent.sensitiveProperties = {
...this.#trackingEvent.sensitiveProperties,
...properties,
};
return this;
}
#removeProperties = (map: JsonMap, keys: string[]) => {
for (const key of keys) {
delete map[key];
}
};
/**
* Remove one or more non-anonymous properties from the event
* @param propNames an array of property names to remove from the event
*/
removeProperties(propNames: string[]) {
this.#removeProperties(this.#trackingEvent.properties, propNames);
return this;
}
/**
* Remove one or more non-anonymous properties from the event
* @param propNames an array of property names to remove from the event
*/
removeSensitiveProperties(propNames: string[]) {
this.#removeProperties(this.#trackingEvent.sensitiveProperties, propNames);
return this;
}
/**
* Set the saveDataRecording flag
* @param saveDataRecording the value to set the flag to, default is true
*/
setSaveDataRecording(saveDataRecording: boolean) {
this.#trackingEvent.saveDataRecording = saveDataRecording;
return this;
}
/**
* Build the event
*/
build(): ITrackingEvent {
return this.#trackingEvent;
}
}
export { MetricsEventBuilder };