Skip to content

Commit

Permalink
chore(blocks): add event tracking for attachment upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 18, 2024
1 parent 9f443bc commit dea2890
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +18,7 @@ export type TelemetryEventMap = OutDatabaseAllEvents &
SplitNote: TelemetryEvent;
CanvasElementAdded: ElementCreationEvent;
EdgelessElementLocked: ElementLockEvent;
AttachmentUploadedEvent: AttachmentUploadedEvent;
};

export interface TelemetryService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ export interface ElementLockEvent extends TelemetryEvent {
module: 'element toolbar';
control: 'lock' | 'unlock' | 'group-lock';
}

export interface AttachmentUploadedEvent extends TelemetryEvent {
page: 'doc editor' | 'whiteboard editor';
segment: 'attachment';
module: 'attachment uploader';
control: 'user';
category: string; // file mime
type: 'success' | 'failure';
}
18 changes: 16 additions & 2 deletions packages/blocks/src/attachment-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<void> {
if (isAttachmentUploading(blockId)) {
return;
Expand Down Expand Up @@ -72,6 +75,17 @@ export async function uploadAttachmentBlob(
sourceId,
} satisfies Partial<AttachmentBlockProps>);
});

editorHost.std
.getOptional(TelemetryProvider)
?.track('AttachmentUploadedEvent', {
control: 'user',
page: `${isEdgeless ? 'whiteboard' : 'doc'} editor`,
module: 'attachment uploader',
segment: 'attachment',
category: filetype,
type: block && sourceId ? 'success' : 'failure',
});
}
}

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions packages/blocks/src/root-block/edgeless/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dea2890

Please sign in to comment.