@@ -51,6 +51,7 @@ static HMODULE g_analytics_module = 0;
5151// This is initialized in `Initialize()` and cleaned up in `Terminate()`.
5252static bool g_initialized = false ;
5353static int g_fake_instance_id = 0 ;
54+ static bool g_analytics_collection_enabled = true ;
5455
5556// Initializes the Analytics desktop API.
5657// This function must be called before any other Analytics methods.
@@ -99,9 +100,8 @@ void Initialize(const App& app) {
99100 } else {
100101 c_options->app_id = current_app_id.c_str ();
101102 c_options->package_name = current_package_name.c_str ();
102- c_options->analytics_collection_enabled_at_first_launch = true ;
103- // c_options->reserved is initialized by
104- // GoogleAnalytics_Options_Create
103+ c_options->analytics_collection_enabled_at_first_launch =
104+ g_analytics_collection_enabled;
105105
106106 LogInfo (
107107 " Analytics: Initializing Google Analytics C API with App ID: %s, "
@@ -144,7 +144,7 @@ void Terminate() {
144144 FreeLibrary (g_analytics_module);
145145 g_analytics_module = 0 ;
146146 }
147- #endif
147+ #endif // defined(_WIN32)
148148
149149 internal::FutureData::Destroy ();
150150 internal::UnregisterTerminateOnDefaultAppDestroy ();
@@ -178,10 +178,15 @@ static void ConvertParametersToGAParams(
178178 // Vector types for top-level event parameters are not supported on
179179 // Desktop. Only specific complex types (like a map processed into an
180180 // ItemVector) are handled.
181- LogError (
182- " Analytics: Parameter '%s' has type Vector, which is unsupported for "
183- " event parameters on Desktop. Skipping." ,
184- 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)
185190 continue ; // Skip this parameter
186191 } else if (param.value .is_map ()) {
187192 // This block handles parameters that are maps.
@@ -365,9 +370,11 @@ void SetUserId(const char* user_id) {
365370//
366371// @param[in] enabled A flag that enables or disables Analytics collection.
367372void SetAnalyticsCollectionEnabled (bool enabled) {
368- FIREBASE_ASSERT_RETURN_VOID ( internal::IsInitialized ()) ;
373+ g_analytics_collection_enabled = enabled ;
369374
370- GoogleAnalytics_SetAnalyticsCollectionEnabled (enabled);
375+ if (internal::IsInitialized ()) {
376+ GoogleAnalytics_SetAnalyticsCollectionEnabled (enabled);
377+ }
371378}
372379
373380// Clears all analytics data for this app from the device and resets the app
@@ -432,53 +439,83 @@ void SetConsent(const std::map<ConsentType, ConsentStatus>& consent_settings) {
432439
433440 // Not supported by the Windows C API.
434441 (void )consent_settings; // Mark as unused
435- LogWarning (
436- " 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)
437449}
438450
439451void InitiateOnDeviceConversionMeasurementWithEmailAddress (
440452 const char * email_address) {
441453 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
442454 (void )email_address;
443- LogWarning (
444- " Analytics: InitiateOnDeviceConversionMeasurementWithEmailAddress() is "
445- " 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)
446463}
447464
448465void InitiateOnDeviceConversionMeasurementWithPhoneNumber (
449466 const char * phone_number) {
450467 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
451468 (void )phone_number;
452- LogWarning (
453- " Analytics: InitiateOnDeviceConversionMeasurementWithPhoneNumber() is "
454- " 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)
455477}
456478
457479void InitiateOnDeviceConversionMeasurementWithHashedEmailAddress (
458480 std::vector<unsigned char > hashed_email_address) {
459481 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
460482 (void )hashed_email_address;
461- LogWarning (
462- " Analytics: "
463- " InitiateOnDeviceConversionMeasurementWithHashedEmailAddress() is not "
464- " 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)
465492}
466493
467494void InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber (
468495 std::vector<unsigned char > hashed_phone_number) {
469496 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
470497 (void )hashed_phone_number;
471- LogWarning (
472- " Analytics: InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber() "
473- " 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)
474506}
475507
476508void SetSessionTimeoutDuration (int64_t milliseconds) {
477509 FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
478510 (void )milliseconds;
479- LogWarning (
480- " Analytics: SetSessionTimeoutDuration() is not supported and has no "
481- " 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)
482519}
483520
484521Future<std::string> GetAnalyticsInstanceId () {
@@ -493,8 +530,14 @@ Future<std::string> GetAnalyticsInstanceId() {
493530 instance_id += ss.str ();
494531 }
495532 api->CompleteWithResult (future_handle, 0 , " " , instance_id);
496- LogWarning (
497- " 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)
498541 return Future<std::string>(api, future_handle.get ());
499542}
500543
@@ -515,14 +558,24 @@ Future<int64_t> GetSessionId() {
515558 api->SafeAlloc <int64_t >(internal::kAnalyticsFnGetSessionId );
516559 int64_t session_id = 0x5E5510171D570BL ; // "SESSIONIDSTUB", kinda
517560 api->CompleteWithResult (future_handle, 0 , " " , session_id);
518- 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)
519567 return Future<int64_t >(api, future_handle.get ());
520568}
521569
522570Future<int64_t > GetSessionIdLastResult () {
523571 FIREBASE_ASSERT_RETURN (Future<int64_t >(), internal::IsInitialized ());
524- LogWarning (
525- " 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)
526579 return static_cast <const Future<int64_t >&>(
527580 internal::FutureData::Get ()->api ()->LastResult (
528581 internal::kAnalyticsFnGetSessionId ));
0 commit comments