Google Firebase Analytics issue on Xamarin MAUI

Bhuwan 636 Reputation points
2024-01-22T07:18:32.9333333+00:00

Hi i am integrating Google firebase analytics in Xamarin MAUI but getting null and also google-services.json and set build action to GoogleServicesJson.(GoogleServicesJson build action not exist).

https://dev.to/vhugogarcia/firebase-analytics-in-net-maui-4p6d

MauiProgram.cs

         IFirebaseAnalyticsService firebaseAnalyticsService = DependencyService.Get<IFirebaseAnalyticsService>();

i am getting null firebaseAnalyticsService

public static void LogScreenView(string screenName, string screenClass)
{
    try
    {
       
            IFirebaseAnalyticsService firebaseAnalyticsService = DependencyService.Get<IFirebaseAnalyticsService>();
            Dictionary<string, string> addtocartProp = new Dictionary<string, string>()
                        {
                            { "screen_name", screenName },
                            { "screen_class", screenClass }
                        };
            firebaseAnalyticsService.LogEvent("screen_view", addtocartProp);
        
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}
 public interface IFirebaseAnalyticsService
 {
     /// <summary>
     /// LogEvent
     /// </summary>
     /// <param name="eventId"></param>
     void LogEvent(string eventId);

     /// <summary>
     /// LogScreenViewEvent
     /// </summary>
     /// <param name="eventId"></param>
     /// <param name="parameters"></param>
     void LogEvent(string eventId, IDictionary<string, string> parameters);

     /// <summary>
     /// LogEvent
     /// </summary>
     /// <param name="eventId"></param>
     /// <param name="parameters"></param>
     /// <param name="items"></param>
     void LogEvent(string eventId, IDictionary<string, string> parameters, List<Constants.FirebaseClassData> items);
 }
            }).ConfigureLifecycleEvents(events =>
            {
#if IOS
                events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) =>
                {
                    Firebase.Core.App.Configure();
                    return false;
                }));
#endif
#if ANDROID
            events.AddAndroid(android => android.OnCreate((activity, bundle) => {
                    Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(activity, bundle);
                    Firebase.FirebaseApp.InitializeApp(activity);
                }));
#endif

#if IOS         builder.Services.AddSingleton<IFirebaseAnalyticsService, Platforms.iOS.Services.FirebaseAnalyticsService>(); #endif #if ANDROID builder.Services.AddSingleton<IFirebaseAnalyticsService, Platforms.Android.Services.FirebaseAnalyticsService>(); #endif
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2024-01-24T02:56:41.04+00:00

    Hello,

    Since you are using DependencyService, this error is expected.

    This feature has been deprecated in MAUI and replaced by dependency injection.

    For dependency injection, you need to call the service component that has been registered with the application in the following way:

    var settingsService = this.Handler.MauiContext.Services.GetServices<ISettingsService>();
    

    Please refer to Dependency injection - Resolution for more detailed information.

    Based on this known GitHub issue Build Action: BundleResource does not exist. #7938, there are two ways you could fix this issue.

    <ItemGroup Condition="$(TargetFramework.Contains('-ios'))">
    <MauiAsset Include="Platforms\iOS\GoogleService-Info.plist" LogicalName="GoogleService-Info.plist" />
    </ItemGroup>
    

    Or

        <ItemGroup>
          <BundleResource  Include="Platforms\iOS\GoogleService-Info.plist" Link="GoogleService-Info.plist" />
        </ItemGroup>
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful