Skip to content

Commit 94abdb3

Browse files
feat: Integrate Firebase logging for Windows Analytics
Replaces placeholder comments with actual calls to LogError() and LogWarning() throughout the Windows Analytics implementation in src/analytics_desktop.cc. Key changes: - Added #include "firebase/log.h". - Updated error handling in LogEvent, ConvertParametersToGAParams, and SetUserProperty to use LogError() for invalid arguments or internal failures. - Updated warnings for unexpected data types or structures in event parameters to use LogWarning(). - Added LogWarning() calls to all stubbed functions (e.g., SetConsent, InitiateOnDeviceConversionMeasurement*, SetSessionTimeoutDuration) to inform developers that these operations are not supported and have no effect on the Windows platform. This change enhances the robustness and diagnosability of the Windows Analytics module by providing clear feedback through the Firebase logging system.
1 parent 6d3d353 commit 94abdb3

File tree

1 file changed

+109
-102
lines changed

1 file changed

+109
-102
lines changed

src/analytics_desktop.cc

Lines changed: 109 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "analytics/windows/include/public/c/analytics.h" // C API
16-
#include "firebase/app.h" // For firebase::App
17-
#include "firebase/analytics.h" // For common Parameter, LogEvent, etc.
18-
#include "firebase/variant.h" // For firebase::Variant (used by Item and LogEvent)
19-
#include "firebase/future.h" // For firebase::Future
15+
#include "analytics/src/windows/analytics_windows.h"
16+
#include "firebase/app.h"
17+
#include "firebase/analytics.h"
18+
#include "firebase/variant.h"
19+
#include "firebase/future.h"
20+
#include "firebase/log.h" // <-- New include
2021

2122
#include <vector>
2223
#include <string>
23-
#include <map> // Will likely need this for Item or Parameter conversion
24+
#include <map>
2425

2526
// Error code for Analytics features not supported on this platform.
2627
const int kAnalyticsErrorNotSupportedOnPlatform = 1; // Or a more specific range
@@ -52,114 +53,120 @@ void Terminate() {
5253
// and for any future global cleanup needs for the desktop wrapper.
5354
}
5455

55-
// Logs an event with the given name and parameters.
56-
void LogEvent(const char* name,
57-
const Parameter* parameters,
58-
size_t number_of_parameters) {
59-
if (name == nullptr || name[0] == '\0') {
60-
// Log an error: event name cannot be null or empty.
61-
// Consider adding a logging mechanism if available, e.g., via firebase::App.
56+
static void ConvertParametersToGAParams(
57+
const Parameter* parameters, // firebase::analytics::Parameter
58+
size_t number_of_parameters,
59+
GoogleAnalytics_EventParameters* c_event_params) {
60+
if (!parameters || number_of_parameters == 0 || !c_event_params) {
6261
return;
6362
}
6463

65-
GoogleAnalytics_EventParameters* c_event_params = nullptr;
66-
if (parameters != nullptr && number_of_parameters > 0) {
67-
c_event_params = GoogleAnalytics_EventParameters_Create();
68-
if (!c_event_params) {
69-
// Log an error: Failed to create event parameters map.
70-
return;
64+
for (size_t i = 0; i < number_of_parameters; ++i) {
65+
const Parameter& param = parameters[i];
66+
if (param.name == nullptr || param.name[0] == '\0') {
67+
LogError("Analytics: Parameter name cannot be null or empty.");
68+
continue;
7169
}
7270

73-
for (size_t i = 0; i < number_of_parameters; ++i) {
74-
const Parameter& param = parameters[i];
75-
if (param.name == nullptr || param.name[0] == '\0') {
76-
// Log an error: parameter name cannot be null or empty.
77-
continue;
78-
}
79-
80-
if (param.value.is_int64()) {
81-
GoogleAnalytics_EventParameters_InsertInt(c_event_params, param.name,
82-
param.value.int64_value());
83-
} else if (param.value.is_double()) {
84-
GoogleAnalytics_EventParameters_InsertDouble(c_event_params, param.name,
71+
if (param.value.is_int64()) {
72+
GoogleAnalytics_EventParameters_InsertInt(c_event_params, param.name,
73+
param.value.int64_value());
74+
} else if (param.value.is_double()) {
75+
GoogleAnalytics_EventParameters_InsertDouble(c_event_params, param.name,
8576
param.value.double_value());
86-
} else if (param.value.is_string()) {
87-
GoogleAnalytics_EventParameters_InsertString(
88-
c_event_params, param.name, param.value.string_value());
89-
} else if (param.value.is_vector()) {
90-
// This parameter is expected to be a vector of Item objects.
91-
// Each Item in the vector is represented as a Variant map.
92-
const std::vector<firebase::Variant>& item_variants =
93-
param.value.vector_value();
94-
95-
GoogleAnalytics_ItemVector* c_item_vector =
96-
GoogleAnalytics_ItemVector_Create();
97-
if (!c_item_vector) {
98-
// Log error: Failed to create ItemVector
99-
continue; // Skip this parameter
100-
}
77+
} else if (param.value.is_string()) {
78+
GoogleAnalytics_EventParameters_InsertString(
79+
c_event_params, param.name, param.value.string_value());
80+
} else if (param.value.is_vector()) {
81+
// This block handles parameters that are vectors of items (e.g., kParameterItems).
82+
// The 'param.value' is expected to be a firebase::Variant of type kTypeVector.
83+
// Each element in this outer vector (item_variants) is itself a firebase::Variant,
84+
// which must be of type kTypeMap, representing a single item's properties.
85+
const std::vector<firebase::Variant>& item_variants =
86+
param.value.vector_value();
87+
88+
GoogleAnalytics_ItemVector* c_item_vector =
89+
GoogleAnalytics_ItemVector_Create();
90+
if (!c_item_vector) {
91+
LogError("Analytics: Failed to create ItemVector for parameter '%s'.", param.name);
92+
continue; // Skip this parameter
93+
}
10194

102-
bool item_vector_populated = false;
103-
for (const firebase::Variant& item_variant : item_variants) {
104-
if (item_variant.is_map()) {
105-
const std::map<std::string, firebase::Variant>& item_map =
106-
item_variant.map_value();
107-
108-
GoogleAnalytics_Item* c_item = GoogleAnalytics_Item_Create();
109-
if (!c_item) {
110-
// Log error: Failed to create Item
111-
// This item won't be added, but continue with other items.
112-
continue;
113-
}
95+
bool item_vector_populated = false;
96+
for (const firebase::Variant& item_variant : item_variants) {
97+
if (item_variant.is_map()) {
98+
const std::map<std::string, firebase::Variant>& item_map =
99+
item_variant.map_value();
114100

115-
bool item_populated = false;
116-
for (const auto& entry : item_map) {
117-
const std::string& item_key = entry.first;
118-
const firebase::Variant& item_val = entry.second;
119-
120-
if (item_val.is_int64()) {
121-
GoogleAnalytics_Item_InsertInt(c_item, item_key.c_str(),
122-
item_val.int64_value());
123-
item_populated = true;
124-
} else if (item_val.is_double()) {
125-
GoogleAnalytics_Item_InsertDouble(c_item, item_key.c_str(),
126-
item_val.double_value());
127-
item_populated = true;
128-
} else if (item_val.is_string()) {
129-
GoogleAnalytics_Item_InsertString(c_item, item_key.c_str(),
130-
item_val.string_value());
131-
item_populated = true;
132-
} else {
133-
// Log warning: Unsupported variant type in Item map for key item_key
134-
}
135-
}
101+
GoogleAnalytics_Item* c_item = GoogleAnalytics_Item_Create();
102+
if (!c_item) {
103+
LogError("Analytics: Failed to create Item for an item in vector parameter '%s'.", param.name);
104+
continue;
105+
}
136106

137-
if (item_populated) {
138-
GoogleAnalytics_ItemVector_InsertItem(c_item_vector, c_item);
139-
// c_item is now owned by c_item_vector
140-
item_vector_populated = true;
107+
bool item_populated = false;
108+
for (const auto& entry : item_map) {
109+
const std::string& item_key = entry.first;
110+
const firebase::Variant& item_val = entry.second;
111+
112+
if (item_val.is_int64()) {
113+
GoogleAnalytics_Item_InsertInt(c_item, item_key.c_str(),
114+
item_val.int64_value());
115+
item_populated = true;
116+
} else if (item_val.is_double()) {
117+
GoogleAnalytics_Item_InsertDouble(c_item, item_key.c_str(),
118+
item_val.double_value());
119+
item_populated = true;
120+
} else if (item_val.is_string()) {
121+
GoogleAnalytics_Item_InsertString(c_item, item_key.c_str(),
122+
item_val.string_value());
123+
item_populated = true;
141124
} else {
142-
// If item had no valid properties or failed to create, destroy it.
143-
GoogleAnalytics_Item_Destroy(c_item);
125+
LogWarning("Analytics: Unsupported variant type in Item map for key '%s' in vector parameter '%s'.", item_key.c_str(), param.name);
144126
}
145-
} else {
146-
// Log warning: Expected a map (Item) in the item_variants vector, got something else.
147127
}
148-
}
149128

150-
if (item_vector_populated) {
151-
GoogleAnalytics_EventParameters_InsertItemVector(
152-
c_event_params, param.name, c_item_vector);
153-
// c_item_vector is now owned by c_event_params
129+
if (item_populated) {
130+
GoogleAnalytics_ItemVector_InsertItem(c_item_vector, c_item);
131+
item_vector_populated = true;
132+
} else {
133+
GoogleAnalytics_Item_Destroy(c_item);
134+
}
154135
} else {
155-
// If no items were successfully added to the vector, destroy the empty vector.
156-
GoogleAnalytics_ItemVector_Destroy(c_item_vector);
136+
LogWarning("Analytics: Expected a map (Item) in vector parameter '%s', but found a different Variant type.", param.name);
157137
}
138+
}
139+
140+
if (item_vector_populated) {
141+
GoogleAnalytics_EventParameters_InsertItemVector(
142+
c_event_params, param.name, c_item_vector);
158143
} else {
159-
// Log an error or warning: unsupported variant type for parameter.
144+
GoogleAnalytics_ItemVector_Destroy(c_item_vector);
160145
}
146+
} else {
147+
LogWarning("Analytics: Unsupported variant type for parameter '%s'.", param.name);
161148
}
162149
}
150+
}
151+
152+
// Logs an event with the given name and parameters.
153+
void LogEvent(const char* name,
154+
const Parameter* parameters, // firebase::analytics::Parameter
155+
size_t number_of_parameters) {
156+
if (name == nullptr || name[0] == '\0') {
157+
LogError("Analytics: Event name cannot be null or empty.");
158+
return;
159+
}
160+
161+
GoogleAnalytics_EventParameters* c_event_params = nullptr;
162+
if (parameters != nullptr && number_of_parameters > 0) {
163+
c_event_params = GoogleAnalytics_EventParameters_Create();
164+
if (!c_event_params) {
165+
LogError("Analytics: Failed to create event parameters map for event '%s'.", name);
166+
return;
167+
}
168+
ConvertParametersToGAParams(parameters, number_of_parameters, c_event_params);
169+
}
163170

164171
GoogleAnalytics_LogEvent(name, c_event_params);
165172
// c_event_params is consumed by GoogleAnalytics_LogEvent, so no explicit destroy if passed.
@@ -192,7 +199,7 @@ void LogEvent(const char* name,
192199
// clear the user property. Must be UTF-8 encoded if not nullptr.
193200
void SetUserProperty(const char* name, const char* property) {
194201
if (name == nullptr || name[0] == '\0') {
195-
// Log an error: User property name cannot be null or empty.
202+
LogError("Analytics: User property name cannot be null or empty.");
196203
return;
197204
}
198205
// The C API GoogleAnalytics_SetUserProperty allows value to be nullptr to remove the property.
@@ -244,7 +251,7 @@ void ResetAnalyticsData() {
244251
void SetConsent(const std::map<ConsentType, ConsentStatus>& consent_settings) {
245252
// Not supported by the Windows C API.
246253
(void)consent_settings; // Mark as unused
247-
// Consider logging a warning if a logging utility is available.
254+
LogWarning("Analytics: SetConsent() is not supported and has no effect on Windows.");
248255
}
249256

250257
void LogEvent(const char* name) {
@@ -293,31 +300,31 @@ void LogEvent(const char* name, const char* parameter_name,
293300

294301
void InitiateOnDeviceConversionMeasurementWithEmailAddress(
295302
const char* email_address) {
296-
// Not supported by the Windows C API.
297303
(void)email_address;
304+
LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithEmailAddress() is not supported and has no effect on Windows.");
298305
}
299306

300307
void InitiateOnDeviceConversionMeasurementWithPhoneNumber(
301308
const char* phone_number) {
302-
// Not supported by the Windows C API.
303309
(void)phone_number;
310+
LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithPhoneNumber() is not supported and has no effect on Windows.");
304311
}
305312

306313
void InitiateOnDeviceConversionMeasurementWithHashedEmailAddress(
307314
std::vector<unsigned char> hashed_email_address) {
308-
// Not supported by the Windows C API.
309315
(void)hashed_email_address;
316+
LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithHashedEmailAddress() is not supported and has no effect on Windows.");
310317
}
311318

312319
void InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber(
313320
std::vector<unsigned char> hashed_phone_number) {
314-
// Not supported by the Windows C API.
315321
(void)hashed_phone_number;
322+
LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber() is not supported and has no effect on Windows.");
316323
}
317324

318325
void SetSessionTimeoutDuration(int64_t milliseconds) {
319-
// Not supported by the Windows C API.
320326
(void)milliseconds;
327+
LogWarning("Analytics: SetSessionTimeoutDuration() is not supported and has no effect on Windows.");
321328
}
322329

323330
Future<std::string> GetAnalyticsInstanceId() {

0 commit comments

Comments
 (0)