2016-08-04 04:25:01 +03:00
|
|
|
|
using System;
|
2017-03-31 01:22:14 +03:00
|
|
|
|
using Bit.App;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2017-03-31 01:22:14 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
using Android.Gms.Analytics;
|
|
|
|
|
using Android.Content;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Services
|
|
|
|
|
{
|
|
|
|
|
public class GoogleAnalyticsService : IGoogleAnalyticsService
|
|
|
|
|
{
|
2016-08-04 15:46:53 +03:00
|
|
|
|
private readonly GoogleAnalytics _instance;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
private readonly Tracker _tracker;
|
|
|
|
|
|
|
|
|
|
public GoogleAnalyticsService(
|
|
|
|
|
Context appContext,
|
|
|
|
|
IAppIdService appIdService,
|
2017-03-31 01:22:14 +03:00
|
|
|
|
IAuthService authService,
|
|
|
|
|
ISettings settings)
|
2016-08-04 04:25:01 +03:00
|
|
|
|
{
|
|
|
|
|
_authService = authService;
|
|
|
|
|
|
2016-08-04 15:46:53 +03:00
|
|
|
|
_instance = GoogleAnalytics.GetInstance(appContext.ApplicationContext);
|
|
|
|
|
_instance.SetLocalDispatchPeriod(10);
|
2016-08-04 04:25:01 +03:00
|
|
|
|
|
2016-08-04 15:46:53 +03:00
|
|
|
|
_tracker = _instance.NewTracker("UA-81915606-2");
|
2017-04-24 23:04:54 +03:00
|
|
|
|
_tracker.EnableExceptionReporting(false);
|
2016-08-04 04:25:01 +03:00
|
|
|
|
_tracker.EnableAdvertisingIdCollection(true);
|
|
|
|
|
_tracker.EnableAutoActivityTracking(true);
|
2016-08-07 02:03:48 +03:00
|
|
|
|
_tracker.SetClientId(appIdService.AnonymousAppId);
|
2017-03-31 01:22:14 +03:00
|
|
|
|
|
2017-04-20 04:05:03 +03:00
|
|
|
|
var gaOptOut = settings.GetValueOrDefault(Constants.SettingGaOptOut, false);
|
2017-03-31 01:22:14 +03:00
|
|
|
|
SetAppOptOut(gaOptOut);
|
2016-08-04 04:25:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 07:25:10 +03:00
|
|
|
|
public void TrackAppEvent(string eventName, string label = null)
|
|
|
|
|
{
|
|
|
|
|
TrackEvent("App", eventName, label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackExtensionEvent(string eventName, string label = null)
|
|
|
|
|
{
|
2017-02-06 07:59:43 +03:00
|
|
|
|
TrackEvent("AutofillService", eventName, label);
|
2016-08-04 07:25:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackEvent(string category, string eventName, string label = null)
|
2016-08-04 04:25:01 +03:00
|
|
|
|
{
|
|
|
|
|
var builder = new HitBuilders.EventBuilder();
|
|
|
|
|
builder.SetCategory(category);
|
|
|
|
|
builder.SetAction(eventName);
|
2016-08-04 07:25:10 +03:00
|
|
|
|
if(label != null)
|
|
|
|
|
{
|
|
|
|
|
builder.SetLabel(label);
|
|
|
|
|
}
|
2016-08-04 04:25:01 +03:00
|
|
|
|
|
|
|
|
|
_tracker.Send(builder.Build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackException(string message, bool fatal)
|
|
|
|
|
{
|
|
|
|
|
var builder = new HitBuilders.ExceptionBuilder();
|
|
|
|
|
builder.SetDescription(message);
|
|
|
|
|
builder.SetFatal(fatal);
|
|
|
|
|
|
|
|
|
|
_tracker.Send(builder.Build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackPage(string pageName)
|
|
|
|
|
{
|
2016-08-04 07:06:09 +03:00
|
|
|
|
_tracker.SetScreenName(pageName);
|
|
|
|
|
_tracker.Send(new HitBuilders.ScreenViewBuilder().Build());
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 02:35:56 +03:00
|
|
|
|
public void Dispatch(Action completionHandler = null)
|
2016-08-04 15:46:53 +03:00
|
|
|
|
{
|
|
|
|
|
_instance.DispatchLocalHits();
|
2016-08-05 02:35:56 +03:00
|
|
|
|
completionHandler?.Invoke();
|
2016-08-04 15:46:53 +03:00
|
|
|
|
}
|
2017-03-31 01:22:14 +03:00
|
|
|
|
|
|
|
|
|
public void SetAppOptOut(bool optOut)
|
|
|
|
|
{
|
|
|
|
|
_instance.AppOptOut = optOut;
|
|
|
|
|
}
|
2016-08-04 04:25:01 +03:00
|
|
|
|
}
|
|
|
|
|
}
|