Skip to content

Commit

Permalink
feat: added posthog analytics for StarSearch for Workspaces (open-sau…
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline authored Jul 18, 2024
1 parent ae5e989 commit 72d7c23
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion components/StarSearch/StarSearchChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type StarSearchChatProps = {
onClose?: () => void;
baseApiStarSearchUrl?: URL;
sharingEnabled?: boolean;
isWorkspace?: boolean;
};

export function StarSearchChat({
Expand All @@ -91,14 +92,15 @@ export function StarSearchChat({
baseApiStarSearchUrl = DEFAULT_STAR_SEARCH_API_BASE_URL,
sharingEnabled = true,
showTopNavigation = false,
isWorkspace = false,
}: StarSearchChatProps) {
const [starSearchState, setStarSearchState] = useState<"initial" | "chat">("initial");
const [chat, setChat] = useState<StarSearchChatMessage[]>([]);
const inputRef = useRef<HTMLInputElement>(null);
const [isRunning, setIsRunning] = useState(false);
const [showSuggestions, setShowSuggestions] = useState(false);
const [ranOnce, setRanOnce] = useState(false);
const { feedback, prompt } = useStarSearchFeedback();
const { feedback, prompt } = useStarSearchFeedback(isWorkspace);
const { toast } = useToast();
const [loginModalOpen, setLoginModalOpen] = useState(false);
const [checkAuth, setCheckAuth] = useState(false);
Expand Down
15 changes: 15 additions & 0 deletions components/StarSearch/StarSearchEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ComponentProps, useRef, useState } from "react";
import { captureException } from "@sentry/nextjs";
import clsx from "clsx";
import { useOutsideClick } from "rooks";
import { usePostHog } from "posthog-js/react";
import { UuidSchema, parseSchema } from "lib/validation-schemas";
import { useToast } from "lib/hooks/useToast";
import { StarSearchButton } from "./StarSearchButton";
Expand All @@ -12,6 +13,10 @@ interface StarSearchEmbedProps extends Omit<ComponentProps<typeof StarSearchChat
signInHandler: () => void;
}

interface StarSearchCtaClickType {
type: "sign_in" | "can_access" | "no_access";
}

export const StarSearchEmbed = ({
userId,
sharedChatId,
Expand All @@ -23,6 +28,7 @@ export const StarSearchEmbed = ({
isEditor = false,
signInHandler,
}: StarSearchEmbedProps) => {
const posthog = usePostHog();
const [drawerOpen, setDrawerOpen] = useState(false);
const onClose = () => setDrawerOpen(false);
const { toast } = useToast();
Expand Down Expand Up @@ -70,12 +76,20 @@ export const StarSearchEmbed = ({
<StarSearchButton
onOpen={() => {
if (!userId) {
posthog.capture("star_search_workspace_cta_click", { type: "sign_in" } satisfies StarSearchCtaClickType);
signInHandler();
return;
}

if (isStarSearchEnabled) {
posthog.capture("star_search_workspace_cta_click", {
type: "can_access",
} satisfies StarSearchCtaClickType);
setDrawerOpen(true);
return;
}

posthog.capture("star_search_workspace_cta_click", { type: "no_access" } satisfies StarSearchCtaClickType);
}}
tooltipText={tooltipText}
enabled={!userId || (isStarSearchEnabled && !!userId)}
Expand All @@ -97,6 +111,7 @@ export const StarSearchEmbed = ({
}}
>
<StarSearchChat
isWorkspace={!!validWorkspaceId}
userId={userId}
sharedChatId={sharedChatId}
bearerToken={bearerToken}
Expand Down
6 changes: 3 additions & 3 deletions lib/hooks/useStarSearchFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ export interface StarSearchPromptAnalytic {
promptResponse: string;
}

export const useStarSearchFeedback = () => {
export const useStarSearchFeedback = (isWorkspace: boolean) => {
const posthog = usePostHog();

const prompt = ({ promptContent, promptResponse }: StarSearchPromptAnalytic) => {
posthog.capture("star_search_prompt", {
posthog.capture(isWorkspace ? "star_search_workspace_prompt" : "star_search_prompt", {
promptContent,
promptResponse,
} satisfies StarSearchPromptAnalytic);
};

const feedback = ({ feedback, promptContent, promptResponse }: StarSearchFeedbackAnalytic) => {
posthog.capture("star_search_feedback", {
posthog.capture(isWorkspace ? "star_search_workspace_feedback" : "star_search_feedback", {
feedback,
promptContent,
promptResponse,
Expand Down

0 comments on commit 72d7c23

Please sign in to comment.