Problem with Firebase Analytics (Android) on Xamarin.Forms

JassimRahmaJassimRahma USMember ✭✭✭✭
edited February 2018 in Xamarin.Forms

Hi,

I am trying to implement Firebase Analytics in my Android project in Xamarin Forms...

My Dependency:

using System;
using Android.Content;
using Xamarin.Forms;
using Firebase.Analytics;
using Firebase.Crash;
using Android.OS;

[assembly: Dependency(typeof(ConvertAny.Droid.FirebaseService))]
namespace ConvertAny.Droid
{
    public class FirebaseService : IFirebaseService
    {
        FirebaseAnalytics firebaseAnalytics;

        public void Track_App_Page(String PageNameToTrack)
        {
            var bundle = new Bundle();
            bundle.PutString(FirebaseAnalytics.Param.ItemId, "1");
            bundle.PutString(FirebaseAnalytics.Param.ItemName, PageNameToTrack);

            firebaseAnalytics.LogEvent(FirebaseAnalytics.Event.SelectContent, bundle);
        }
    }
}

In MainActivity:

FirebaseAnalytics firebaseAnalytics;

protected override void OnCreate(Bundle bundle)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(bundle);

    global::Xamarin.Forms.Forms.Init(this, bundle);

    firebaseAnalytics = FirebaseAnalytics.GetInstance(this);
    ...........
    ...........
    ...........

In my Home.xaml.cs:

FirebaseDependencyService.Track_App_Page("Pages - Home");

But I am getting:

Object reference not set to an instance of an object.

On this line:

firebaseAnalytics.LogEvent(FirebaseAnalytics.Event.SelectContent, bundle);

Can anyone help please

Thanks,
Jassim

Answers

  • ilanolkiesilanolkies ARMember ✭✭

    I use it in another order and it works:

    protected override void OnCreate(Bundle bundle)
    {
        var firebase = FirebaseAnalytics.GetInstance(this);
        firebase.LogEvent(FirebaseAnalytics.Event.AppOpen, null);
    
        base.OnCreate(bundle);
    
        global::Xamarin.Forms.Forms.Init(this, bundle);
    
        LoadApplication(new App());
    }
    
  • DevanshuDevanshu Member
    edited June 2018

    Update you method of Firebase service as below:

    public void Track_App_Page(String PageNameToTrack)
    {
    firebaseAnalytics= FirebaseAnalytics.GetInstance(Forms.Context);
    var bundle = new Bundle();
    bundle.PutString(FirebaseAnalytics.Param.ItemId, "1");
    bundle.PutString(FirebaseAnalytics.Param.ItemName, PageNameToTrack);
    firebaseAnalytics.LogEvent(FirebaseAnalytics.Event.SelectContent, bundle);
    }

Sign In or Register to comment.