fix: mixpanel issues (#6840)

This commit is contained in:
pengx17
2024-05-09 07:50:14 +00:00
parent 3744a0a5e0
commit 917ad1965a
7 changed files with 52 additions and 23 deletions

View File

@@ -1,6 +1,9 @@
import { DebugLogger } from '@affine/debug';
import type { OverridedMixpanel } from 'mixpanel-browser';
import mixpanelBrowser from 'mixpanel-browser';
const logger = new DebugLogger('affine:mixpanel');
export const mixpanel = process.env.MIXPANEL_TOKEN
? mixpanelBrowser
: new Proxy(
@@ -10,15 +13,19 @@ export const mixpanel = process.env.MIXPANEL_TOKEN
function createProxyHandler(property?: string | symbol) {
const handler = {
get: (_target, property) => {
get: (_target, childProperty) => {
const path = property
? String(property) + '.' + String(childProperty)
: String(childProperty);
return new Proxy(
function () {} as unknown as OverridedMixpanel,
createProxyHandler(property)
createProxyHandler(path)
);
},
apply: (_target, _thisArg, args) => {
console.info(
`Mixpanel is not initialized, calling ${property ? String(property) : 'mixpanel'} with args: ${JSON.stringify(args)}`
logger.debug(
`mixpanel.${property ? String(property) : 'mixpanel'}`,
...args
);
},
} as ProxyHandler<OverridedMixpanel>;