From fb35b9b10a01c8669f3bec016f90d7316e103aac Mon Sep 17 00:00:00 2001
From: Shan <11812r@gmail.com>
Date: Fri, 31 Mar 2017 03:52:14 +0530
Subject: [PATCH] Fixes #8 Disable Google Analytics (#55)
* Opt-out of Google Analytics
* Move OptOut to Other in Settings
* Change OptOut Order and Resource key
---
.../Services/GoogleAnalyticsService.cs | 13 ++++++++++-
.../Services/IGoogleAnalyticsService.cs | 1 +
src/App/Constants.cs | 1 +
src/App/Pages/Settings/SettingsPage.cs | 22 +++++++++++++++++++
src/App/Resources/AppResources.Designer.cs | 9 ++++++++
src/App/Resources/AppResources.resx | 3 +++
.../Services/GoogleAnalyticsService.cs | 12 +++++++++-
7 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/src/Android/Services/GoogleAnalyticsService.cs b/src/Android/Services/GoogleAnalyticsService.cs
index a944c7150..56c381d90 100644
--- a/src/Android/Services/GoogleAnalyticsService.cs
+++ b/src/Android/Services/GoogleAnalyticsService.cs
@@ -1,5 +1,7 @@
using System;
+using Bit.App;
using Bit.App.Abstractions;
+using Plugin.Settings.Abstractions;
using Android.Gms.Analytics;
using Android.Content;
@@ -17,7 +19,8 @@ namespace Bit.Android.Services
public GoogleAnalyticsService(
Context appContext,
IAppIdService appIdService,
- IAuthService authService)
+ IAuthService authService,
+ ISettings settings)
{
_authService = authService;
@@ -29,6 +32,9 @@ namespace Bit.Android.Services
_tracker.EnableAdvertisingIdCollection(true);
_tracker.EnableAutoActivityTracking(true);
_tracker.SetClientId(appIdService.AnonymousAppId);
+
+ var gaOptOut = settings.GetValueOrDefault(Constants.SettingGAOptOut, false);
+ SetAppOptOut(gaOptOut);
}
public void RefreshUserId()
@@ -92,5 +98,10 @@ namespace Bit.Android.Services
_instance.DispatchLocalHits();
completionHandler?.Invoke();
}
+
+ public void SetAppOptOut(bool optOut)
+ {
+ _instance.AppOptOut = optOut;
+ }
}
}
diff --git a/src/App/Abstractions/Services/IGoogleAnalyticsService.cs b/src/App/Abstractions/Services/IGoogleAnalyticsService.cs
index 32f56a1fb..b7453fbd8 100644
--- a/src/App/Abstractions/Services/IGoogleAnalyticsService.cs
+++ b/src/App/Abstractions/Services/IGoogleAnalyticsService.cs
@@ -11,5 +11,6 @@ namespace Bit.App.Abstractions
void TrackEvent(string category, string eventName, string label = null);
void TrackException(string message, bool fatal);
void Dispatch(Action completionHandler = null);
+ void SetAppOptOut(bool optOut);
}
}
diff --git a/src/App/Constants.cs b/src/App/Constants.cs
index 9afca298f..dcbdcce88 100644
--- a/src/App/Constants.cs
+++ b/src/App/Constants.cs
@@ -7,6 +7,7 @@
public const string SettingFingerprintUnlockOn = "setting:fingerprintUnlockOn";
public const string SettingPinUnlockOn = "setting:pinUnlockOn";
public const string SettingLockSeconds = "setting:lockSeconds";
+ public const string SettingGAOptOut = "setting:googleAnalyticsOptOut";
public const string PasswordGeneratorLength = "pwGenerator:length";
public const string PasswordGeneratorUppercase = "pwGenerator:uppercase";
diff --git a/src/App/Pages/Settings/SettingsPage.cs b/src/App/Pages/Settings/SettingsPage.cs
index eebd94b94..6a8a75d9a 100644
--- a/src/App/Pages/Settings/SettingsPage.cs
+++ b/src/App/Pages/Settings/SettingsPage.cs
@@ -40,6 +40,7 @@ namespace Bit.App.Pages
private ExtendedTextCell TwoStepCell { get; set; }
private ExtendedTextCell ChangeMasterPasswordCell { get; set; }
private ExtendedTextCell ChangeEmailCell { get; set; }
+ private ExtendedSwitchCell AnalyticsCell { get; set; }
private ExtendedTextCell FoldersCell { get; set; }
private ExtendedTextCell SyncCell { get; set; }
private ExtendedTextCell LockCell { get; set; }
@@ -103,6 +104,12 @@ namespace Bit.App.Pages
ShowDisclousure = true
};
+ AnalyticsCell = new ExtendedSwitchCell
+ {
+ Text = AppResources.DisableGA,
+ On = _settings.GetValueOrDefault(Constants.SettingGAOptOut, false)
+ };
+
FoldersCell = new ExtendedTextCell
{
Text = AppResources.Folders,
@@ -139,6 +146,7 @@ namespace Bit.App.Pages
var otherSection = new TableSection(AppResources.Other)
{
+ AnalyticsCell,
AboutCell,
HelpCell
};
@@ -193,6 +201,7 @@ namespace Bit.App.Pages
base.OnAppearing();
PinCell.OnChanged += PinCell_Changed;
+ AnalyticsCell.OnChanged += AnalyticsCell_Changed;
LockOptionsCell.Tapped += LockOptionsCell_Tapped;
TwoStepCell.Tapped += TwoStepCell_Tapped;
ChangeMasterPasswordCell.Tapped += ChangeMasterPasswordCell_Tapped;
@@ -226,6 +235,7 @@ namespace Bit.App.Pages
base.OnDisappearing();
PinCell.OnChanged -= PinCell_Changed;
+ AnalyticsCell.OnChanged -= AnalyticsCell_Changed;
LockOptionsCell.Tapped -= LockOptionsCell_Tapped;
TwoStepCell.Tapped -= TwoStepCell_Tapped;
ChangeMasterPasswordCell.Tapped -= ChangeMasterPasswordCell_Tapped;
@@ -411,6 +421,18 @@ namespace Bit.App.Pages
}
}
+ private void AnalyticsCell_Changed(object sender, ToggledEventArgs e)
+ {
+ var cell = sender as ExtendedSwitchCell;
+ if (cell == null)
+ {
+ return;
+ }
+
+ _settings.AddOrUpdateValue(Constants.SettingGAOptOut, cell.On);
+ _googleAnalyticsService.SetAppOptOut(cell.On);
+ }
+
private void PinEntered(SettingsPinPage page)
{
page.PinControl.Entry.Unfocus();
diff --git a/src/App/Resources/AppResources.Designer.cs b/src/App/Resources/AppResources.Designer.cs
index 99bfbbf35..18373f29d 100644
--- a/src/App/Resources/AppResources.Designer.cs
+++ b/src/App/Resources/AppResources.Designer.cs
@@ -520,6 +520,15 @@ namespace Bit.App.Resources {
}
}
+ ///
+ /// Looks up a localized string similar to Disable Google Analytics.
+ ///
+ public static string DisableGA {
+ get {
+ return ResourceManager.GetString("DisableGA", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to Do you really want to delete? This cannot be undone..
///
diff --git a/src/App/Resources/AppResources.resx b/src/App/Resources/AppResources.resx
index b4072d42c..335eceaa0 100644
--- a/src/App/Resources/AppResources.resx
+++ b/src/App/Resources/AppResources.resx
@@ -302,6 +302,9 @@
Ok
Acknowledgement.
+
+ Disable Google Analytics
+
Password
Label for a password.
diff --git a/src/iOS.Core/Services/GoogleAnalyticsService.cs b/src/iOS.Core/Services/GoogleAnalyticsService.cs
index d8acf424e..b02be905f 100644
--- a/src/iOS.Core/Services/GoogleAnalyticsService.cs
+++ b/src/iOS.Core/Services/GoogleAnalyticsService.cs
@@ -1,6 +1,7 @@
using System;
using Bit.App.Abstractions;
using Google.Analytics;
+using Plugin.Settings.Abstractions;
namespace Bit.iOS.Core.Services
{
@@ -12,7 +13,8 @@ namespace Bit.iOS.Core.Services
public GoogleAnalyticsService(
IAppIdService appIdService,
- IAuthService authService)
+ IAuthService authService,
+ ISettings settings)
{
_authService = authService;
@@ -21,6 +23,9 @@ namespace Bit.iOS.Core.Services
_tracker = Gai.SharedInstance.GetTracker("UA-81915606-1");
_tracker.SetAllowIdfaCollection(true);
_tracker.Set(GaiConstants.ClientId, appIdService.AnonymousAppId);
+
+ var gaOptOut = settings.GetValueOrDefault(App.Constants.SettingGAOptOut, false);
+ SetAppOptOut(gaOptOut);
}
public void RefreshUserId()
@@ -78,5 +83,10 @@ namespace Bit.iOS.Core.Services
_setUserId = false;
}
}
+
+ public void SetAppOptOut(bool optOut)
+ {
+ Gai.SharedInstance.OptOut = optOut;
+ }
}
}