首页 > 解决方案 > 当我的 android 应用程序处于终止模式时如何触发 GA 事件?

问题描述

我已经使用 Xamarin 在我的 android 应用程序中实现了谷歌分析,它工作正常,我能够在我的应用程序处于后台或运行时触发 GA 事件。我得到的唯一问题是当我的应用程序处于终止模式时我无法触发我的 GA 事件?当我的应用程序处于终止模式时,我正在使用广播接收器发送本地通知,我想在其中触发我的 GA 事件。我找不到与此相关的任何信息,有人可以帮助我吗?

  public class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent){
      //My implementation over here, in which I want to trigger GA events.
    }

}

我已经初始化了我的 GA 实例并在单独的类中的以下方法中触发了 GA 事件。

 public IAnalyticsManager InitWithId(string analyiticsId)
    {
        if(Android.App.Application.Context==null)
        {
            gaInstance = GoogleAnalytics.GetInstance(context);
        }
        else
        {
            gaInstance = GoogleAnalytics.GetInstance(Android.App.Application.Context);
        }
       //
        gaInstance.SetLocalDispatchPeriod(10);

        tracker = gaInstance.NewTracker(analyiticsId);
        tracker.EnableExceptionReporting(true);
        return this;
    }public void TrackEvent(String screenName, String lineNo,IDictionary<string,string> allEvents)
    {
        HitBuilders.EventBuilder builder = new HitBuilders.EventBuilder();
        builder.SetCategory(screenName);
        builder.SetAction(lineNo);
        builder.SetAll(allEvents);

        builder.SetLabel(allEvents[EMADefines.KeyTimeStamp]);
        tracker.SetClientId(allEvents[EMADefines.KeyUserName]);
        tracker.Send(builder.Build());
    }

标签: androidgoogle-analyticsxamarin.android

解决方案


onReceive像这样在您的方法中启动 GA -

gaInstance = GoogleAnalytics.GetInstance(context.getApplicationContext());
gaInstance.SetLocalDispatchPeriod(10);
tracker = gaInstance.NewTracker(analyiticsId);
tracker.EnableExceptionReporting(true);

然后调用该TrackEvent方法,确保TrackEvent您的类中也有该方法AlarmReceiver


推荐阅读