2016-08-04 04:25:01 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Android.Gms.Analytics;
|
|
|
|
|
using Android.Content;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Services
|
|
|
|
|
{
|
|
|
|
|
public class GoogleAnalyticsService : IGoogleAnalyticsService
|
|
|
|
|
{
|
|
|
|
|
private const string UserId = "&uid";
|
|
|
|
|
|
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
private readonly Tracker _tracker;
|
2016-08-04 07:06:09 +03:00
|
|
|
|
private bool _setUserId = true;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
|
|
|
|
|
public GoogleAnalyticsService(
|
|
|
|
|
Context appContext,
|
|
|
|
|
IAppIdService appIdService,
|
|
|
|
|
IAuthService authService)
|
|
|
|
|
{
|
|
|
|
|
_authService = authService;
|
|
|
|
|
|
|
|
|
|
var instance = GoogleAnalytics.GetInstance(appContext.ApplicationContext);
|
|
|
|
|
instance.SetLocalDispatchPeriod(10);
|
|
|
|
|
|
|
|
|
|
_tracker = instance.NewTracker("UA-81915606-2");
|
|
|
|
|
_tracker.EnableExceptionReporting(true);
|
|
|
|
|
_tracker.EnableAdvertisingIdCollection(true);
|
|
|
|
|
_tracker.EnableAutoActivityTracking(true);
|
|
|
|
|
_tracker.SetClientId(appIdService.AppId);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 07:06:09 +03:00
|
|
|
|
public void RefreshUserId()
|
|
|
|
|
{
|
|
|
|
|
_tracker.Set(UserId, null);
|
|
|
|
|
_setUserId = true;
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
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);
|
|
|
|
|
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
2016-08-04 04:25:01 +03:00
|
|
|
|
_tracker.Send(builder.Build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackPage(string pageName)
|
|
|
|
|
{
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
|
|
|
|
_tracker.SetScreenName(pageName);
|
|
|
|
|
_tracker.Send(new HitBuilders.ScreenViewBuilder().Build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetUserId()
|
|
|
|
|
{
|
|
|
|
|
if(_setUserId && _authService.IsAuthenticated)
|
2016-08-04 04:25:01 +03:00
|
|
|
|
{
|
|
|
|
|
_tracker.Set(UserId, _authService.UserId);
|
2016-08-04 07:06:09 +03:00
|
|
|
|
_setUserId = false;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|