Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useTranslation } from 'react-i18next';
import { isAPIError } from '@/api';
import { Box, Icon } from '@/components';
import { useDocOptions, useDocStore } from '@/docs/doc-management/';
import { useAnalytics } from '@/libs';

import {
AITransformActions,
Expand Down Expand Up @@ -217,22 +218,44 @@ const AIMenuItemTransform = ({
children,
icon,
}: PropsWithChildren<AIMenuItemTransform>) => {
const { trackEvent } = useAnalytics();
const { mutateAsync: requestAI, isPending } = useDocAITransform();
const editor = useBlockNoteEditor();

const requestAIAction = async (selectedBlocks: Block[]) => {
const text = await editor.blocksToMarkdownLossy(selectedBlocks);

const requestStartTime = performance.now();
const responseAI = await requestAI({
text,
action,
docId,
});
const requestDuration = performance.now() - requestStartTime;

const eventProperties = {
eventName: 'requestAIAction',
action: action,
docId: docId,
requestLength: text.length,
numberBlocks: selectedBlocks.length,
requestDuration: requestDuration,
};

if (!responseAI?.answer) {
trackEvent({
...eventProperties,
status: 'error',
});
throw new Error('No response from AI');
}

trackEvent({
...eventProperties,
status: 'success',
responseLength: String(responseAI.answer),
});

const markdown = await editor.tryParseMarkdownToBlocks(responseAI.answer);
editor.replaceBlocks(selectedBlocks, markdown);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { useCreateDoc } from '@/docs/doc-management';
import { DocSearchModal } from '@/docs/doc-search';
import { useAuth } from '@/features/auth';
import { useCmdK } from '@/hook/useCmdK';
import { useAnalytics } from '@/libs';

import { useLeftPanelStore } from '../stores';

export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
const router = useRouter();
const { authenticated } = useAuth();
const { trackEvent } = useAnalytics();
const [isSearchModalOpen, setIsSearchModalOpen] = useState(false);

const openSearchModal = useCallback(() => {
Expand All @@ -23,8 +25,10 @@ export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
return;
}

trackEvent({ eventName: 'openSearchModal', position: 'LeftPanelHeader' });

setIsSearchModalOpen(true);
}, []);
}, [trackEvent]);

const closeSearchModal = useCallback(() => {
setIsSearchModalOpen(false);
Expand All @@ -46,6 +50,7 @@ export const LeftPanelHeader = ({ children }: PropsWithChildren) => {
};

const createNewDoc = () => {
trackEvent({ eventName: 'createNewDoc', position: 'LeftPanelHeader' });
createDoc();
};

Expand Down
10 changes: 9 additions & 1 deletion src/frontend/apps/impress/src/libs/Analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ type AnalyticEventUser = {
email: string;
};

export type AnalyticEvent = AnalyticEventClick | AnalyticEventUser;
export type AnalyticEventGeneric = {
eventName: string;
[key: string]: string | number;
};

export type AnalyticEvent =
| AnalyticEventClick
| AnalyticEventUser
| AnalyticEventGeneric;

export abstract class AbstractAnalytic {
public constructor() {
Expand Down
12 changes: 9 additions & 3 deletions src/frontend/apps/impress/src/services/PosthogAnalytic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import posthog from 'posthog-js';
import { PostHogProvider as PHProvider } from 'posthog-js/react';
import { JSX, PropsWithChildren, ReactNode, useEffect } from 'react';

import { AbstractAnalytic, AnalyticEvent } from '@/libs/';
import { AbstractAnalytic, AnalyticEvent, AnalyticEventGeneric } from '@/libs/';

export class PostHogAnalytic extends AbstractAnalytic {
private conf?: PostHogConf = undefined;
Expand All @@ -19,8 +19,14 @@ export class PostHogAnalytic extends AbstractAnalytic {
}

public trackEvent(evt: AnalyticEvent): void {
if (evt.eventName === 'user') {
posthog.identify(evt.id, { email: evt.email });
switch (evt.eventName) {
case 'user':
posthog.identify(evt.id as string, { email: evt.email });
break;
default:
const { eventName, ...properties } = evt as AnalyticEventGeneric;
posthog.capture(eventName, properties);
break;
}
}

Expand Down
Loading