Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

chore: track doc create action in bs editor (#6915)

pengx17dd45c80cc44b
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts
index 7771d2c987..360d5d7fca 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/specs.ts
@@ -1,12 +1,16 @@
import type { ElementOrFactory } from '@affine/component';
+import { mixpanel } from '@affine/core/utils';
import type { BlockSpec } from '@blocksuite/block-std';
import type { ParagraphService, RootService } from '@blocksuite/blocks';
import {
+ AffineLinkedDocWidget,
+ AffineSlashMenuWidget,
AttachmentService,
CanvasTextFonts,
EdgelessRootService,
PageRootService,
} from '@blocksuite/blocks';
+import type { BlockModel } from '@blocksuite/store';
import bytes from 'bytes';
import type { TemplateResult } from 'lit';
@@ -48,6 +52,37 @@ class CustomEdgelessPageService extends EdgelessRootService {
override loadFonts(): void {
customLoadFonts(this);
}
+
+ override addElement<T = Record<string, unknown>>(type: string, props: T) {
+ const res = super.addElement(type, props);
+ mixpanel.track('WhiteboardObjectCreated', {
+ page: 'whiteboard editor',
+ module: 'whiteboard',
+ segment: 'canvas',
+ // control:
+ type: 'whiteboard object',
+ category: type,
+ });
+ return res;
+ }
+
+ override addBlock(
+ flavour: string,
+ props: Record<string, unknown>,
+ parent?: string | BlockModel,
+ parentIndex?: number
+ ) {
+ const res = super.addBlock(flavour, props, parent, parentIndex);
+ mixpanel.track('WhiteboardObjectCreated', {
+ page: 'whiteboard editor',
+ module: 'whiteboard',
+ segment: 'canvas',
+ // control:
+ type: 'whiteboard object',
+ category: flavour.split(':')[1], // affine:paragraph -> paragraph
+ });
+ return res;
+ }
}
type AffineReference = HTMLElementTagNameMap['affine-reference'];
@@ -85,6 +120,63 @@ function patchSpecsWithReferenceRenderer(
});
}
+function patchSlashMenuWidget() {
+ const menuGroup = AffineSlashMenuWidget.DEFAULT_OPTIONS.menus.find(group => {
+ return group.name === 'Docs';
+ });
+
+ if (Array.isArray(menuGroup?.items)) {
+ const newDocItem = menuGroup.items.find(item => {
+ return item.name === 'New Doc';
+ });
+
+ if (newDocItem) {
+ const oldAction = newDocItem.action;
+ newDocItem.action = async (...props) => {
+ await oldAction(...props);
+ mixpanel.track('DocCreated', {
+ segment: 'doc',
+ module: 'command menu',
+ control: 'new doc command',
+ type: 'doc',
+ category: 'doc',
+ });
+ };
+ }
+ }
+}
+
+function patchLinkedDocPopover() {
+ const oldGetMenus = AffineLinkedDocWidget.DEFAULT_OPTIONS.getMenus;
+
+ AffineLinkedDocWidget.DEFAULT_OPTIONS.getMenus = ctx => {
+ const menus = oldGetMenus(ctx);
+ const newDocGroup = menus.find(group => group.name === 'New Doc');
+ const newDocItem = newDocGroup?.items.find(item => item.key === 'create');
+ // todo: patch import doc/workspace action
+ // const importItem = newDocGroup?.items.find(item => item.key === 'import');
+
+ if (newDocItem) {
+ const oldAction = newDocItem.action;
+ newDocItem.action = async () => {
+ await oldAction();
+ mixpanel.track('DocCreated', {
+ segment: 'doc',
+ module: 'linked doc popover',
+ control: 'new doc command',
+ type: 'doc',
+ category: 'doc',
+ });
+ };
+ }
+
+ return menus;
+ };
+}
+
+patchSlashMenuWidget();
+patchLinkedDocPopover();
+
/**
* Patch the block specs with custom renderers.
*/