Skip to content

Commit beba5f6

Browse files
committed
feat: Refactor analytics event handler and update CHANGELOG
1 parent d2a3199 commit beba5f6

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
- Add `defineJsonSecret` API for storing structured JSON objects in Cloud Secret Manager
1+
# Change Log
2+
3+
## Unreleased
4+
5+
- Add `defineJsonSecret` API for storing structured JSON objects in Cloud Secret Manager (#1745)
6+
- Refactored analytics event handler to improve readability and maintainability (#1738)

src/v1/providers/analytics.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ export class AnalyticsEvent {
135135
}
136136
}
137137

138+
/**
139+
* @hidden
140+
*/
141+
function isValidUserProperty(property: unknown): property is { value: unknown } {
142+
if (property == null || typeof property !== "object" || !("value" in property)) {
143+
return false;
144+
}
145+
146+
const { value } = property;
147+
if (value == null) {
148+
return false;
149+
}
150+
151+
if (typeof value === "object" && Object.keys(value).length === 0) {
152+
return false;
153+
}
154+
155+
return true;
156+
}
157+
138158
/**
139159
* Interface representing the user who triggered the events.
140160
*/
@@ -181,18 +201,7 @@ export class UserDimensions {
181201
this.userProperties = {}; // With no entries in the wire format, present an empty (as opposed to absent) map.
182202
copyField(wireFormat, this, "userProperties", (r: unknown) => {
183203
const entries = Object.entries(r as Record<string, unknown>)
184-
.filter(([, v]) => {
185-
// Property must be an object and have a 'value' field.
186-
if (v == null || typeof v !== "object" || !("value" in v)) {
187-
return false;
188-
}
189-
190-
// The 'value' field must not be null, undefined, or an empty object.
191-
const value = (v as { value: unknown }).value;
192-
const isEmptyObject =
193-
typeof value === "object" && value !== null && Object.keys(value).length === 0;
194-
return value != null && !isEmptyObject;
195-
})
204+
.filter(([, v]) => isValidUserProperty(v))
196205
.map(([k, v]) => [k, new UserPropertyValue(v)]);
197206
return Object.fromEntries(entries);
198207
});

0 commit comments

Comments
 (0)