Adding Google firebase analytics to Xamarin forms mobile app.
1. Nuget Packages
Install the following Nuget packages to your xamarin forms projects as follows.
- Install Xamarin.Firebase.iOS.Analytics (Version 6.9.0) in iOS project.
- Install Xamarin.Firebase.Analytics (Version 71.1630.4) and Plugin.CurrentActivity (Version 2.1.0.4) in Android project.
NOTE: For older Android phones, Nuget package Xamarin.Firebase.Analytics version later than 71.1630.4 didn’t work, so I end up installing this version, feel free to install the latest version of this package.
2. Google Firebase Console
Create an account and register your app in Google firebase console. Follow the instructions and download config files to disk.
- google-services.json for Android.
- GoogleService-Info.plist for iOS.
3. Implementation
1) Add google-services.json file to Android project and set its Build Action to GoogleServicesJson under Properties.
NOTE: If this option does not show up under Build Action dropdown list, closing and restarting Visual Studio should help. If not, edit MyXamarinApp.Android.csproj file and add the following lines:
<ItemGroup>
<GoogleServicesJson Include="google-services.json" />
</ItemGroup>
2) Add GoogleService-Info.plist file to iOS project and set its Build Action to BundleResource under Properties.
3) In your shared project, add new interface called IFirebaseAnalyticsService.cs and add the following code:
C#
public interface IFirebaseAnalyticsService
{
void LogEvent(string eventId);
void LogEvent(string eventId, string paramName, string value);
void LogEvent(string eventId, IDictionary<string, string> parameters);
}
4) Register this interface as Dependency in you shared project App.xaml.cs file
C#
DependencyService.Register<IFirebaseAnalyticsService>();
5) Add new class FirebaseAnalyticsService.cs to Android project and add the following implementation:
C#
using Firebase.Analytics;
using Plugin.CurrentActivity;
using MyXamarinApp.Droid;
// Add Dependency
[assembly: Xamarin.Forms.Dependency(typeof(FirebaseAnalyticsService))]
namespace MyXamarinApp.Droid
{
public class FirebaseAnalyticsService : IFirebaseAnalyticsService
{
public void LogEvent(string eventId)
{
if (!string.IsNullOrWhiteSpace(eventId))
{
// Get instance
var fb = Firebase.Analytics.FirebaseAnalytics
.GetInstance(CrossCurrentActivity.Current.AppContext);
// Log event
fb.LogEvent(eventId, null);
}
}
public void LogEvent(string eventId, string paramName, string value)
{
if (!string.IsNullOrWhiteSpace(eventId)
&& !string.IsNullOrWhiteSpace(paramName)
&& !string.IsNullOrWhiteSpace(value))
{
var bundle = new Bundle();
bundle.PutString(paramName, value);
// Get instance
var fb = Firebase.Analytics.FirebaseAnalytics
.GetInstance(CrossCurrentActivity.Current.AppContext);
// Log event
fb.LogEvent(eventId, bundle);
}
}
public void LogEvent(string eventId, IDictionary<string, string> parameters)
{
if (!string.IsNullOrWhiteSpace(eventId)
&& parameters != null
&& parameters.Any())
{
var bundle = new Bundle();
foreach (var param in parameters)
{
bundle.PutString(param.Key, param.Value);
}
// Get instance
var fb = Firebase.Analytics.FirebaseAnalytics
.GetInstance(CrossCurrentActivity.Current.AppContext);
// Log event
fb.LogEvent(eventId, bundle);
}
}
}
}
6) Add new class FirebaseAnalyticsService.cs to iOS project and add the following implementation:
C#
using MyXamarinApp.iOS;
using Firebase.Analytics;
// Add Dependency
[assembly: Xamarin.Forms.Dependency(typeof(FirebaseAnalyticsService))]
namespace MyXamarinApp.iOS
{
public class FirebaseAnalyticsService : IFirebaseAnalyticsService
{
public void LogEvent(string eventId)
{
if (!string.IsNullOrWhiteSpace(eventId))
{
// Log event
Analytics.LogEvent(eventId, new NSDictionary<NSString, NSObject>());
}
}
public void LogEvent(string eventId, string paramName, string value)
{
if (!string.IsNullOrWhiteSpace(eventId)
&& !string.IsNullOrWhiteSpace(paramName)
&& !string.IsNullOrWhiteSpace(value))
{
var dict = new Dictionary<object, object>();
dict.Add(paramName, value);
// Log event
Analytics.LogEvent(eventId, dict);
}
}
public void LogEvent(string eventId, IDictionary<string, string> parameters)
{
if (!string.IsNullOrWhiteSpace(eventId)
&& parameters != null
&& parameters.Any())
{
// Log event
Analytics.LogEvent(eventId, (Dictionary<object, object>)parameters);
}
}
}
}
7) For adding custom events, go to your view models in share code and simply log any event as follows:
C#
// Add dependency
public IFirebaseAnalyticsService firebaseAnalyticsService =>
DependencyService.Get<IFirebaseAnalyticsService>();
And for events add the following code in functions:
C#
// Log firebase event
firebaseAnalyticsService.LogEvent("event", "name", "value");
These events will then show up under Google Firebase Console.
Happy Coding :)