Skip to content

Commit fc5aa8e

Browse files
Initialize Analytics C SDK with AppOptions on desktop
This change updates the desktop analytics initialization to use the newly required Options struct for the Windows C API. - In `analytics/src/analytics_desktop.cc`: - `firebase::analytics::Initialize(const App& app)` now retrieves `app_id` and `package_name` from `app.options()`. - It calls `GoogleAnalytics_Options_Create()` to create the options struct. - Populates `app_id`, `package_name`, and sets a default for `analytics_collection_enabled_at_first_launch`. - Calls `GoogleAnalytics_Initialize()` with the populated options struct. - String lifetimes for `app_id` and `package_name` are handled by creating local `std::string` copies before passing their `c_str()` to the C API.
1 parent 7936e99 commit fc5aa8e

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

analytics/src/analytics_desktop.cc

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#if defined(_WIN32)
3030
#include <windows.h>
3131

32-
#include "analytics/src/analytics_windows.h"
32+
// analytics_windows.h is not strictly needed if only using C API via analytics_desktop_dynamic.h
33+
// #include "analytics/src/analytics_windows.h"
3334
#endif // defined(_WIN32)
3435

3536
namespace firebase {
@@ -39,6 +40,13 @@ namespace analytics {
3940
#define ANALYTICS_DLL_FILENAME L"analytics_win.dll"
4041

4142
static HMODULE g_analytics_module = 0;
43+
// It's generally safer to use local std::string copies within Initialize
44+
// for app_id and package_name to ensure their lifetime if Initialize
45+
// could theoretically be called multiple times with different App objects,
46+
// or if AppOptions getters returned temporaries.
47+
// Given AppOptions structure, direct use of app.options().app_id() etc.
48+
// within the Initialize call *should* be safe as App outlives the call,
49+
// but local copies are more robust.
4250
#endif // defined(_WIN32)
4351

4452
// Future data for analytics.
@@ -49,10 +57,7 @@ static int g_fake_instance_id = 0;
4957
// Initializes the Analytics desktop API.
5058
// This function must be called before any other Analytics methods.
5159
void Initialize(const App& app) {
52-
// The 'app' parameter is not directly used by the underlying Google Analytics
53-
// C API for Windows for global initialization. It's included for API
54-
// consistency with other Firebase platforms.
55-
(void)app;
60+
// app parameter is now used to retrieve AppOptions.
5661

5762
g_initialized = true;
5863
internal::RegisterTerminateOnDefaultAppDestroy();
@@ -72,7 +77,7 @@ void Initialize(const App& app) {
7277

7378
if (g_analytics_module) {
7479
int num_loaded = FirebaseAnalytics_LoadDynamicFunctions(
75-
g_analytics_module); // Ensure g_analytics_module is used
80+
g_analytics_module);
7681
if (num_loaded < FirebaseAnalytics_DynamicFunctionCount) {
7782
LogWarning(
7883
"Analytics: Failed to load functions from Google Analytics "
@@ -81,12 +86,41 @@ void Initialize(const App& app) {
8186
FirebaseAnalytics_UnloadDynamicFunctions();
8287
FreeLibrary(g_analytics_module);
8388
g_analytics_module = 0;
89+
// Do not proceed with C API initialization if functions didn't load
8490
} else {
8591
LogInfo("Analytics: Loaded Google Analytics module.");
92+
93+
// Initialize Google Analytics C API
94+
std::string current_app_id = app.options().app_id();
95+
std::string current_package_name = app.options().package_name();
96+
97+
GoogleAnalytics_Options* c_options = GoogleAnalytics_Options_Create();
98+
if (!c_options) {
99+
LogError("Analytics: Failed to create GoogleAnalytics_Options.");
100+
} else {
101+
c_options->app_id = current_app_id.c_str();
102+
c_options->package_name = current_package_name.c_str();
103+
c_options->analytics_collection_enabled_at_first_launch = true;
104+
// c_options->reserved is initialized by GoogleAnalytics_Options_Create
105+
106+
LogInfo("Analytics: Initializing Google Analytics C API with App ID: %s, Package Name: %s",
107+
c_options->app_id ? c_options->app_id : "null",
108+
c_options->package_name ? c_options->package_name : "null");
109+
110+
if (!GoogleAnalytics_Initialize(c_options)) {
111+
LogError("Analytics: Failed to initialize Google Analytics C API.");
112+
// GoogleAnalytics_Initialize destroys c_options automatically if created by _Create
113+
} else {
114+
LogInfo("Analytics: Google Analytics C API initialized successfully.");
115+
}
116+
}
86117
}
118+
} else {
119+
// LogWarning for g_analytics_module load failure is handled by VerifyAndLoadAnalyticsLibrary
120+
g_analytics_module = 0; // Ensure it's null if loading failed
87121
}
88122
}
89-
#endif
123+
#endif // defined(_WIN32)
90124
}
91125

92126
namespace internal {

0 commit comments

Comments
 (0)