M Vignesh
M Vignesh

Reputation: 1646

Integrate Firebase Analytics with Xamarin Android

There is no official documentation for analytics integration with xamarin. Only Xamarin Google Analytics Sample is available officially.

But we found an official Firebase nuget package Xamarin.Firebase.Analytics has been available for analytics which is provided by microsoft.

But there is no proper document to configure firebase with xamarin. We have created app id and .json file got generated from firebase console for our application. But there is no reference to configure these in xamarimn android app.

Upvotes: 3

Views: 3844

Answers (1)

FreakyAli
FreakyAli

Reputation: 16449

This is because you do not need to have a official documentation for basic Android code conversion anything as simple as tracking code that is used by Android studio is directly available in the C# side:

Add the FirebaseAnalytics global field in your activity like this:

FirebaseAnalytics firebaseAnalytics;

Then get its instance in Activity Context :

firebaseAnalytics = FirebaseAnalytics.GetInstance(this);

Then define a bundle and log events:

var bundle = new Bundle();
bundle.PutString(FirebaseAnalytics.Param.ItemId, "1");
bundle.PutString(FirebaseAnalytics.Param.ItemName, PageNameToTrack);
firebaseAnalytics.LogEvent(FirebaseAnalytics.Event.SelectContent, bundle);

Reference can be found here: https://firebase.google.com/docs/analytics/android/start/

Update

In context to the GoogleServices.Json just add it to your root folder of the project and set build action as GoogleServicesJson

Follow the following steps:

  • Copy google-services.json to the project folder.

  • Add google-services.json to the app project (click Show All Files in the Solution Explorer, right-click google-services.json, then select Include in Project).

  • Select google-services.json in the Solution Explorer window.

  • In the Properties pane, set the Build Action to GoogleServicesJson:

Official quote from FCM:

When google-services.json is added to the project (and the GoogleServicesJson build action is set), the build process extracts the client ID and API key and then adds these credentials to the merged/generated AndroidManifest.xml that resides at obj/Debug/android/AndroidManifest.xml.

For more information

Upvotes: 5

Related Questions