Refactor anonymity derivation
parent
95f4275807
commit
5e0a397631
|
@ -1,6 +1,7 @@
|
||||||
import posthog, { PostHog } from 'posthog-js';
|
import posthog, { PostHog } from 'posthog-js';
|
||||||
import PlatformPeg from './PlatformPeg';
|
import PlatformPeg from './PlatformPeg';
|
||||||
import SdkConfig from './SdkConfig';
|
import SdkConfig from './SdkConfig';
|
||||||
|
import SettingsStore from './settings/SettingsStore';
|
||||||
|
|
||||||
interface IEvent {
|
interface IEvent {
|
||||||
// The event name that will be used by PostHog.
|
// 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 {
|
export class PosthogAnalytics {
|
||||||
private anonymity = Anonymity.Anonymous;
|
private anonymity = Anonymity.Anonymous;
|
||||||
private initialised = false;
|
|
||||||
private posthog?: PostHog = null;
|
private posthog?: PostHog = null;
|
||||||
|
|
||||||
|
// set true during init() if posthog config is present
|
||||||
private enabled = false;
|
private enabled = false;
|
||||||
|
|
||||||
|
// set to true after init() has been called
|
||||||
|
private initialised = false;
|
||||||
|
|
||||||
private static _instance = null;
|
private static _instance = null;
|
||||||
|
|
||||||
public static instance(): PosthogAnalytics {
|
public static instance(): PosthogAnalytics {
|
||||||
|
@ -155,7 +160,9 @@ export class PosthogAnalytics {
|
||||||
}
|
}
|
||||||
|
|
||||||
public registerSuperProperties(properties) {
|
public registerSuperProperties(properties) {
|
||||||
this.posthog.register(properties);
|
if (this.enabled) {
|
||||||
|
this.posthog.register(properties);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public isInitialised() {
|
public isInitialised() {
|
||||||
|
@ -248,3 +255,24 @@ export async function getPlatformProperties() {
|
||||||
export function getAnalytics(): PosthogAnalytics {
|
export function getAnalytics(): PosthogAnalytics {
|
||||||
return PosthogAnalytics.instance();
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -107,7 +107,7 @@ import UIStore, { UI_EVENTS } from "../../stores/UIStore";
|
||||||
import SoftLogout from './auth/SoftLogout';
|
import SoftLogout from './auth/SoftLogout';
|
||||||
import { makeRoomPermalink } from "../../utils/permalinks/Permalinks";
|
import { makeRoomPermalink } from "../../utils/permalinks/Permalinks";
|
||||||
import { copyPlaintext } from "../../utils/strings";
|
import { copyPlaintext } from "../../utils/strings";
|
||||||
import { Anonymity, getAnalytics, getPlatformProperties } from '../../PosthogAnalytics';
|
import { Anonymity, getAnalytics, getAnonymityFromSettings, getPlatformProperties } from '../../PosthogAnalytics';
|
||||||
|
|
||||||
/** constants for MatrixChat.state.view */
|
/** constants for MatrixChat.state.view */
|
||||||
export enum Views {
|
export enum Views {
|
||||||
|
@ -390,7 +390,9 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const analytics = getAnalytics();
|
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));
|
getPlatformProperties().then((properties) => analytics.registerSuperProperties(properties));
|
||||||
|
|
||||||
CountlyAnalytics.instance.enable(/* anonymous = */ true);
|
CountlyAnalytics.instance.enable(/* anonymous = */ true);
|
||||||
|
|
Loading…
Reference in New Issue