Refactor anonymity derivation

pull/21833/head
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;
}

View File

@ -107,7 +107,7 @@ import UIStore, { UI_EVENTS } from "../../stores/UIStore";
import SoftLogout from './auth/SoftLogout';
import { makeRoomPermalink } from "../../utils/permalinks/Permalinks";
import { copyPlaintext } from "../../utils/strings";
import { Anonymity, getAnalytics, getPlatformProperties } from '../../PosthogAnalytics';
import { Anonymity, getAnalytics, getAnonymityFromSettings, getPlatformProperties } from '../../PosthogAnalytics';
/** constants for MatrixChat.state.view */
export enum Views {
@ -390,7 +390,9 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
const analytics = getAnalytics();
analytics.init(SettingsStore.getValue("analyticsOptIn") ? Anonymity.Pseudonymous : Anonymity.Anonymous);
analytics.init(getAnonymityFromSettings());
// note this requires a network request in the browser, so some events can potentially
// before before registerSuperProperties has been called
getPlatformProperties().then((properties) => analytics.registerSuperProperties(properties));
CountlyAnalytics.instance.enable(/* anonymous = */ true);