2016-08-04 04:25:01 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Google.Analytics;
|
|
|
|
|
|
|
|
|
|
namespace Bit.iOS.Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class GoogleAnalyticsService : IGoogleAnalyticsService
|
|
|
|
|
{
|
|
|
|
|
private readonly ITracker _tracker;
|
|
|
|
|
private readonly IAuthService _authService;
|
2016-08-04 07:06:09 +03:00
|
|
|
|
private bool _setUserId = true;
|
2016-08-04 04:25:01 +03:00
|
|
|
|
|
|
|
|
|
public GoogleAnalyticsService(
|
|
|
|
|
IAppIdService appIdService,
|
|
|
|
|
IAuthService authService)
|
|
|
|
|
{
|
|
|
|
|
_authService = authService;
|
|
|
|
|
|
|
|
|
|
Gai.SharedInstance.DispatchInterval = 10;
|
|
|
|
|
Gai.SharedInstance.TrackUncaughtExceptions = true;
|
|
|
|
|
_tracker = Gai.SharedInstance.GetTracker("UA-81915606-1");
|
|
|
|
|
_tracker.Set(GaiConstants.ClientId, appIdService.AppId);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 07:06:09 +03:00
|
|
|
|
public void RefreshUserId()
|
|
|
|
|
{
|
|
|
|
|
_tracker.Set(GaiConstants.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)
|
|
|
|
|
{
|
|
|
|
|
TrackEvent("Extension", eventName, label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackEvent(string category, string eventName, string label = null)
|
2016-08-04 04:25:01 +03:00
|
|
|
|
{
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
2016-08-04 07:25:10 +03:00
|
|
|
|
var dict = DictionaryBuilder.CreateEvent(category, eventName, label, null).Build();
|
2016-08-04 04:25:01 +03:00
|
|
|
|
_tracker.Send(dict);
|
|
|
|
|
Gai.SharedInstance.Dispatch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackException(string message, bool fatal)
|
|
|
|
|
{
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
2016-08-04 04:25:01 +03:00
|
|
|
|
var dict = DictionaryBuilder.CreateException(message, fatal).Build();
|
|
|
|
|
_tracker.Send(dict);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TrackPage(string pageName)
|
|
|
|
|
{
|
2016-08-04 07:06:09 +03:00
|
|
|
|
SetUserId();
|
2016-08-04 04:25:01 +03:00
|
|
|
|
_tracker.Set(GaiConstants.ScreenName, pageName);
|
|
|
|
|
var dict = DictionaryBuilder.CreateScreenView().Build();
|
|
|
|
|
_tracker.Send(dict);
|
|
|
|
|
}
|
2016-08-04 07:06:09 +03:00
|
|
|
|
|
|
|
|
|
private void SetUserId()
|
|
|
|
|
{
|
|
|
|
|
if(_setUserId && _authService.IsAuthenticated)
|
|
|
|
|
{
|
|
|
|
|
_tracker.Set(GaiConstants.UserId, _authService.UserId);
|
|
|
|
|
_setUserId = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-04 04:25:01 +03:00
|
|
|
|
}
|
|
|
|
|
}
|