Firebase Analytics on Xamarin.Forms

Kako Botasso
Firebase Developers
4 min readMay 18, 2019

Every project we work on, we must understand the behavior and all moves of our users to focus our energy and time on improving his experience. You can use Firebase Analytics to track this information and I'll show how integrate it with Xamarin.Forms

Firebase Analytics + Xamarin.Forms

Let's start by creating a Xamarin.Forms project and use a Blank App Template. I called my project FireEvents. We'll see 3 kind of events that can be sent, so I changed my MainPage.xaml and added buttons to it and added handlers to them on my code behind

After that, we need to add some libraries to our project to integrate with Firebase. In our shared project we'll install Xamarin.Firebase.Core and in Android project we'll install Xamarin.FireBase.Analytics.Impl. After that, we'll add an interface to our shared project, who will be implemented by Android and iOS project.

In this interface, we have 3 methods. One for a simple event, like "About page opened" count event; one for an event with a single parameter, like a search event and you can pass query as parameter; and the other one is for an event that need many parameters, like sign up event, you can pass as parameter all the data that you saved in your database.

With the interface created, we need to implement it on the projects. First you go to FireEvents.Android and create an implementation to our interface

As you can see, we start our class adding a dependency to assembly. Without it, when we try to send events on click buttons, we'll have an error because the runtime could not find our implementation. The first 2 methods, we just prepare the parameters to call our last method, that have all rules.

We'll have to add the google-services.json to our project. After download it from Firebase console, we'll add it to our Android project. To Xamarin find this file, we'll have to update our FireEvents.Android.csproj by adding an ItemGroup with this file

Now that Android is done, let's play with iOS. First of all, we need to add a NuGet package called Xamarin.FireBase.iOS.Analytics and initialize Firebase on our AppDelegate.

To Firebase work well, we need to import GoogleService-Info.plist from our console. Add it to your project and change its Build Action to Bundle Resource by clicking with right button into it.

Now let's create an implementation to our interface, like we did on Android. Let's create a file to it and add the 3 methods with exactly the same idea of Android project

After it, we can go back to code behind from our MainPage and start sending events. We'll use our interface to call the methods that send events but, it won't work if we don't get the dependency that contains the implementation of this interface. After doing it on the constructor, we can use the send events methods on the handlers. If you’re using MVVM in your project, you can send the events from your ViewModel.

Firebase Analytics start showing our events 24 hours before we start sending it, but for testing, we can use Firebase Debug View. Open Firebase console, open Debug View and you'll a timeline with your events. Click the buttons and see the events. In the right side, you see the amount of events sent and, clicking on it, you can see details about these events.

Firebase Debug View after sending some events
Event Click03 Detail

Running the Android app, you'll not find any problem with Debug View, but if you're running iOS you have to add an extra configuration to run it on emulator. Go to Run > Run With > Custom Configuration and add the following to Extra mlaunch Arguments to be able to debug it:

Hope this article have helped and hope see you soon. If you have any doubt, don’t be afraid to ask in the comments. If you liked it, recommend and share :D

You can find the code from this article on Github:

--

--