简体   繁体   中英

How to trigger GA events when my android app is in kill mode?

I have implemented google analytics in my android application using Xamarin and it is working fine and I am able to trigger GA events while my app is in the background or running. The Only problem which, I am getting is that I am not able to Trigger my GA events when my app is in kill mode? When my app is in kill mode, I am sending local notification using a broadcast receiver in which, I want to trigger my GA events. I am not able to find anything relevant regarding this, could anyone help me out in this?

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

}

I have initialized my GA instance and trigger GA events in the below method in a separate class.

 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());
    }

Initiate GA in your onReceive method like this -

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

And then call the TrackEvent method, ensure that you have the TrackEvent method in your AlarmReceiver class as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM