I am trying to use firebase analytics for xamatir forms, but any tutorial I found is outdated.
I already created a firebase project, download the json and the firebase crashanalytics is working fine. Now for the firebase analytics in the same project i installed the Xamarin.Firebase.Analytics nugget version 121.3.0.5 in the Android project. I am using xamarin forms 5.0.0.2578. I have the 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);
}
and the class:
using Firebase.Analytics;
using Soft1.Droid.Services;
using Soft1.Droid.Services.Dialogs;
using Soft1.Interfaces;
using System.Collections.Generic;
using Xamarin.Forms;
[assembly: Dependency(typeof(FirebaseAnalyticsService))]
namespace Soft1.Droid.Services
{
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)
{
var fireBaseAnalytics = FirebaseAnalytics.GetInstance(Xamarin.Essentials.Platform.CurrentActivity);
fireBaseAnalytics.SetUserId(userId);
}
public void LogEvent(string eventId, IDictionary<string, string> parameters)
{
var fireBaseAnalytics = FirebaseAnalytics.GetInstance(Xamarin.Essentials.Platform.CurrentActivity);
if (parameters == null)
{
fireBaseAnalytics.LogEvent(eventId, null);
return;
}
var bundle = new Android.OS.Bundle();
foreach (var item in parameters)
{
bundle.PutString(item.Key, item.Value);
}
fireBaseAnalytics.LogEvent(eventId, bundle);
}
}
}
In the mainactivity i declare
FirebaseAnalytics firebaseAnalytics;
and then in the create method :
firebaseAnalytics = FirebaseAnalytics.GetInstance(this);
Inside my project i tried:
IFirebaseAnalyticsService service = DependencyService.Get<IFirebaseAnalyticsService>();
service.LogEvent("login event", "paramname1", "111");
I do not get any erros but in the firebase project console i do not get any updates. I tried also with the debugView, run : adb shell setprop debug.firebase.analytics.app **testappname **, but with no success.
Elias Koukoulas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.