Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

PosthogAnalytics unwatch settings on logout #11207

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,12 @@ let _isLoggingOut = false;
* Logs the current session out and transitions to the logged-out state
*/
export function logout(): void {
if (!MatrixClientPeg.get()) return;
const client = MatrixClientPeg.get();
if (!client) return;

PosthogAnalytics.instance.logout();

if (MatrixClientPeg.get()!.isGuest()) {
if (client.isGuest()) {
// logout doesn't work for guest sessions
// Also we sometimes want to re-log in a guest session if we abort the login.
// defer until next tick because it calls a synchronous dispatch, and we are likely here from a dispatch.
Expand All @@ -761,7 +762,6 @@ export function logout(): void {
}

_isLoggingOut = true;
const client = MatrixClientPeg.get()!;
PlatformPeg.get()?.destroyPickleKey(client.getSafeUserId(), client.getDeviceId() ?? "");
client.logout(true).then(onLoggedOut, (err) => {
// Just throwing an error here is going to be very unhelpful
Expand Down
4 changes: 3 additions & 1 deletion src/PosthogAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class PosthogAnalytics {
private propertiesForNextEvent: Partial<Record<"$set" | "$set_once", UserProperties>> = {};
private userPropertyCache: UserProperties = {};
private authenticationType: Signup["authenticationType"] = "Other";
private watchSettingRef?: string;

public static get instance(): PosthogAnalytics {
if (!this._instance) {
Expand Down Expand Up @@ -337,6 +338,7 @@ export class PosthogAnalytics {
if (this.enabled) {
this.posthog.reset();
}
if (this.watchSettingRef) SettingsStore.unwatchSetting(this.watchSettingRef);
this.setAnonymity(Anonymity.Disabled);
}

Expand Down Expand Up @@ -400,7 +402,7 @@ export class PosthogAnalytics {
// * When the user changes their preferences on this device
// Note that for new accounts, pseudonymousAnalyticsOptIn won't be set, so updateAnonymityFromSettings
// won't be called (i.e. this.anonymity will be left as the default, until the setting changes)
SettingsStore.watchSetting(
this.watchSettingRef = SettingsStore.watchSetting(
"pseudonymousAnalyticsOptIn",
null,
(originalSettingName, changedInRoomId, atLevel, newValueAtLevel, newValue) => {
Expand Down