From dd45c80cc44b965513a980fa5a5dbad2eb9cac8f Mon Sep 17 00:00:00 2001 From: pengx17 Date: Tue, 14 May 2024 05:35:08 +0000 Subject: [PATCH] chore: track doc create action in bs editor (#6915) fix TOV-855 added shape element create & doc create event tracking in blocksuite editor. What's still missing: the control (source) that triggered whiteboard element creation, i.e., from canvas dbclick, dnd or pasting. --- .../blocksuite/block-suite-editor/specs.ts | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) 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>(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, + 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. */