Created
October 30, 2022 23:20
-
-
Save vhugogarcia/abbdafa01d17c754eab747e06d2c7653 to your computer and use it in GitHub Desktop.
.NET MAUI Firebase Analytics Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Maui.Controls.Compatibility; | |
using Firebase.Analytics; | |
#if IOS | |
using Foundation; | |
#else | |
using Android.Content; | |
using Android.OS; | |
#endif | |
namespace DemoApp.Services.Firebase; | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment