Skip to content

Commit

Permalink
chore: add Plausible events to Unleash AI chat
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois committed Oct 21, 2024
1 parent 0f3e7ec commit 128fb86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions frontend/src/component/ai/AIChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { AIChatMessage } from './AIChatMessage';
import { AIChatHeader } from './AIChatHeader';
import { Resizable } from 'component/common/Resizable/Resizable';
import { AIChatDisclaimer } from './AIChatDisclaimer';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';

const AI_ERROR_MESSAGE = {
role: 'assistant',
Expand Down Expand Up @@ -90,6 +91,7 @@ export const AIChat = () => {
const [loading, setLoading] = useState(false);
const { setToastApiError } = useToast();
const { chat, newChat } = useAIApi();
const { trackEvent } = usePlausibleTracker();

const [messages, setMessages] = useState<ChatMessage[]>([]);

Expand Down Expand Up @@ -136,6 +138,12 @@ export const AIChat = () => {
const onSend = async (content: string) => {
if (!content.trim() || loading) return;

trackEvent('unleash-ai-chat', {
props: {
eventType: 'send',
},
});

try {
setLoading(true);
setMessages((currentMessages) => [
Expand Down Expand Up @@ -168,7 +176,17 @@ export const AIChat = () => {
if (!open) {
return (
<StyledAIIconContainer>
<StyledAIIconButton size='large' onClick={() => setOpen(true)}>
<StyledAIIconButton
size='large'
onClick={() => {
trackEvent('unleash-ai-chat', {
props: {
eventType: 'open',
},
});
setOpen(true);
}}
>
<SmartToyIcon />
</StyledAIIconButton>
</StyledAIIconContainer>
Expand All @@ -187,7 +205,14 @@ export const AIChat = () => {
<StyledChat>
<AIChatHeader
onNew={onNewChat}
onClose={() => setOpen(false)}
onClose={() => {
trackEvent('unleash-ai-chat', {
props: {
eventType: 'close',
},
});
setOpen(false);
}}
/>
<StyledChatContent>
<AIChatDisclaimer />
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/usePlausibleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export type CustomEvents =
| 'event-timeline'
| 'onboarding'
| 'personal-dashboard'
| 'order-environments';
| 'order-environments'
| 'unleash-ai-chat';

export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);
Expand Down

0 comments on commit 128fb86

Please sign in to comment.