From bd523c264402f5a4ddc4a310c0a244e6a9af03c2 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Wed, 18 Dec 2024 18:31:44 +0800 Subject: [PATCH] chore(blocks): add event tracking for attachment upload --- .../telemetry-service/telemetry-service.ts | 2 ++ .../src/services/telemetry-service/types.ts | 9 +++++++++ packages/blocks/src/attachment-block/utils.ts | 18 ++++++++++++++++-- .../src/root-block/edgeless/utils/common.ts | 8 ++++++-- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/affine/shared/src/services/telemetry-service/telemetry-service.ts b/packages/affine/shared/src/services/telemetry-service/telemetry-service.ts index b5d23876d44f..fd1d4d0122de 100644 --- a/packages/affine/shared/src/services/telemetry-service/telemetry-service.ts +++ b/packages/affine/shared/src/services/telemetry-service/telemetry-service.ts @@ -3,6 +3,7 @@ import { createIdentifier } from '@blocksuite/global/di'; import type { OutDatabaseAllEvents } from './database.js'; import type { LinkToolbarEvents } from './link.js'; import type { + AttachmentUploadedEvent, DocCreatedEvent, ElementCreationEvent, ElementLockEvent, @@ -19,6 +20,7 @@ export type TelemetryEventMap = OutDatabaseAllEvents & CanvasElementAdded: ElementCreationEvent; EdgelessElementLocked: ElementLockEvent; ExpandedAndCollapsed: MindMapCollapseEvent; + AttachmentUploadedEvent: AttachmentUploadedEvent; }; export interface TelemetryService { diff --git a/packages/affine/shared/src/services/telemetry-service/types.ts b/packages/affine/shared/src/services/telemetry-service/types.ts index 1dfcb02aaef8..553dfa351cef 100644 --- a/packages/affine/shared/src/services/telemetry-service/types.ts +++ b/packages/affine/shared/src/services/telemetry-service/types.ts @@ -52,3 +52,12 @@ export interface MindMapCollapseEvent extends TelemetryEvent { segment: 'mind map'; type: 'expand' | 'collapse'; } + +export interface AttachmentUploadedEvent extends TelemetryEvent { + page: 'doc editor' | 'whiteboard editor'; + segment: 'attachment'; + module: 'attachment'; + control: 'uploader'; + type: string; // file type + category: 'success' | 'failure'; +} diff --git a/packages/blocks/src/attachment-block/utils.ts b/packages/blocks/src/attachment-block/utils.ts index c1ab58bd236f..deae7e18c297 100644 --- a/packages/blocks/src/attachment-block/utils.ts +++ b/packages/blocks/src/attachment-block/utils.ts @@ -7,6 +7,7 @@ import type { BlockModel } from '@blocksuite/store'; import { toast } from '@blocksuite/affine-components/toast'; import { defaultAttachmentProps } from '@blocksuite/affine-model'; +import { TelemetryProvider } from '@blocksuite/affine-shared/services'; import { humanFileSize } from '@blocksuite/affine-shared/utils'; import type { AttachmentBlockComponent } from './attachment-block.js'; @@ -40,7 +41,9 @@ function isAttachmentUploading(blockId: string) { export async function uploadAttachmentBlob( editorHost: EditorHost, blockId: string, - blob: Blob + blob: Blob, + filetype: string, + isEdgeless?: boolean ): Promise { if (isAttachmentUploading(blockId)) { return; @@ -72,6 +75,17 @@ export async function uploadAttachmentBlob( sourceId, } satisfies Partial); }); + + editorHost.std + .getOptional(TelemetryProvider) + ?.track('AttachmentUploadedEvent', { + page: `${isEdgeless ? 'whiteboard' : 'doc'} editor`, + module: 'attachment', + segment: 'attachment', + control: 'uploader', + type: filetype, + category: block && sourceId ? 'success' : 'failure', + }); } } @@ -233,7 +247,7 @@ export async function addSiblingAttachmentBlocks( blockIds.map( (blockId, index) => - void uploadAttachmentBlob(editorHost, blockId, files[index]) + void uploadAttachmentBlob(editorHost, blockId, files[index], types[index]) ); return blockIds; diff --git a/packages/blocks/src/root-block/edgeless/utils/common.ts b/packages/blocks/src/root-block/edgeless/utils/common.ts index 2516949a910a..234b6d5ed559 100644 --- a/packages/blocks/src/root-block/edgeless/utils/common.ts +++ b/packages/blocks/src/root-block/edgeless/utils/common.ts @@ -31,7 +31,10 @@ import { Vec, } from '@blocksuite/global/utils'; -import { uploadAttachmentBlob } from '../../../attachment-block/utils.js'; +import { + getFileType, + uploadAttachmentBlob, +} from '../../../attachment-block/utils.js'; import { calcBoundByOrigin, readImageSize } from '../components/utils.js'; import { DEFAULT_NOTE_OFFSET_X, DEFAULT_NOTE_OFFSET_Y } from './consts.js'; import { addBlock } from './crud.js'; @@ -99,7 +102,8 @@ export async function addAttachments( // upload file and update the attachment model const uploadPromises = dropInfos.map(async ({ blockId, file }) => { - await uploadAttachmentBlob(std.host, blockId, file); + const filetype = await getFileType(file); + await uploadAttachmentBlob(std.host, blockId, file, filetype, true); return blockId; }); const blockIds = await Promise.all(uploadPromises);