Firebase Analytics not logging events in MAUI .net9 for both iOS and Android ?

Jermin Contractor 0 Reputation points
2024-12-02T14:09:37.8866667+00:00

Hi, Firebase dashboard is not registering any logs for my MAUI app. I am using Xamarin.firebase.Analytics for Android and AdamE.Firebase.iOS.Analytics for iOS.

.csproj

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-ios'">
 	<BundleResource Include="GoogleService-Info.plist" />
	<PackageReference Include="AdamE.Firebase.iOS.Analytics" Version="10.29.0" />
	<PackageReference Include="AdamE.Firebase.iOS.Crashlytics" Version="10.29.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'">
	<GoogleServicesJson Include="Google-services.json" />
	<PackageReference Include="Xamarin.Firebase.Analytics" Version="122.1.2" />
	<PackageReference Include="Xamarin.Firebase.Crashlytics" Version="119.2.1" />
</ItemGroup>

MAUIProgram.cs

private static MauiAppBuilder RegisterFirebase(this MauiAppBuilder builder) 
{	
	builder.ConfigureLifecycleEvents(events => 	{
#if IOS		
		events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => 		{							Firebase.Core.App.Configure();			Firebase.Crashlytics.Crashlytics.SharedInstance.Init(); Firebase.Crashlytics.Crashlytics.SharedInstance.SetCrashlyticsCollectionEnabled(true);			Firebase.Crashlytics.Crashlytics.SharedInstance.SendUnsentReports();			
return false; 		
		}));
#else		
		events.AddAndroid(android => android.OnCreate((activity, bundle) => 		{			Firebase.FirebaseApp.InitializeApp(activity);Firebase.Crashlytics.FirebaseCrashlytics.Instance.SetCrashlyticsCollectionEnabled(Java.Lang.Boolean.True);				Firebase.Crashlytics.FirebaseCrashlytics.Instance.SendUnsentReports(); 		
		}));
#endif 	
	});	

return builder; 
}

AnalyticsService.cs

using System;
using Microsoft.Maui.Controls.Compatibility;
using Firebase.Analytics;

#if IOS
using Foundation;
#else
using Android.Content;
using Android.OS;
#endif

namespace Analytics09;

public interface IFirebaseAnalyticsService
{
    void Log(string value, string eventName = "screen_view", string paramName = "screen_name");
    void Log(string eventName, IDictionary<string, string> parameters);
}


public class FirebaseAnalyticsService : IFirebaseAnalyticsService
{

    public void Log(string value, string eventName = "screen_view", string paramName = "screen_name")
    {
        Log(eventName, new Dictionary<string, string>
            {
                { paramName, value.Replace("ViewModel", "", StringComparison.InvariantCulture) }
            });
    }

    public void Log(string eventName, IDictionary<string, string> parameters)
    {

#if IOS
        if (parameters == null)
        {
            Analytics.LogEvent(eventName, (Dictionary<object, object>)null);
            return;
        }

        var keys = new List<NSString>();
        var values = new List<NSString>();
        foreach (var item in parameters)
        {
            keys.Add(new NSString(item.Key));
            values.Add(new NSString(item.Value));
        }

        var parametersDictionary =
            NSDictionary<NSString, NSObject>.FromObjectsAndKeys(values.ToArray(), keys.ToArray(), keys.Count);
        Analytics.LogEvent(eventName, parametersDictionary);
#else
        var firebaseAnalytics = FirebaseAnalytics.GetInstance(Platform.CurrentActivity);

        if (parameters == null)
        {
            firebaseAnalytics.LogEvent(eventName, null);
            return;
        }

        var bundle = new Bundle();
        foreach (var param in parameters)
        {
            bundle.PutString(param.Key, param.Value);
        }

        firebaseAnalytics.LogEvent(eventName, bundle);
#endif
    }
}

MainPage.xaml.cs


private void OnEventButtonClicked(object sender, EventArgs e)
{	
#if IOS	
	_firebaseAnalyticsService.Log("iOS_CounterButtonTapped");
#elif ANDROID	
	_firebaseAnalyticsService.Log("Android_CounterButtonTapped");
#endif 
}

GoogleService-info.plist

<key>FIREBASE_ANALYTICS_COLLECTION_ENABLED</key>
	<true></true>

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,815 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 46,981 Reputation points Microsoft Vendor
    2024-12-03T02:32:33.7033333+00:00

    Hello,

    I noticed that you are currently using the .net 9.0 framework. Currently, the latest versions of these two NugetPackages do not yet support .net 9.0, so this should be a compatibility issue.

    Regarding how to integrate FireBase Analytics into Maui project, you can refer to the solution in this thread.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.