From 430991d1dcdcdb721066309e7c65b6a7d4108946 Mon Sep 17 00:00:00 2001 From: Saul-Mirone Date: Thu, 19 Dec 2024 09:54:21 +0000 Subject: [PATCH] feat: add telemetry event for drop linked doc (#9028) --- .../watchers/drag-event-watcher.ts | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/packages/blocks/src/root-block/widgets/drag-handle/watchers/drag-event-watcher.ts b/packages/blocks/src/root-block/widgets/drag-handle/watchers/drag-event-watcher.ts index 38e9ff2f6c4b..f1cb4fa96641 100644 --- a/packages/blocks/src/root-block/widgets/drag-handle/watchers/drag-event-watcher.ts +++ b/packages/blocks/src/root-block/widgets/drag-handle/watchers/drag-event-watcher.ts @@ -4,7 +4,11 @@ import { EMBED_CARD_HEIGHT, EMBED_CARD_WIDTH, } from '@blocksuite/affine-shared/consts'; -import { DndApiExtensionIdentifier } from '@blocksuite/affine-shared/services'; +import { + DndApiExtensionIdentifier, + DocModeProvider, + TelemetryProvider, +} from '@blocksuite/affine-shared/services'; import { captureEventTarget, getBlockComponentsExcludeSubtrees, @@ -288,7 +292,7 @@ export class DragEventWatcher { const id = first.id; const std = this._std; - const job = this._job; + const job = this._getJob(); const snapshotWithoutNote = { ...snapshot, content: first.children, @@ -329,7 +333,7 @@ export class DragEventWatcher { first.props.height = height; const std = this._std; - const job = this._job; + const job = this._getJob(); job .snapshotToSlice(snapshot, std.doc, edgelessRoot.surfaceBlockModel.id) .catch(console.error); @@ -351,6 +355,10 @@ export class DragEventWatcher { ); if (!newBound) return; + if (first.flavour === 'affine:embed-linked-doc') { + this._trackLinkedDocCreated(first.id); + } + importToSurface(width, height, newBound); return; } @@ -430,16 +438,26 @@ export class DragEventWatcher { this._serializeData(slice, state); }; - private get _dndAPI() { - return this._std.get(DndApiExtensionIdentifier); - } + private _trackLinkedDocCreated = (id: string) => { + const isNewBlock = !this._std.doc.hasBlock(id); + if (!isNewBlock) { + return; + } - private get _job() { - const std = this._std; - return new Job({ - collection: std.collection, - middlewares: [newIdCrossDoc(std), surfaceRefToEmbed(std)], + const mode = + this._std.getOptional(DocModeProvider)?.getEditorMode() ?? 'page'; + + const telemetryService = this._std.getOptional(TelemetryProvider); + telemetryService?.track('LinkedDocCreated', { + control: `drop on ${mode}`, + module: 'drag and drop', + type: 'doc', + other: 'new doc', }); + }; + + private get _dndAPI() { + return this._std.get(DndApiExtensionIdentifier); } private get _std() { @@ -458,10 +476,16 @@ export class DragEventWatcher { if (!dataTransfer) throw new Error('No data transfer'); const std = this._std; - const job = this._job; + const job = this._getJob(); const snapshot = this._deserializeSnapshot(state); if (snapshot) { + if (snapshot.content.length === 1) { + const [first] = snapshot.content; + if (first.flavour === 'affine:embed-linked-doc') { + this._trackLinkedDocCreated(first.id); + } + } // use snapshot const slice = await job.snapshotToSlice( snapshot, @@ -512,11 +536,19 @@ export class DragEventWatcher { } } + private _getJob() { + const std = this._std; + return new Job({ + collection: std.collection, + middlewares: [newIdCrossDoc(std), surfaceRefToEmbed(std)], + }); + } + private _serializeData(slice: Slice, state: DndEventState) { const dataTransfer = state.raw.dataTransfer; if (!dataTransfer) return; - const job = this._job; + const job = this._getJob(); const snapshot = job.sliceToSnapshot(slice); if (!snapshot) return;