feat(core): add globalcontext info to mixpanel track (#7681)

This commit is contained in:
EYHN
2024-08-01 09:29:31 +00:00
parent bb767a6cdc
commit 553fbed60f
10 changed files with 126 additions and 74 deletions
@@ -1,14 +1,8 @@
import { type Framework, WorkspaceScope } from '@toeverything/infra';
import { type Framework, GlobalContextService } from '@toeverything/infra';
import { AuthService } from '../cloud';
import {
TelemetryService,
TelemetryWorkspaceContextService,
} from './services/telemetry';
import { TelemetryService } from './services/telemetry';
export function configureTelemetryModule(framework: Framework) {
framework.service(TelemetryService, [AuthService]);
framework
.scope(WorkspaceScope)
.service(TelemetryWorkspaceContextService, [WorkspaceScope]);
framework.service(TelemetryService, [AuthService, GlobalContextService]);
}
@@ -1,12 +1,7 @@
import { mixpanel } from '@affine/core/mixpanel';
import type { QuotaQuery } from '@affine/graphql';
import type { WorkspaceScope } from '@toeverything/infra';
import {
ApplicationStarted,
DocsService,
OnEvent,
Service,
} from '@toeverything/infra';
import type { GlobalContextService } from '@toeverything/infra';
import { ApplicationStarted, OnEvent, Service } from '@toeverything/infra';
import {
AccountChanged,
@@ -15,8 +10,6 @@ import {
} from '../../cloud';
import { AccountLoggedOut } from '../../cloud/services/auth';
import { UserQuotaChanged } from '../../cloud/services/user-quota';
import { resolveRouteLinkMeta } from '../../navigation';
import { WorkbenchService } from '../../workbench';
@OnEvent(ApplicationStarted, e => e.onApplicationStart)
@OnEvent(AccountChanged, e => e.updateIdentity)
@@ -25,14 +18,19 @@ import { WorkbenchService } from '../../workbench';
export class TelemetryService extends Service {
private prevQuota: NonNullable<QuotaQuery['currentUser']>['quota'] | null =
null;
private readonly disposables: (() => void)[] = [];
constructor(private readonly auth: AuthService) {
constructor(
private readonly auth: AuthService,
private readonly globalContextService: GlobalContextService
) {
super();
}
onApplicationStart() {
const account = this.auth.session.account$.value;
this.updateIdentity(account);
this.registerMiddlewares();
}
updateIdentity(account: AuthAccountInfo | null) {
@@ -61,39 +59,41 @@ export class TelemetryService extends Service {
}
this.prevQuota = quota;
}
}
// get telemetry related context in Workspace scope
export class TelemetryWorkspaceContextService extends Service {
constructor(private readonly provider: WorkspaceScope) {
super();
registerMiddlewares() {
this.disposables.push(
mixpanel.middleware((_event, parameters) => {
const extraContext = this.extractGlobalContext();
return {
...extraContext,
...parameters,
};
})
);
}
getPageContext() {
const workbench = this.provider?.getOptional(WorkbenchService)?.workbench;
const docs = this.provider?.getOptional(DocsService);
extractGlobalContext() {
const globalContext = this.globalContextService.globalContext;
const page = globalContext.isDoc.get()
? globalContext.isTrashDoc.get()
? 'trash'
: globalContext.docMode.get() === 'page'
? 'doc editor'
: 'whiteboard editor'
: globalContext.isAllDocs.get()
? 'doc library'
: globalContext.isTrash.get()
? 'trash library'
: globalContext.isCollection.get()
? 'collection detail'
: globalContext.isTag.get()
? 'tag detail'
: 'unknown';
return { page, activePage: page };
}
if (!workbench || !docs) return '';
const basename = workbench.basename$.value;
const path = workbench.location$.value;
const fullPath = basename + path.pathname + path.search + path.hash;
const linkMeta = resolveRouteLinkMeta(fullPath);
return (() => {
const moduleName =
linkMeta?.moduleName === 'doc'
? docs.list.getMode(linkMeta.docId)
: linkMeta?.moduleName;
switch (moduleName) {
case 'page':
return 'doc editor';
case 'edgeless':
return 'whiteboard editor';
case 'trash':
return 'trash';
default:
return 'doc library';
}
})();
override dispose(): void {
this.disposables.forEach(dispose => dispose());
super.dispose();
}
}