How to integrate Xamarin.Firebase.Analytics to the Xamarin.forms?

Viewed 473

Problem: But I don't see an active user in my firebase account.

I do this.

  1. I add package the Xamarin.Firebase.Analytics to the MyName.Android project.

  2. I add the google-services.json in My Name.Android.

  3. Install build Action = GoogleService.json

  4. I run the project as "Debug".

description of the problem: The project is successfully assembled and running, but the standard actions in the firebase statistics are not fixed: first_open, screen_view, session_start.

description of the problem: The firebase statistics in the realtime section are also empty.

Please tell me what I'm doing wrong? Do I need additional packages? "Xamarin.Firebase.Analytics" need to be initialized in MainActivity?

Developers create nuget packages and upload them to github. Some developers write instructions. And some do not write instructions. The question is where to look for instructions?

2 Answers

I remember using the following medium post to do the same: https://thewissen.io/using-firebase-analytics-in-your-xamarin-forms-app/

Basically, you perform the prerequisites, you create an interface

public interface IFirebaseAnalyticsService
{
   void LogEvent(string eventId);
   void LogEvent(string eventId, string paramName, string value);
   void LogEvent(string eventId, IDictionary<string, string> parameters);
   void SetUserId(string userId);
}

Then you create platform level classes for dependency injection

public class FirebaseAnalyticsService : IFirebaseAnalyticsService
{
public void LogEvent(string eventId)
{
    LogEvent(eventId, null);
}

public void LogEvent(string eventId, string paramName, string value)
{
    LogEvent(eventId, new Dictionary<string, string>
    {
        {paramName, value}
    });
}

public void SetUserId(string userId)
{
#if !DEBUG
    var fireBaseAnalytics = FirebaseAnalytics.GetInstance(CrossCurrentActivity.Current.AppContext);

    fireBaseAnalytics.SetUserId(userId);
#endif
}

public void LogEvent(string eventId, IDictionary<string, string> parameters)
{
 #if !DEBUG
    var fireBaseAnalytics = FirebaseAnalytics.GetInstance(CrossCurrentActivity.Current.AppContext);

    if (parameters == null)
    {
        fireBaseAnalytics.LogEvent(eventId, null);
        return;
    }

    var bundle = new Bundle();

    foreach (var item in parameters)
    {
        bundle.PutString(item.Key, item.Value);
    }

    fireBaseAnalytics.LogEvent(eventId, bundle);
 #endif
}
}

Good luck Lemme know if you have any queries

Add to MainAvtivity.cs (for Android)

  1. using Firebase.Analytics;
  2. FirebaseAnalytics firebaseAnalytics; to the beginning of class MainActivity
  3. to the method onCreate firebaseAnalytics = FirebaseAnalytics.GetInstance(this);
  4. in the properties of the google-service.json file, set build action = Google Services.Json
  5. look at my package versions. This link is working. I'm tired of picking up packages...

look at my package versions. This link is working. I'm tired of picking up packages...

Add to AppDelegate.cs (for iOS)

  1. before namespace add the following − [assembly: Preserve(typeof(Firebase.Analytics.Analytics), AllMembers = true)]
  2. before return base.FinishedLaunching(app, options); add the code Firebase.Core.App.Configure();
  3. i use Xamarin.Firebase.iOS.Anatics(6.9.0)

The rest is according to the instructions in firebase for ios

Related