|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
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 |
20 | 21 |
|
21 | 22 | #include <vector>
|
22 | 23 | #include <string>
|
23 |
| -#include <map> // Will likely need this for Item or Parameter conversion |
| 24 | +#include <map> |
24 | 25 |
|
25 | 26 | // Error code for Analytics features not supported on this platform.
|
26 | 27 | const int kAnalyticsErrorNotSupportedOnPlatform = 1; // Or a more specific range
|
@@ -52,114 +53,120 @@ void Terminate() {
|
52 | 53 | // and for any future global cleanup needs for the desktop wrapper.
|
53 | 54 | }
|
54 | 55 |
|
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) { |
62 | 61 | return;
|
63 | 62 | }
|
64 | 63 |
|
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; |
71 | 69 | }
|
72 | 70 |
|
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, |
85 | 76 | 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 | + } |
101 | 94 |
|
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(); |
114 | 100 |
|
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 | + } |
136 | 106 |
|
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; |
141 | 124 | } 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); |
144 | 126 | }
|
145 |
| - } else { |
146 |
| - // Log warning: Expected a map (Item) in the item_variants vector, got something else. |
147 | 127 | }
|
148 |
| - } |
149 | 128 |
|
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 | + } |
154 | 135 | } 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); |
157 | 137 | }
|
| 138 | + } |
| 139 | + |
| 140 | + if (item_vector_populated) { |
| 141 | + GoogleAnalytics_EventParameters_InsertItemVector( |
| 142 | + c_event_params, param.name, c_item_vector); |
158 | 143 | } else {
|
159 |
| - // Log an error or warning: unsupported variant type for parameter. |
| 144 | + GoogleAnalytics_ItemVector_Destroy(c_item_vector); |
160 | 145 | }
|
| 146 | + } else { |
| 147 | + LogWarning("Analytics: Unsupported variant type for parameter '%s'.", param.name); |
161 | 148 | }
|
162 | 149 | }
|
| 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 | + } |
163 | 170 |
|
164 | 171 | GoogleAnalytics_LogEvent(name, c_event_params);
|
165 | 172 | // c_event_params is consumed by GoogleAnalytics_LogEvent, so no explicit destroy if passed.
|
@@ -192,7 +199,7 @@ void LogEvent(const char* name,
|
192 | 199 | // clear the user property. Must be UTF-8 encoded if not nullptr.
|
193 | 200 | void SetUserProperty(const char* name, const char* property) {
|
194 | 201 | 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."); |
196 | 203 | return;
|
197 | 204 | }
|
198 | 205 | // The C API GoogleAnalytics_SetUserProperty allows value to be nullptr to remove the property.
|
@@ -244,7 +251,7 @@ void ResetAnalyticsData() {
|
244 | 251 | void SetConsent(const std::map<ConsentType, ConsentStatus>& consent_settings) {
|
245 | 252 | // Not supported by the Windows C API.
|
246 | 253 | (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."); |
248 | 255 | }
|
249 | 256 |
|
250 | 257 | void LogEvent(const char* name) {
|
@@ -293,31 +300,31 @@ void LogEvent(const char* name, const char* parameter_name,
|
293 | 300 |
|
294 | 301 | void InitiateOnDeviceConversionMeasurementWithEmailAddress(
|
295 | 302 | const char* email_address) {
|
296 |
| - // Not supported by the Windows C API. |
297 | 303 | (void)email_address;
|
| 304 | + LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithEmailAddress() is not supported and has no effect on Windows."); |
298 | 305 | }
|
299 | 306 |
|
300 | 307 | void InitiateOnDeviceConversionMeasurementWithPhoneNumber(
|
301 | 308 | const char* phone_number) {
|
302 |
| - // Not supported by the Windows C API. |
303 | 309 | (void)phone_number;
|
| 310 | + LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithPhoneNumber() is not supported and has no effect on Windows."); |
304 | 311 | }
|
305 | 312 |
|
306 | 313 | void InitiateOnDeviceConversionMeasurementWithHashedEmailAddress(
|
307 | 314 | std::vector<unsigned char> hashed_email_address) {
|
308 |
| - // Not supported by the Windows C API. |
309 | 315 | (void)hashed_email_address;
|
| 316 | + LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithHashedEmailAddress() is not supported and has no effect on Windows."); |
310 | 317 | }
|
311 | 318 |
|
312 | 319 | void InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber(
|
313 | 320 | std::vector<unsigned char> hashed_phone_number) {
|
314 |
| - // Not supported by the Windows C API. |
315 | 321 | (void)hashed_phone_number;
|
| 322 | + LogWarning("Analytics: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber() is not supported and has no effect on Windows."); |
316 | 323 | }
|
317 | 324 |
|
318 | 325 | void SetSessionTimeoutDuration(int64_t milliseconds) {
|
319 |
| - // Not supported by the Windows C API. |
320 | 326 | (void)milliseconds;
|
| 327 | + LogWarning("Analytics: SetSessionTimeoutDuration() is not supported and has no effect on Windows."); |
321 | 328 | }
|
322 | 329 |
|
323 | 330 | Future<std::string> GetAnalyticsInstanceId() {
|
|
0 commit comments