Ash*_*mar 12 firebase xamarin.forms firebase-analytics
我们是否可以获得自定义事件,例如在Xamarin Forms项目中使用Firebase Analytics按下按钮1?
Gia*_*bba 31
当然,您需要DI(依赖注入)来调用平台代码.
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)
确保您的清单中具有INTERNET权限.
导入您的google-services.json(从您的firebase帐户生成),并将编译操作设置为"GoogleServicesJson"
这是自定义渲染器:
CrossCurrentActivity.Current.Init(this, bundle);
Run Code Online (Sandbox Code Playgroud)
在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)
例如:
adb shell setprop debug.firebase.analytics.app .none.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5918 次 |
| 最近记录: |