From 129b76345c5903cad47e636cc8ffbd894836075c Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Wed, 29 Jan 2025 18:42:39 -0500 Subject: [PATCH 1/7] fire a posthog event when clicking the "Topic" link on the homepage in "Browse by Topic" --- .../src/page-components/HeroSearch/HeroSearch.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx b/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx index f3c7fdd822..72e9e14656 100644 --- a/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx +++ b/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx @@ -29,6 +29,7 @@ import { } from "@remixicon/react" import Image from "next/image" import { SearchField } from "@/page-components/SearchField/SearchField" +import { usePostHog } from "posthog-js/react" type SearchChip = { label: string @@ -193,6 +194,12 @@ const BoldLink = styled(Link)(({ theme }) => ({ })) const HeroSearch: React.FC<{ imageIndex: number }> = ({ imageIndex }) => { + const posthog = usePostHog() + const posthogCapture = (event: string) => { + if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { + posthog.capture(event) + } + } const [searchText, setSearchText] = useState("") const onSearchClear = useCallback(() => setSearchText(""), []) const router = useRouter() @@ -240,7 +247,13 @@ const HeroSearch: React.FC<{ imageIndex: number }> = ({ imageIndex }) => { or browse by{" "} - + { + posthogCapture("clicked_hero_browse_topics") + }} + color="red" + > Topic From 0fc015c3a4cb970a823278073ae8314d1cc90659 Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Wed, 5 Feb 2025 12:24:50 -0500 Subject: [PATCH 2/7] add events for homepage browse topics section --- .../src/app-pages/HomePage/BrowseTopicsSection.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx b/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx index ed4af8a188..e922a5676d 100644 --- a/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx +++ b/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx @@ -11,6 +11,7 @@ import { ButtonLink } from "@mitodl/smoot-design" import { useLearningResourceTopics } from "api/hooks/learningResources" import { RiArrowRightLine } from "@remixicon/react" import RootTopicIcon from "@/components/RootTopicIcon/RootTopicIcon" +import { usePostHog } from "posthog-js/react" const Section = styled.section` background: #fff url("/images/backgrounds/open-bg-texture-with-gradient.svg") @@ -104,6 +105,7 @@ const SeeAllButton = styled(ButtonLink)` ` const BrowseTopicsSection: React.FC = () => { + const posthog = usePostHog() const { data: topics } = useLearningResourceTopics({ is_toplevel: true }) return ( @@ -119,6 +121,9 @@ const BrowseTopicsSection: React.FC = () => { { + posthog.capture("home_topic_clicked", { topic: name }) + }} > @@ -130,7 +135,14 @@ const BrowseTopicsSection: React.FC = () => { }, )} - + { + posthog.capture("home_see_all_topics_clicked") + }} + size="large" + responsive + > See all From ca5c317ccbf3cd7a9c929b1d61033b7f1d56785c Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Wed, 5 Feb 2025 12:56:05 -0500 Subject: [PATCH 3/7] wrap new events in posthog api key check --- .../main/src/app-pages/HomePage/BrowseTopicsSection.tsx | 8 ++++++-- .../main/src/page-components/HeroSearch/HeroSearch.tsx | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx b/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx index e922a5676d..6ec844289d 100644 --- a/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx +++ b/frontends/main/src/app-pages/HomePage/BrowseTopicsSection.tsx @@ -122,7 +122,9 @@ const BrowseTopicsSection: React.FC = () => { key={id} href={channelUrl ? new URL(channelUrl!).pathname : ""} onClick={() => { - posthog.capture("home_topic_clicked", { topic: name }) + if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { + posthog.capture("home_topic_clicked", { topic: name }) + } }} > @@ -138,7 +140,9 @@ const BrowseTopicsSection: React.FC = () => { { - posthog.capture("home_see_all_topics_clicked") + if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { + posthog.capture("home_see_all_topics_clicked") + } }} size="large" responsive diff --git a/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx b/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx index 72e9e14656..e1b6a293b0 100644 --- a/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx +++ b/frontends/main/src/page-components/HeroSearch/HeroSearch.tsx @@ -250,7 +250,9 @@ const HeroSearch: React.FC<{ imageIndex: number }> = ({ imageIndex }) => { { - posthogCapture("clicked_hero_browse_topics") + if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { + posthogCapture("clicked_hero_browse_topics") + } }} color="red" > From 501cef0a008d947ab09d9de0ad11444b8f6df26b Mon Sep 17 00:00:00 2001 From: Carey Gumaer Date: Wed, 5 Feb 2025 13:07:57 -0500 Subject: [PATCH 4/7] track topic_clicked vs subtopic_clicked on topics listing page --- .../TopicsListingPage/TopicsListingPage.tsx | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/frontends/main/src/app-pages/TopicsListingPage/TopicsListingPage.tsx b/frontends/main/src/app-pages/TopicsListingPage/TopicsListingPage.tsx index b52b3dd090..4b3d93745f 100644 --- a/frontends/main/src/app-pages/TopicsListingPage/TopicsListingPage.tsx +++ b/frontends/main/src/app-pages/TopicsListingPage/TopicsListingPage.tsx @@ -23,6 +23,17 @@ import { aggregateProgramCounts, aggregateCourseCounts } from "@/common/utils" import { useChannelCounts } from "api/hooks/channels" import backgroundSteps from "@/public/images/backgrounds/background_steps.jpg" import { usePostHog } from "posthog-js/react" +import type { PostHog } from "posthog-js" + +const captureTopicClicked = ( + posthog: PostHog, + event: string, + topic: string, +) => { + if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { + posthog.capture(event, { topic }) + } +} type ChannelSummary = { id: number | string @@ -37,12 +48,20 @@ type TopicBoxHeaderProps = { icon?: string href?: string className?: string + posthog?: PostHog } const TopicBoxHeader = styled( - ({ title, icon, href, className }: TopicBoxHeaderProps) => { + ({ title, icon, href, className, posthog }: TopicBoxHeaderProps) => { return ( - + { + if (posthog) { + captureTopicClicked(posthog, "topic_clicked", title) + } + }} + >