If you’re building a .NET MAUI app and want to track user behavior, integrating Google Analytics is a must. In this guide, I’ll walk you through the steps to set up Google Analytics, integrate it into your app, and track custom events with just a few lines of code.
đź’ˇ Note: You need to set up Firebase first to get the necessary configuration files. If you haven’t done that yet, check out the linked video tutorial for step-by-step instructions.
Step 1: Add Firebase Configuration Files
Before integrating Firebase Analytics, you need to include the Firebase configuration files in your project:
- For Android:
google-services.json
- For iOS:
GoogleService-Info.plist
These files are generated in your Firebase Console when setting up your app. If you’re unsure how to get them, watch the video tutorial linked above.
Once you have the files, add them to your .csproj file like this:
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'">
<GoogleServicesJson Include="google-services.json" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-ios'">
<BundleResource Include="GoogleService-Info.plist" />
</ItemGroup>
Step 2: Install the Firebase Analytics NuGet Package
Now, install the required package to enable Firebase Analytics in your project: Plugin.Firebase.Analytics
This package simplifies working with Google Analytics in .NET MAUI.
Step 3: Configure Firebase in Your .NET MAUI App
To enable Firebase Analytics, modify your MauiProgram.cs
file as follows:
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using Plugin.Firebase.Analytics;
#if ANDROID
using Plugin.Firebase.Core.Platforms.Android;
#elif IOS
using Plugin.Firebase.Core.Platforms.iOS;
#endif
namespace GoogleAnalyticsMAUISample
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.RegisterFirebaseServices()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
{
builder.ConfigureLifecycleEvents(events => {
#if IOS
events.AddiOS(iOS => iOS.WillFinishLaunching((_, __) => {
CrossFirebase.Initialize();
return false;
}));
#elif ANDROID
events.AddAndroid(android => android.OnCreate((activity, _) =>
{
CrossFirebase.Initialize(activity);
FirebaseAnalyticsImplementation.Initialize(activity);
}));
#endif
});
return builder;
}
}
}
Step 4: Track Custom Events in Your App
Now that Firebase is set up, you can track custom events. Here’s how to log a button click event:
private void OnCounterClicked(object sender, EventArgs e)
{
CrossFirebaseAnalytics.Current.LogEvent("counter_clicked", new Dictionary<string, object>()
{
{ "count", ++count }
});
}
Step 5: Test Your Integration
To ensure everything works:
- Run your app on a real device or emulator.
- Open the Firebase Console and navigate to Analytics → DebugView.
- Trigger events in your app and check if they appear in real time.
If your events aren’t showing up, double-check your Firebase configuration and ensure the files are correctly included.
Conclusion
Congrats! 🎉 You’ve successfully integrated Google Analytics into your .NET MAUI app. Now, you can track user behavior and make data-driven decisions to improve your app.
💬 Got questions or want more MAUI tutorials? Drop a comment below! 🚀