Skip to content

Commit

Permalink
feat: Firebase Analytics (#2497)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhunty authored Sep 5, 2024
1 parent 47aa232 commit 69e1404
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 74 deletions.
3 changes: 2 additions & 1 deletion projects/Mallard/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isValid } from './authentication/lib/Attempt';
import { oktaInitialisation } from './authentication/services/okta';
import { BugButtonHandler } from './components/Button/BugButtonHandler';
import { ErrorBoundary } from './components/layout/ui/errors/error-boundary';
import { logUserId } from './helpers/analytics';
import { logUserId, toggleAnalyticsRecording } from './helpers/analytics';
import { prepFileSystem } from './helpers/files';
import { nestProviders } from './helpers/provider';
import { AppStateProvider } from './hooks/use-app-state-provider';
Expand All @@ -30,6 +30,7 @@ import { WeatherProvider } from './hooks/use-weather-provider';
import { remoteConfigService } from './services/remote-config';

remoteConfigService.init();
toggleAnalyticsRecording(true);

// --- SETUP OPERATIONS ---
prepFileSystem();
Expand Down
9 changes: 2 additions & 7 deletions projects/Mallard/src/components/Button/BugButtonHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ const BugButtonHandler = () => {
type,
};

const {
gdprAllowEssential,
gdprAllowPerformance,
gdprAllowFunctionality,
gdprConsentVersion,
} = useGdprSettings();
const { gdprAllowEssential, gdprAllowPerformance, gdprConsentVersion } =
useGdprSettings();
const gdprSettings = {
gdprAllowEssential,
gdprAllowPerformance,
gdprAllowFunctionality,
gdprConsentVersion,
};
return isInBeta() ? (
Expand Down
4 changes: 0 additions & 4 deletions projects/Mallard/src/helpers/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ const maxAvailableEditionsCache = createAsyncCache<number>(
const gdprAllowPerformanceCache = createAsyncCache<boolean>(
'@Setting_gdprAllowPerformance',
);
const gdprAllowFunctionalityCache = createAsyncCache<boolean>(
'@Setting_gdprAllowFunctionality',
);
const gdprConsentVersionCache = createAsyncCache<number>(
'@Setting_gdprConsentVersion',
);
Expand Down Expand Up @@ -204,7 +201,6 @@ export {
wifiOnlyDownloadsCache,
maxAvailableEditionsCache,
gdprAllowPerformanceCache,
gdprAllowFunctionalityCache,
gdprConsentVersionCache,
isWeatherShownCache,
isUsingProdDevtoolsCache,
Expand Down
45 changes: 2 additions & 43 deletions projects/Mallard/src/hooks/use-gdpr.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { toggleAnalyticsRecording } from '../helpers/analytics';
import {
gdprAllowFunctionalityCache,
gdprAllowPerformanceCache,
gdprConsentVersionCache,
} from '../helpers/storage';
Expand All @@ -20,8 +18,9 @@ import { errorService } from '../services/errors';
* v8 - Add Firebase Analytics as PERFORMANCE
* v9 - Remove additional Logging
* v10 - Remove Sentry from the app
* V11 - Make Firebase analytics required, Remove Sentry, Remove Functionality Section
*/
const CURRENT_CONSENT_VERSION = 10;
const CURRENT_CONSENT_VERSION = 11;

/*
Consent switches can be 'unset' or null
Expand All @@ -31,7 +30,6 @@ export type GdprSwitchSetting = null | boolean;
export type GdprCoreSettings = {
gdprAllowEssential: GdprSwitchSetting;
gdprAllowPerformance: GdprSwitchSetting;
gdprAllowFunctionality: GdprSwitchSetting;
gdprConsentVersion: number | null;
};

Expand All @@ -43,7 +41,6 @@ export enum OnboardingStatus {
}

interface GdprSettings extends GdprCoreSettings {
setGdprFunctionalityBucket: (setting: GdprSwitchSetting) => void;
setGdprPerformanceBucket: (setting: GdprSwitchSetting) => void;
enableAllSettings: () => void;
rejectAllSettings: () => void;
Expand All @@ -54,15 +51,12 @@ interface GdprSettings extends GdprCoreSettings {

export type GdprSwitches = {
gdprAllowPerformance: GdprSettings['gdprAllowPerformance'];
gdprAllowFunctionality: GdprSettings['gdprAllowFunctionality'];
};

const defaultState: GdprSettings = {
gdprAllowEssential: true, // essential defaults to true and not switchable
gdprAllowPerformance: null,
gdprAllowFunctionality: null,
gdprConsentVersion: null,
setGdprFunctionalityBucket: () => {},
setGdprPerformanceBucket: () => {},
enableAllSettings: () => {},
rejectAllSettings: () => {},
Expand All @@ -77,9 +71,6 @@ export const GDPRProvider = ({ children }: { children: React.ReactNode }) => {
const [gdprAllowPerformance, setGdprAllowPerformance] = useState<
GdprSettings['gdprAllowPerformance']
>(defaultState.gdprAllowPerformance);
const [gdprAllowFunctionality, setGdprAllowFunctionality] = useState<
GdprSettings['gdprAllowFunctionality']
>(defaultState.gdprAllowFunctionality);
const [gdprConsentVersion, setGdprConsentVersion] = useState<
GdprSettings['gdprConsentVersion']
>(defaultState.gdprConsentVersion);
Expand All @@ -97,30 +88,18 @@ export const GDPRProvider = ({ children }: { children: React.ReactNode }) => {
.get()
.then((val) => setGdprConsentVersion(val))
.catch((e) => errorService.captureException(e)),
gdprAllowFunctionalityCache
.get()
.then((val) => setGdprAllowFunctionality(val))
.catch((e) => errorService.captureException(e)),
]).finally(() => setLoading(false));
}, []);

const hasSetGdpr = () => {
if (!loading) {
if (
gdprAllowFunctionality != null &&
gdprAllowPerformance != null &&
gdprConsentVersion === CURRENT_CONSENT_VERSION
) {
return OnboardingStatus.Complete;
}

if (
gdprAllowFunctionality != null ||
gdprAllowPerformance != null
) {
return OnboardingStatus.InProgress;
}

return OnboardingStatus.NotStarted;
}
return OnboardingStatus.Unknown;
Expand All @@ -130,35 +109,19 @@ export const GDPRProvider = ({ children }: { children: React.ReactNode }) => {
// Local state modifier
setGdprAllowPerformance(setting);
setGdprConsentVersion(CURRENT_CONSENT_VERSION);
setting
? toggleAnalyticsRecording(setting)
: toggleAnalyticsRecording(false);
// Persisted state modifier
setting === null
? gdprAllowPerformanceCache.reset()
: gdprAllowPerformanceCache.set(setting);
gdprConsentVersionCache.set(CURRENT_CONSENT_VERSION);
};

const setGdprFunctionalityBucket = (setting: GdprSwitchSetting) => {
// Local state modifier
setGdprAllowFunctionality(setting);
setGdprConsentVersion(CURRENT_CONSENT_VERSION);
// Persisted state modifier
setting === null
? gdprAllowFunctionalityCache.reset()
: gdprAllowFunctionalityCache.set(setting);
gdprConsentVersionCache.set(CURRENT_CONSENT_VERSION);
};

const allSettings = (modifier: boolean) => {
// Local state modifier
setGdprAllowPerformance(modifier);
setGdprAllowFunctionality(modifier);
setGdprConsentVersion(CURRENT_CONSENT_VERSION);
// Persisted state modifier
gdprAllowPerformanceCache.set(modifier);
gdprAllowFunctionalityCache.set(modifier);
gdprConsentVersionCache.set(CURRENT_CONSENT_VERSION);
};

Expand All @@ -169,11 +132,9 @@ export const GDPRProvider = ({ children }: { children: React.ReactNode }) => {
const resetAllSettings = () => {
// Local state modifier
setGdprAllowPerformance(defaultState.gdprAllowPerformance);
setGdprAllowFunctionality(defaultState.gdprAllowFunctionality);
setGdprConsentVersion(defaultState.gdprConsentVersion);
// Persisted state modifier
gdprAllowPerformanceCache.reset();
gdprAllowFunctionalityCache.reset();
gdprConsentVersionCache.reset();
};

Expand All @@ -185,9 +146,7 @@ export const GDPRProvider = ({ children }: { children: React.ReactNode }) => {
value={{
gdprAllowEssential: defaultState.gdprAllowEssential,
gdprAllowPerformance,
gdprAllowFunctionality,
gdprConsentVersion,
setGdprFunctionalityBucket,
setGdprPerformanceBucket,
enableAllSettings,
rejectAllSettings,
Expand Down
14 changes: 2 additions & 12 deletions projects/Mallard/src/screens/settings/gdpr-consent-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type EssentialGdprSwitch = Omit<GdprSwitch, 'key' | 'modifier' | 'value'>;
const essentials: EssentialGdprSwitch = {
name: 'Essential',
services:
'YouTube Player - Firebase Cloud Messaging - Firebase Remote Config',
'YouTube Player - Firebase Cloud Messaging - Firebase Remote Config - Firebase Analytics',
description:
'These are essential to provide you with services that you have requested. These services support the ability for you to watch videos, see service-related messages, download content automatically and receive new features without app releases.',
};
Expand All @@ -61,30 +61,20 @@ const GdprConsent = ({
rejectAllSettings,
resetAllSettings,
gdprAllowPerformance,
gdprAllowFunctionality,
setGdprPerformanceBucket,
setGdprFunctionalityBucket,
hasSetGdpr,
isCorrectConsentVersion,
} = useGdprSettings();

const switches: { [key in keyof GdprSwitches]: GdprSwitch } = {
gdprAllowPerformance: {
name: 'Performance',
services: 'Sentry - Crashlytics',
services: 'Crashlytics',
description:
'Enabling these allow us to observe and measure how you use our services. We use this information to fix bugs more quickly so that users have a better experience. For example, we would be able to see the journey you have taken and where the error was encountered. Your data will only be stored in our servers for two weeks. If you disable this, we will not be able to observe and measure your use of our services, and we will have less information about their performance and details of any issues encountered.',
modifier: setGdprPerformanceBucket,
value: gdprAllowPerformance,
},
gdprAllowFunctionality: {
name: 'Functionality',
services: 'Apple - Google',
description:
'Enabling these allow us to provide extra sign-in functionality. It enables us to offer alternative options for you to sign-in to your Guardian account using your Apple or Google credentials. If you disable this, you won’t be able to sign-in with the third-party services above.',
modifier: setGdprFunctionalityBucket,
value: gdprAllowFunctionality,
},
};

const onEnableAllAndContinue = () => {
Expand Down
9 changes: 2 additions & 7 deletions projects/Mallard/src/screens/settings/help-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ const HelpScreen = () => {
downloadBlocked,
isInternetReachable,
};
const {
gdprAllowEssential,
gdprAllowPerformance,
gdprAllowFunctionality,
gdprConsentVersion,
} = useGdprSettings();
const { gdprAllowEssential, gdprAllowPerformance, gdprConsentVersion } =
useGdprSettings();
const gdprSettings = {
gdprAllowEssential,
gdprAllowPerformance,
gdprAllowFunctionality,
gdprConsentVersion,
};

Expand Down

0 comments on commit 69e1404

Please sign in to comment.