2828
2929#if defined(_WIN32)
3030#include < windows.h>
31-
3231#include " analytics_windows.h"
3332#endif // defined(_WIN32)
3433
@@ -52,6 +51,7 @@ static HMODULE g_analytics_module = 0;
5251// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
5352static bool g_initialized = false ;
5453static int g_fake_instance_id = 0 ;
54+ static bool g_analytics_collection_enabled = true ;
5555
5656// Initializes the Analytics desktop API.
5757// This function must be called before any other Analytics methods.
@@ -100,9 +100,8 @@ void Initialize(const App& app) {
100100 } else {
101101 c_options->app_id = current_app_id.c_str ();
102102 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
105- // GoogleAnalytics_Options_Create
103+ c_options->analytics_collection_enabled_at_first_launch =
104+ g_analytics_collection_enabled;
106105
107106 LogInfo (
108107 " Analytics: Initializing Google Analytics C API with App ID: %s, "
@@ -145,7 +144,7 @@ void Terminate() {
145144 FreeLibrary (g_analytics_module);
146145 g_analytics_module = 0 ;
147146 }
148- #endif
147+ #endif // defined(_WIN32)
149148
150149 internal::FutureData::Destroy ();
151150 internal::UnregisterTerminateOnDefaultAppDestroy ();
@@ -179,10 +178,15 @@ static void ConvertParametersToGAParams(
179178 // Vector types for top-level event parameters are not supported on
180179 // Desktop. Only specific complex types (like a map processed into an
181180 // ItemVector) are handled.
182- LogError (
183- " Analytics: Parameter '%s' has type Vector, which is unsupported for "
184- " event parameters on Desktop. Skipping." ,
185- param.name );
181+ #if defined(_WIN32)
182+ if (g_analytics_module) {
183+ // Only log this if we are not in stub mode.
184+ LogError (
185+ " Analytics: Parameter '%s' has type Vector, which is unsupported for "
186+ " event parameters on Desktop. Skipping." ,
187+ param.name );
188+ }
189+ #endif // defined(_WIN32)
186190 continue ; // Skip this parameter
187191 } else if (param.value .is_map ()) {
188192 // This block handles parameters that are maps.
@@ -366,9 +370,11 @@ void SetUserId(const char* user_id) {
366370//
367371// @param[in] enabled A flag that enables or disables Analytics collection.
368372void SetAnalyticsCollectionEnabled (bool enabled) {
369- FIREBASE_ASSERT_RETURN_VOID ( internal::IsInitialized ()) ;
373+ g_analytics_collection_enabled = enabled ;
370374
371- GoogleAnalytics_SetAnalyticsCollectionEnabled (enabled);
375+ if (internal::IsInitialized ()) {
376+ GoogleAnalytics_SetAnalyticsCollectionEnabled (enabled);
377+ }
372378}
373379
374380// Clears all analytics data for this app from the device and resets the app
@@ -433,53 +439,83 @@ void SetConsent(const std::map<ConsentType, ConsentStatus>& consent_settings) {
433439
434440 // Not supported by the Windows C API.
435441 (void )consent_settings; // Mark as unused
436- LogWarning (
437- " Analytics: SetConsent() is not supported and has no effect on Desktop." );
442+ #if defined(_WIN32)
443+ if (g_analytics_module) {
444+ // Only log this if we are not in stub mode.
445+ LogWarning (
446+ " Analytics: SetConsent() is not supported and has no effect on Desktop." );
447+ }
448+ #endif // defined(_WIN32)
438449}
439450
440451void InitiateOnDeviceConversionMeasurementWithEmailAddress (
441452 const char * email_address) {
442453 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
443454 (void )email_address;
444- LogWarning (
445- " Analytics: InitiateOnDeviceConversionMeasurementWithEmailAddress() is "
446- " not supported and has no effect on Desktop." );
455+ #if defined(_WIN32)
456+ if (g_analytics_module) {
457+ // Only log this if we are not in stub mode.
458+ LogWarning (
459+ " Analytics: InitiateOnDeviceConversionMeasurementWithEmailAddress() is "
460+ " not supported and has no effect on Desktop." );
461+ }
462+ #endif // defined(_WIN32)
447463}
448464
449465void InitiateOnDeviceConversionMeasurementWithPhoneNumber (
450466 const char * phone_number) {
451467 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
452468 (void )phone_number;
453- LogWarning (
454- " Analytics: InitiateOnDeviceConversionMeasurementWithPhoneNumber() is "
455- " not supported and has no effect on Desktop." );
469+ #if defined(_WIN32)
470+ if (g_analytics_module) {
471+ // Only log this if we are not in stub mode.
472+ LogWarning (
473+ " Analytics: InitiateOnDeviceConversionMeasurementWithPhoneNumber() is "
474+ " not supported and has no effect on Desktop." );
475+ }
476+ #endif // defined(_WIN32)
456477}
457478
458479void InitiateOnDeviceConversionMeasurementWithHashedEmailAddress (
459480 std::vector<unsigned char > hashed_email_address) {
460481 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
461482 (void )hashed_email_address;
462- LogWarning (
463- " Analytics: "
464- " InitiateOnDeviceConversionMeasurementWithHashedEmailAddress() is not "
465- " supported and has no effect on Desktop." );
483+ #if defined(_WIN32)
484+ if (g_analytics_module) {
485+ // Only log this if we are not in stub mode.
486+ LogWarning (
487+ " Analytics: "
488+ " InitiateOnDeviceConversionMeasurementWithHashedEmailAddress() is not "
489+ " supported and has no effect on Desktop." );
490+ }
491+ #endif // defined(_WIN32)
466492}
467493
468494void InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber (
469495 std::vector<unsigned char > hashed_phone_number) {
470496 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
471497 (void )hashed_phone_number;
472- LogWarning (
473- " Analytics: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber() "
474- " is not supported and has no effect on Desktop." );
498+ #if defined(_WIN32)
499+ if (g_analytics_module) {
500+ // Only log this if we are not in stub mode.
501+ LogWarning (
502+ " Analytics: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber() "
503+ " is not supported and has no effect on Desktop." );
504+ }
505+ #endif // defined(_WIN32)
475506}
476507
477508void SetSessionTimeoutDuration (int64_t milliseconds) {
478509 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
479510 (void )milliseconds;
480- LogWarning (
481- " Analytics: SetSessionTimeoutDuration() is not supported and has no "
482- " effect on Desktop." );
511+ #if defined(_WIN32)
512+ if (g_analytics_module) {
513+ // Only log this if we are not in stub mode.
514+ LogWarning (
515+ " Analytics: SetSessionTimeoutDuration() is not supported and has no "
516+ " effect on Desktop." );
517+ }
518+ #endif // defined(_WIN32)
483519}
484520
485521Future<std::string> GetAnalyticsInstanceId () {
@@ -494,8 +530,14 @@ Future<std::string> GetAnalyticsInstanceId() {
494530 instance_id += ss.str ();
495531 }
496532 api->CompleteWithResult (future_handle, 0 , " " , instance_id);
497- LogWarning (
498- " Analytics: GetAnalyticsInstanceId() is not supported on Desktop." );
533+
534+ #if defined(_WIN32)
535+ if (g_analytics_module) {
536+ // Only log this if we are not in stub mode.
537+ LogWarning (
538+ " Analytics: GetAnalyticsInstanceId() is not supported on Desktop." );
539+ }
540+ #endif // defined(_WIN32)
499541 return Future<std::string>(api, future_handle.get ());
500542}
501543
@@ -516,14 +558,24 @@ Future<int64_t> GetSessionId() {
516558 api->SafeAlloc <int64_t >(internal::kAnalyticsFnGetSessionId );
517559 int64_t session_id = 0x5E5510171D570BL ; // "SESSIONIDSTUB", kinda
518560 api->CompleteWithResult (future_handle, 0 , " " , session_id);
519- LogWarning (" Analytics: GetSessionId() is not supported on Desktop." );
561+ #if defined(_WIN32)
562+ if (g_analytics_module) {
563+ // Only log this if we are not in stub mode.
564+ LogWarning (" Analytics: GetSessionId() is not supported on Desktop." );
565+ }
566+ #endif // defined(_WIN32)
520567 return Future<int64_t >(api, future_handle.get ());
521568}
522569
523570Future<int64_t > GetSessionIdLastResult () {
524571 FIREBASE_ASSERT_RETURN (Future<int64_t >(), internal::IsInitialized ());
525- LogWarning (
526- " Analytics: GetSessionIdLastResult() is not supported on Desktop." );
572+ #if defined(_WIN32)
573+ if (g_analytics_module) {
574+ // Only log this if we are not in stub mode.
575+ LogWarning (
576+ " Analytics: GetSessionIdLastResult() is not supported on Desktop." );
577+ }
578+ #endif // defined(_WIN32)
527579 return static_cast <const Future<int64_t >&>(
528580 internal::FutureData::Get ()->api ()->LastResult (
529581 internal::kAnalyticsFnGetSessionId ));
0 commit comments