Refactor anonymity derivation

This commit is contained in:
James Salter 2021-07-23 17:58:31 +01:00
parent 95f4275807
commit 5e0a397631
2 changed files with 34 additions and 4 deletions

View file

@ -1,6 +1,7 @@
import posthog, { PostHog } from 'posthog-js';
import PlatformPeg from './PlatformPeg';
import SdkConfig from './SdkConfig';
import SettingsStore from './settings/SettingsStore';
interface IEvent {
// The event name that will be used by PostHog.
@ -78,10 +79,14 @@ export async function getRedactedCurrentLocation(origin: string, hash: string, p
export class PosthogAnalytics {
private anonymity = Anonymity.Anonymous;
private initialised = false;
private posthog?: PostHog = null;
// set true during init() if posthog config is present
private enabled = false;
// set to true after init() has been called
private initialised = false;
private static _instance = null;
public static instance(): PosthogAnalytics {
@ -155,7 +160,9 @@ export class PosthogAnalytics {
}
public registerSuperProperties(properties) {
this.posthog.register(properties);
if (this.enabled) {
this.posthog.register(properties);
}
}
public isInitialised() {
@ -248,3 +255,24 @@ export async function getPlatformProperties() {
export function getAnalytics(): PosthogAnalytics {
return PosthogAnalytics.instance();
}
export function getAnonymityFromSettings(): Anonymity {
// determine the current anonymity level based on curernt user settings
// "Send anonymous usage data which helps us improve Element. This will use a cookie."
const analyticsOptIn = SettingsStore.getValue("analyticsOptIn");
// "Send pseudonymous usage data which helps us improve Element. This will use a cookie."
const pseudonumousOptIn = SettingsStore.getValue("pseudonymousAnalyticsOptIn");
let anonymity;
if (pseudonumousOptIn) {
anonymity = Anonymity.Pseudonymous;
} else if (analyticsOptIn) {
anonymity = Anonymity.Anonymous;
} else {
anonymity = Anonymity.Disabled;
}
return anonymity;
}