Xamarin表单中的Firebase Analytics

Ash*_*mar 12 firebase xamarin.forms firebase-analytics

我们是否可以获得自定义事件,例如在Xamarin Forms项目中使用Firebase Analytics按下按钮1?

Gia*_*bba 31

当然,您需要DI(依赖注入)来调用平台代码.

  • 引用Firebase Analytics的正确nuget包:
    • Xamarin.FireBase.Analytics
    • Xamarin.FireBase.Analytics.Impl
    • Xamarin.FireBase.iOS.Analytics
  • 在您的PCL(或.NETStandard)项目中创建界面
  • 在Android和iOS项目中编写plaftorm特定代码
  • 在您的视图模型中,在PCL(或.NETStandard)项目中使用DI来调用"LogEvent"方法来存储您的事件

PCL或.NETStandard项目

using System.Collections.Generic;

namespace MobileApp.Services
{
    public interface IAnalyticsService
    {
        void LogEvent(string eventId);
        void LogEvent(string eventId, string paramName, string value);
        void LogEvent(string eventId, IDictionary<string, string> parameters);
    }
}
Run Code Online (Sandbox Code Playgroud)

Android的

确保您的清单中具有INTERNET权限.

导入您的google-services.json(从您的firebase帐户生成),并将编译操作设置为"GoogleServicesJson"

这是自定义渲染器:

CrossCurrentActivity.Current.Init(this, bundle);
Run Code Online (Sandbox Code Playgroud)

iOS版

在base.FinishedLaunching之前初始化AppDelegate中的组件:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Android.OS;
using Firebase.Analytics;
using Plugin.CurrentActivity;
using MobileApp.Services;

namespace MobileApp.Droid.Services
{
    [assembly: Dependency (typeof(AnalyticsServiceDroid))]
    public class AnalyticsServiceDroid : IAnalyticsService
    {

        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 LogEvent(string eventId, IDictionary<string, string> parameters)
        {

            //utility method to fix eventId, you can skip it if you are sure to always pass valid eventIds
            eventId = FixEventId(eventId);

            var fireBaseAnalytics = FirebaseAnalytics.GetInstance(CrossCurrentActivity.Current.AppContext);

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

            var bundle = new Bundle();

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

            fireBaseAnalytics.LogEvent(eventId, bundle);
        }

        //utility method to fix eventId, you can skip it if you are sure to always pass valid eventIds
        private string FixEventId(string eventId)
        {
            if (string.IsNullOrWhiteSpace(eventId))
                return "unknown";

            //remove unwanted characters
            eventId = Regex.Replace(eventId, @"[^a-zA-Z0-9_]+", "_", RegexOptions.Compiled);

            //trim to 40 if needed
            return eventId.Substring(0, Math.Min(40, eventId.Length));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

然后导入您的GoogleService-Info.plist(从您的firebase帐户生成),并将编译操作设置为"BundleResource".

这是自定义渲染器:

adb shell setprop debug.firebase.analytics.app <package_name>
Run Code Online (Sandbox Code Playgroud)

在ViewModel中,您可以跟踪所需的任何事件

例如:

adb shell setprop debug.firebase.analytics.app .none.
Run Code Online (Sandbox Code Playgroud)

  • 我用它来跟踪分析数据(页面浏览量,用户参与度,自定义事件,屏幕时间,ecc ...)。上次我检查时,关于分析部分,appcenter落后于Firebase。我想我需要重新检查!也许您在谈论Firebase Crashlytics?这是另一个话题。Firebase崩溃报告已被打折并在几个月前退役。 (2认同)