Skip to content

Commit 524e1cc

Browse files
sign in banner events
1 parent 1952c20 commit 524e1cc

4 files changed

Lines changed: 44 additions & 14 deletions

File tree

packages/web/src/features/chat/actions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { LanguageModelInfo, SBChatMessage } from "./types";
3131
import { withAuthV2, withOptionalAuthV2 } from "@/withAuthV2";
3232
import { getAnonymousId, getOrCreateAnonymousId } from "@/lib/anonymousId";
3333
import { Chat, PrismaClient, User } from "@sourcebot/db";
34+
import { captureEvent } from "@/lib/posthog";
3435

3536
const logger = createLogger('chat-actions');
3637
const auditService = getAuditService();
@@ -387,6 +388,12 @@ export const claimAnonymousChats = async () => sew(() =>
387388
},
388389
});
389390

391+
if (result.count > 0) {
392+
captureEvent('wa_anonymous_chats_claimed', {
393+
claimedCount: result.count,
394+
});
395+
}
396+
390397
return { claimed: result.count };
391398
})
392399
);

packages/web/src/features/chat/components/chatThread/chatThread.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export const ChatThread = ({
392392
</ScrollArea>
393393
<div className="w-full max-w-3xl mx-auto mb-8">
394394
<SignInPromptBanner
395+
chatId={chatId}
395396
isAuthenticated={isAuthenticated}
396397
isOwner={isOwner}
397398
hasMessages={messages.length > 0}

packages/web/src/features/chat/components/chatThread/signInPromptBanner.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22

33
import { Button } from '@/components/ui/button';
4+
import { captureEvent } from '@/hooks/useCaptureEvent';
45
import { X } from 'lucide-react';
56
import Link from 'next/link';
67
import { usePathname } from 'next/navigation';
@@ -9,48 +10,57 @@ import { useState, useEffect } from 'react';
910
const DISMISSED_KEY = 'sb.chat-sign-in-prompt-dismissed';
1011

1112
interface SignInPromptBannerProps {
13+
chatId: string;
1214
isAuthenticated: boolean;
1315
isOwner: boolean;
1416
hasMessages: boolean;
1517
isStreaming: boolean;
1618
}
1719

1820
export const SignInPromptBanner = ({
21+
chatId,
1922
isAuthenticated,
2023
isOwner,
2124
hasMessages,
2225
isStreaming,
2326
}: SignInPromptBannerProps) => {
2427
const pathname = usePathname();
2528
const [isDismissed, setIsDismissed] = useState(true); // Start as true to avoid flash
26-
const [hasShownOnce, setHasShownOnce] = useState(false);
29+
const [hasDisplayedEventFired, setHasDisplayedEventFired] = useState(false);
2730

2831
// Check sessionStorage on mount
2932
useEffect(() => {
3033
const dismissed = sessionStorage.getItem(DISMISSED_KEY) === 'true';
3134
setIsDismissed(dismissed);
3235
}, []);
3336

34-
// Show the banner after first response completes
37+
const isBannerVisible =
38+
!isDismissed &&
39+
!isAuthenticated &&
40+
isOwner &&
41+
hasMessages &&
42+
!isStreaming;
43+
44+
// Show the banner after first response completes and track display
3545
useEffect(() => {
36-
if (!isAuthenticated && isOwner && hasMessages && !isStreaming && !hasShownOnce) {
37-
setHasShownOnce(true);
46+
if (isBannerVisible && !hasDisplayedEventFired) {
47+
setHasDisplayedEventFired(true);
48+
captureEvent('wa_chat_sign_in_banner_displayed', { chatId });
3849
}
39-
}, [isAuthenticated, isOwner, hasMessages, isStreaming, hasShownOnce]);
50+
}, [isBannerVisible, chatId, hasDisplayedEventFired]);
51+
4052

4153
const handleDismiss = () => {
54+
captureEvent('wa_chat_sign_in_banner_dismissed', { chatId });
4255
setIsDismissed(true);
4356
sessionStorage.setItem(DISMISSED_KEY, 'true');
4457
};
4558

46-
// Don't show if:
47-
// - User is authenticated
48-
// - User doesn't own this chat
49-
// - Banner was dismissed
50-
// - No messages yet (haven't had first interaction)
51-
// - Still streaming (wait for response to complete)
52-
// - Haven't triggered the "show" condition yet
53-
if (isAuthenticated || !isOwner || isDismissed || !hasMessages || isStreaming || !hasShownOnce) {
59+
const handleSignInClick = () => {
60+
captureEvent('wa_chat_sign_in_banner_clicked', { chatId });
61+
};
62+
63+
if (!isBannerVisible) {
5464
return null;
5565
}
5666

@@ -64,6 +74,7 @@ export const SignInPromptBanner = ({
6474
variant="default"
6575
size="sm"
6676
asChild
77+
onClick={handleSignInClick}
6778
>
6879
<Link href={`/login?callbackUrl=${encodeURIComponent(pathname)}`}>
6980
Sign in

packages/web/src/lib/posthogEvents.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ export type PosthogEventMap = {
158158
toolName: string,
159159
success: boolean,
160160
},
161-
// Chat Sharing Funnel
162161
wa_chat_share_dialog_opened: {
163162
chatId: string,
164163
currentVisibility: 'PUBLIC' | 'PRIVATE',
@@ -185,6 +184,18 @@ export type PosthogEventMap = {
185184
viewerType: 'authenticated' | 'anonymous',
186185
accessType: 'public_link' | 'direct_invite',
187186
},
187+
wa_chat_sign_in_banner_displayed: {
188+
chatId: string,
189+
},
190+
wa_chat_sign_in_banner_dismissed: {
191+
chatId: string,
192+
},
193+
wa_chat_sign_in_banner_clicked: {
194+
chatId: string,
195+
},
196+
wa_anonymous_chats_claimed: {
197+
claimedCount: number,
198+
},
188199
//////////////////////////////////////////////////////////////////
189200
wa_demo_docs_link_pressed: {},
190201
wa_demo_search_example_card_pressed: {

0 commit comments

Comments
 (0)