mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-18 23:07:02 +08:00
fix: mixpanel issues (#6840)
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
import { OnEvent, Service } from '@toeverything/infra';
|
||||
import type { QuotaQuery } from '@affine/graphql';
|
||||
import { createEvent, OnEvent, Service } from '@toeverything/infra';
|
||||
|
||||
import { UserQuota } from '../entities/user-quota';
|
||||
import { AccountChanged } from './auth';
|
||||
|
||||
type UserQuotaInfo = NonNullable<QuotaQuery['currentUser']>['quota'];
|
||||
|
||||
export const UserQuotaChanged = createEvent<UserQuotaInfo>('UserQuotaChanged');
|
||||
|
||||
@OnEvent(AccountChanged, e => e.onAccountChanged)
|
||||
export class UserQuotaService extends Service {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.quota.quota$.distinctUntilChanged().subscribe(q => {
|
||||
this.eventBus.emit(UserQuotaChanged, q);
|
||||
});
|
||||
}
|
||||
|
||||
quota = this.framework.createEntity(UserQuota);
|
||||
|
||||
private onAccountChanged() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { mixpanel } from '@affine/core/utils';
|
||||
import type { QuotaQuery } from '@affine/graphql';
|
||||
import { ApplicationStarted, OnEvent, Service } from '@toeverything/infra';
|
||||
|
||||
import {
|
||||
@@ -6,10 +7,15 @@ import {
|
||||
type AuthAccountInfo,
|
||||
type AuthService,
|
||||
} from '../../cloud';
|
||||
import { UserQuotaChanged } from '../../cloud/services/user-quota';
|
||||
|
||||
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
|
||||
@OnEvent(AccountChanged, e => e.onAccountChanged)
|
||||
@OnEvent(UserQuotaChanged, e => e.onUserQuotaChanged)
|
||||
export class TelemetryService extends Service {
|
||||
private prevQuota: NonNullable<QuotaQuery['currentUser']>['quota'] | null =
|
||||
null;
|
||||
|
||||
constructor(private readonly auth: AuthService) {
|
||||
super();
|
||||
}
|
||||
@@ -22,9 +28,7 @@ export class TelemetryService extends Service {
|
||||
});
|
||||
}
|
||||
const account = this.auth.session.account$.value;
|
||||
if (account) {
|
||||
mixpanel.identify(account.id);
|
||||
}
|
||||
this.onAccountChanged(account);
|
||||
}
|
||||
|
||||
onAccountChanged(account: AuthAccountInfo | null) {
|
||||
@@ -33,6 +37,22 @@ export class TelemetryService extends Service {
|
||||
} else {
|
||||
mixpanel.reset();
|
||||
mixpanel.identify(account.id);
|
||||
mixpanel.people.set({
|
||||
$email: account.email,
|
||||
$name: account.label,
|
||||
$avatar: account.avatar,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onUserQuotaChanged(quota: NonNullable<QuotaQuery['currentUser']>['quota']) {
|
||||
const plan = quota?.humanReadable.name;
|
||||
// only set when plan is not empty and changed
|
||||
if (plan !== this.prevQuota?.humanReadable.name && plan) {
|
||||
mixpanel.people.set({
|
||||
plan: quota?.humanReadable.name,
|
||||
});
|
||||
}
|
||||
this.prevQuota = quota;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user