Skip to content

Commit e29cce5

Browse files
refactor: Address final review comments for Windows Analytics
This commit incorporates the latest round of specific feedback on the Windows Analytics C++ implementation (src/analytics_desktop.cc): 1. **Comment Cleanup:** - Removed a large commented-out block that previously contained a local `AnalyticsFn` enum definition. - Removed a comment in `Initialize()` related to marking the `app` parameter as unused. - Removed an outdated comment in `Initialize()` that described the function as a placeholder. - Removed a type comment from the `ConvertParametersToGAParams()` function signature. - Replaced a lengthy comment in `LogEvent()` regarding C API object lifecycle with a more concise one. 2. **Refined Map Parameter Processing Logic:** - In `ConvertParametersToGAParams`, when handling a `Parameter` whose value is a map, the logic for creating `GoogleAnalytics_Item` objects from the map's entries has been clarified. - A local boolean flag (`successfully_set_property`) is used for each map entry to track if a value was successfully set in the corresponding `GoogleAnalytics_Item`. - A `GoogleAnalytics_Item` is only added to the `GoogleAnalytics_ItemVector` if a property was successfully set. Otherwise, the item is destroyed. This prevents empty or partially formed items (e.g., from map entries with unsupported value types) from being included in the ItemVector.
1 parent 9b377ac commit e29cce5

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

src/analytics_desktop.cc

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@
2828
namespace firebase {
2929
namespace analytics {
3030

31-
// (Local AnalyticsFn enum removed, assuming it's now provided by analytics_common.h)
32-
// namespace internal {
33-
// // Functions that return a Future.
34-
// enum AnalyticsFn {
35-
// kAnalyticsFn_GetAnalyticsInstanceId = 0,
36-
// kAnalyticsFn_GetSessionId,
37-
// kAnalyticsFnCount // Must be the last enum value.
38-
// };
39-
// } // namespace internal
40-
4131
// Future data for analytics.
4232
// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
4333
static FutureData* g_future_data = nullptr;
@@ -48,12 +38,8 @@ void Initialize(const App& app) {
4838
// The 'app' parameter is not directly used by the underlying Google Analytics C API
4939
// for Windows for global initialization. It's included for API consistency
5040
// with other Firebase platforms.
51-
(void)app; // Mark as unused if applicable by style guides.
41+
(void)app;
5242

53-
// The underlying Google Analytics C API for Windows does not have an explicit
54-
// global initialization function.
55-
// This function is provided for API consistency with other Firebase platforms
56-
// and for any future global initialization needs for the desktop wrapper.
5743
if (g_future_data) {
5844
LogWarning("Analytics: Initialize() called when already initialized.");
5945
} else {
@@ -78,7 +64,7 @@ void Terminate() {
7864
}
7965

8066
static void ConvertParametersToGAParams(
81-
const Parameter* parameters, // firebase::analytics::Parameter
67+
const Parameter* parameters,
8268
size_t number_of_parameters,
8369
GoogleAnalytics_EventParameters* c_event_params) {
8470
if (!parameters || number_of_parameters == 0 || !c_event_params) {
@@ -140,26 +126,27 @@ static void ConvertParametersToGAParams(
140126

141127
// Removed: GoogleAnalytics_Item_InsertString(c_item, "name", key_from_map.c_str());
142128

143-
bool item_had_valid_property = false;
129+
bool successfully_set_property = false;
144130
if (value_from_map.is_int64()) {
145131
GoogleAnalytics_Item_InsertInt(c_item, key_from_map.c_str(), value_from_map.int64_value());
146-
item_had_valid_property = true;
132+
successfully_set_property = true;
147133
} else if (value_from_map.is_double()) {
148134
GoogleAnalytics_Item_InsertDouble(c_item, key_from_map.c_str(), value_from_map.double_value());
149-
item_had_valid_property = true;
135+
successfully_set_property = true;
150136
} else if (value_from_map.is_string()) {
151137
GoogleAnalytics_Item_InsertString(c_item, key_from_map.c_str(), value_from_map.string_value());
152-
item_had_valid_property = true;
138+
successfully_set_property = true;
153139
} else {
154140
LogWarning("Analytics: Value for key '%s' in map parameter '%s' has an unsupported Variant type. This key-value pair will be skipped.", key_from_map.c_str(), param.name);
141+
// successfully_set_property remains false
155142
}
156143

157-
if (item_had_valid_property) {
144+
if (successfully_set_property) {
158145
GoogleAnalytics_ItemVector_InsertItem(c_item_vector, c_item);
159146
// c_item is now owned by c_item_vector
160-
item_vector_populated = true;
147+
item_vector_populated = true; // Mark that the vector has at least one item
161148
} else {
162-
// If no valid property was set for this c_item (e.g., value type was unsupported)
149+
// If no property was set (e.g., value type was unsupported), destroy the created c_item.
163150
GoogleAnalytics_Item_Destroy(c_item);
164151
}
165152
}
@@ -199,20 +186,7 @@ void LogEvent(const char* name,
199186
}
200187

201188
GoogleAnalytics_LogEvent(name, c_event_params);
202-
// c_event_params is consumed by GoogleAnalytics_LogEvent, so no explicit destroy if passed.
203-
// However, if we created it but didn't pass it (e.g. error), it should be destroyed.
204-
// The C API doc says: "Automatically destroyed when it is logged."
205-
// "The caller is responsible for destroying the event parameter map using the
206-
// GoogleAnalytics_EventParameters_Destroy() function, unless it has been
207-
// logged, in which case it will be destroyed automatically when it is logged."
208-
// So, if GoogleAnalytics_LogEvent is called with c_event_params, it's handled.
209-
// If there was an error before that, and c_event_params was allocated, it would leak.
210-
// For robustness, a unique_ptr or similar RAII wrapper would be better for c_event_params
211-
// if not for the C API's ownership transfer.
212-
// Given the current structure, if c_event_params is created, it's always passed or should be.
213-
// If `name` is invalid, we return early, c_event_params is not created.
214-
// If `c_event_params` creation fails, we return, nothing to destroy.
215-
// If a parameter is bad, we `continue`, `c_event_params` is still valid and eventually logged.
189+
// GoogleAnalytics_LogEvent is expected to handle the lifecycle of c_event_params if non-null.
216190
}
217191

218192
// Sets a user property to the given value.

0 commit comments

Comments
 (0)