Refactor platform properties loading

pull/21833/head
James Salter 2021-07-21 18:35:25 +01:00
parent 585b702652
commit c34afdb4bd
3 changed files with 36 additions and 5 deletions

View File

@ -48,7 +48,7 @@ import { Jitsi } from "./widgets/Jitsi";
import { SSO_HOMESERVER_URL_KEY, SSO_ID_SERVER_URL_KEY, SSO_IDP_ID_KEY } from "./BasePlatform"; import { SSO_HOMESERVER_URL_KEY, SSO_ID_SERVER_URL_KEY, SSO_IDP_ID_KEY } from "./BasePlatform";
import ThreepidInviteStore from "./stores/ThreepidInviteStore"; import ThreepidInviteStore from "./stores/ThreepidInviteStore";
import CountlyAnalytics from "./CountlyAnalytics"; import CountlyAnalytics from "./CountlyAnalytics";
import { getAnalytics } from "./PosthogAnalytics"; import { Anonymity, getAnalytics, getPlatformProperties } from "./PosthogAnalytics";
import CallHandler from './CallHandler'; import CallHandler from './CallHandler';
import LifecycleCustomisations from "./customisations/Lifecycle"; import LifecycleCustomisations from "./customisations/Lifecycle";
import ErrorDialog from "./components/views/dialogs/ErrorDialog"; import ErrorDialog from "./components/views/dialogs/ErrorDialog";
@ -574,6 +574,14 @@ async function doSetLoggedIn(
await abortLogin(); await abortLogin();
} }
if (SettingsStore.getValue("analyticsOptIn")) {
const analytics = getAnalytics();
analytics.setAnonymity(Anonymity.Pseudonymous);
await analytics.identifyUser(credentials.userId);
} else {
getAnalytics().setAnonymity(Anonymity.Anonymous);
}
Analytics.setLoggedIn(credentials.guest, credentials.homeserverUrl); Analytics.setLoggedIn(credentials.guest, credentials.homeserverUrl);
MatrixClientPeg.replaceUsingCreds(credentials); MatrixClientPeg.replaceUsingCreds(credentials);

View File

@ -1,4 +1,5 @@
import posthog, { PostHog } from 'posthog-js'; import posthog, { PostHog } from 'posthog-js';
import PlatformPeg from './PlatformPeg';
import SdkConfig from './SdkConfig'; import SdkConfig from './SdkConfig';
interface IEvent { interface IEvent {
@ -226,6 +227,22 @@ export class PosthogAnalytics {
} }
} }
export async function getPlatformProperties() {
const platform = PlatformPeg.get();
let appVersion;
try {
appVersion = await platform.getAppVersion();
} catch (e) {
// this happens if no version is set i.e. in dev
appVersion = "unknown";
}
return {
appVersion,
appPlatform: platform.getHumanReadableName(),
};
}
export function getAnalytics(): PosthogAnalytics { export function getAnalytics(): PosthogAnalytics {
return PosthogAnalytics.instance(); return PosthogAnalytics.instance();
} }

View File

@ -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, IPageChange } from '../../PosthogAnalytics'; import { Anonymity, getAnalytics, getPlatformProperties } from '../../PosthogAnalytics';
/** constants for MatrixChat.state.view */ /** constants for MatrixChat.state.view */
export enum Views { export enum Views {
@ -388,7 +388,11 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
if (SettingsStore.getValue("analyticsOptIn")) { if (SettingsStore.getValue("analyticsOptIn")) {
Analytics.enable(); Analytics.enable();
} }
getAnalytics().init(SettingsStore.getValue("analyticsOptIn") ? Anonymity.Pseudonymous : Anonymity.Anonymous);
const analytics = getAnalytics();
analytics.init(SettingsStore.getValue("analyticsOptIn") ? Anonymity.Pseudonymous : Anonymity.Anonymous);
getPlatformProperties().then((properties) => analytics.registerSuperProperties(properties));
CountlyAnalytics.instance.enable(/* anonymous = */ true); CountlyAnalytics.instance.enable(/* anonymous = */ true);
} }
@ -501,8 +505,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
} else if (SettingsStore.getValue("analyticsOptIn")) { } else if (SettingsStore.getValue("analyticsOptIn")) {
CountlyAnalytics.instance.enable(/* anonymous = */ false); CountlyAnalytics.instance.enable(/* anonymous = */ false);
} }
getAnalytics().setAnonymity(SettingsStore.getValue("analyticsOptIn") ?
Anonymity.Pseudonymous: Anonymity.Anonymous);
}); });
// Note we don't catch errors from this: we catch everything within // Note we don't catch errors from this: we catch everything within
// loadSession as there's logic there to ask the user if they want // loadSession as there's logic there to ask the user if they want
@ -828,6 +830,10 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
CountlyAnalytics.instance.enable(/* anonymous = */ false); CountlyAnalytics.instance.enable(/* anonymous = */ false);
} }
getAnalytics().setAnonymity(Anonymity.Pseudonymous); getAnalytics().setAnonymity(Anonymity.Pseudonymous);
// TODO: this is an async call and we're not waiting for it to complete -
// so potentially an event could be fired prior to it completing and would be
// missing the user identification.
getAnalytics().identifyUser(MatrixClientPeg.get().getUserId());
break; break;
case 'reject_cookies': case 'reject_cookies':
SettingsStore.setValue("analyticsOptIn", null, SettingLevel.DEVICE, false); SettingsStore.setValue("analyticsOptIn", null, SettingLevel.DEVICE, false);