Forum Xamarin Xamarin.Forms

SetCurrentScreen is not working

JassimRahmaJassimRahma USMember ✭✭✭✭

Hi,

I am trying to use the SetCurrentScreen with Xamarin Forms but I am not getting anything logged in my Analytics

Here is my Android class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Analytics;
using Jafaria;
using Xamarin.Forms;

[assembly: Xamarin.Forms.Dependency(typeof(Jafaria.Droid.GoogleFirebaseAnalytics))]
namespace Jafaria.Droid
{
    public class GoogleFirebaseAnalytics : IFirebaseAnalytics
    {
        // Event Analytics
        public void LogEvent(string eventId)
        {
            LogEvent(eventId, null);
        }

        public void LogEvent(string eventId, string paramName, string value)
        {
            LogEvent(eventId, new Dictionary<string, string>
            {
                {paramName, value}
            });
        }

        public void LogScreen(string screenName, string screenClass)
        {
            var fireBaseAnalytics = FirebaseAnalytics.GetInstance(Android.App.Application.Context);

            fireBaseAnalytics.SetCurrentScreen(new Android.App.Activity(), screenName, screenClass);
        }

        public void LogEvent(string eventId, IDictionary<string, string> parameters)
        {
            var fireBaseAnalytics = FirebaseAnalytics.GetInstance(Android.App.Application.Context);

            if (parameters == null)
            {
                fireBaseAnalytics.LogEvent(eventId, null);
                return;
            }

            var bundle = new Bundle();

            // foreach (var item in parameters)
            foreach (var param in parameters)
            {
                bundle.PutString(param.Key, param.Value);
            }

            fireBaseAnalytics.LogEvent(eventId, bundle);
        }
    }
}

and my Interface:

using System;
using System.Collections.Generic;
using System.Text;

namespace Jafaria
{
    public interface IFirebaseAnalytics
    {
        void LogEvent(string eventId);
        void LogEvent(string eventId, string paramName, string value);
        void LogEvent(string eventId, IDictionary<string, string> parameters);

        void LogScreen(string screenName, string screenClass);
    }
}

then in my Page I am calling:

DependencyService.Get<IFirebaseAnalytics>().LogScreen(this.Title, this.Title);

Kindly help..

Thanks,
Jassim

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to use the Singleton class to save the FirebaseAnalytics and Activity, and maintain the properties be unique.

    public class AnalyticsSingleton
    {
        public static AnalyticsSingleton GetInstance { get; } = new AnalyticsSingleton();
    
        public FirebaseAnalytics Analytics { get; set; }
        public Activity Activity { get; set; }
    
        private AnalyticsSingleton()
        {
        }
    }
    

    MainActivity class

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
    
        AnalyticsSingleton.GetInstance.Analytics = FirebaseAnalytics.GetInstance(this);
        AnalyticsSingleton.GetInstance.Activity = this;
    
        base.OnCreate(savedInstanceState);
    
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    

    Android.IAnalytics class:

    [assembly: Dependency(typeof(Analytics_Android))]
    namespace FASample.Droid
    {
        public class Analytics_Android : IAnalytics
        {
            ...
            public void Screen(string screenName)
            {
                if (screenName == null) return;
    
                var analytics = AnalyticsSingleton.GetInstance.Analytics;
                var activity = AnalyticsSingleton.GetInstance.Activity;
    
                analytics.SetCurrentScreen(activity, screenName, null);
            }
        }
    }
    

    Check the tutorial:
    https://dev.classmethod.jp/smartphone/xamarin-forms-firebase-analytics-getting-started/

Sign In or Register to comment.