Skip to content

feat: implement PostHog event tracking for user interactions#3218

Merged
rachellougee merged 11 commits into
mainfrom
feat/posthog-event-tracking
Apr 16, 2026
Merged

feat: implement PostHog event tracking for user interactions#3218
rachellougee merged 11 commits into
mainfrom
feat/posthog-event-tracking

Conversation

@rachellougee
Copy link
Copy Markdown
Contributor

@rachellougee rachellougee commented Apr 16, 2026

What are the relevant tickets?

https://github.com/mitodl/hq/issues/10916

Description (What does it do?)

Adds PostHog analytics event tracking for user interactions:

  • course_card_clicked — fired when a user clicks a course card in:
    • Homepage featured courses
    • Drawer - Similar Learning Resources
    • Program page course cards
  • cta_clicked — fired when a user clicks a button in:
    • Resource drawer CTA button (e.g. "Access to Course Materials", "Learn More")
    • Course enrollment button on course product page
    • Program enrollment button on program product page
  • asktim_clicked — fired when a user opens the AskTIM AI chat on:
    • Homepage AskTIM drawer button
    • Resource drawer syllabus bot toggle

How can this be tested?

env/frontend.local.env:

NEXT_PUBLIC_MITX_ONLINE_BASE_URL=http://mitxonline.odl.local:8013
NEXT_PUBLIC_MITX_ONLINE_LEGACY_BASE_URL=http://mitxonline.odl.local:8013
FEATURE_FLAGS={"home-page-recommendation-bot": true}

Ensure you have courses and programs product setup on your local mitxonline http://mitxonline.odl.local:8013/api/v2/programs/

env/shared.local.env:

POSTHOG_PROJECT_API_KEY=
POSTHOG_PROJECT_ID=

course_card_clicked

  • Go to homepage featured courses
  • Open any resource drawer -> click on "Similar Learning Resources"
  • Go to a program page (e.g. /programs/program-v1:MITxT+18.01) → click a course card in "Required Courses" or "Elective Courses"
Pasted Graphic 19 Pasted Graphic 20

cta_clicked

  • Open any resource drawer → click the CTA button (e.g. "Learn More")
Pasted Graphic 13 Pasted Graphic 14
  • Go to a course product page → click the enrollment button
  • Go to a program product page → click the enrollment button
image image

asktim_clicked

  • Go to homepage (/?recommender) → click the AskTIM button
  • Open a resource drawer → click the "AskTIM" syllabus bot toggle
Pasted Graphic 18 Pasted Graphic 17

Additional Context

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 16, 2026

OpenAPI Changes

No changes detected

View full changelog

Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

@rachellougee rachellougee force-pushed the feat/posthog-event-tracking branch from 0e5962e to 5f8329e Compare April 16, 2026 13:34
@rachellougee rachellougee marked this pull request as ready for review April 16, 2026 13:58
Copilot AI review requested due to automatic review settings April 16, 2026 13:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds PostHog analytics instrumentation for key user interactions across the Learn UI (resource cards, CTAs, AskTIM entry points, and MITxOnline product pages), supporting HQ issue tracking requirements.

Changes:

  • Introduces new PostHog event names (course_card_clicked, asktim_clicked) and captures them in several UI entry points.
  • Adds CTA click labeling for cta_clicked and updates the corresponding unit test expectation.
  • Enables click handlers to run for LinkAdapter “shallow” navigation (query-param pushState), so analytics can be captured for shallow links.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
frontends/ol-components/src/components/LinkAdapter/LinkAdapter.tsx Ensures onClick runs even for shallow pushState links.
frontends/main/src/page-components/ResourceCarousel/ResourceCarousel.tsx Captures course_card_clicked for carousel card interactions.
frontends/main/src/page-components/LearningResourceExpanded/CallToActionSection.tsx Adds label to cta_clicked payload for drawer CTAs.
frontends/main/src/page-components/LearningResourceExpanded/CallToActionSection.test.tsx Updates test to assert label is included in capture payload.
frontends/main/src/page-components/LearningResourceExpanded/AiChatSyllabusSlideDown.tsx Captures asktim_clicked for syllabus-bot toggling.
frontends/main/src/page-components/AiChat/AskTimDrawerButton.tsx Captures asktim_clicked for opening the homepage recommender drawer via shallow link.
frontends/main/src/common/constants.ts Adds new PostHog event constants.
frontends/main/src/app-pages/ProductPages/ProgramPage.tsx Passes section label into MITxOnline requirement cards for analytics context.
frontends/main/src/app-pages/ProductPages/ProgramEnrollmentButton.tsx Captures cta_clicked for program enrollment button clicks.
frontends/main/src/app-pages/ProductPages/MitxOnlineResourceCard.tsx Captures course_card_clicked for MITxOnline product requirement cards.
frontends/main/src/app-pages/ProductPages/CourseEnrollmentButton.tsx Captures cta_clicked for course enrollment button clicks.

Comment thread frontends/main/src/page-components/ResourceCarousel/ResourceCarousel.tsx Outdated
Comment thread frontends/main/src/page-components/ResourceCarousel/ResourceCarousel.tsx Outdated
Comment thread frontends/main/src/app-pages/ProductPages/MitxOnlineResourceCard.tsx Outdated
Comment thread frontends/main/src/app-pages/ProductPages/MitxOnlineResourceCard.tsx Outdated
Comment on lines +91 to +96
if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
posthog.capture(PostHogEvents.CallToActionClicked, {
program,
label: program.title,
})
}
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProgramEnrollmentButton has a comprehensive test suite, but the new PostHog tracking path isn’t covered. Add a test that sets NEXT_PUBLIC_POSTHOG_API_KEY, mocks usePostHog().capture, clicks the enroll button, and asserts cta_clicked is captured with the expected properties (and that it does not fire when the env var is unset).

Copilot generated this review using guidance from repository custom instructions.
Comment thread frontends/main/src/page-components/ResourceCarousel/ResourceCarousel.tsx Outdated
Comment thread frontends/main/src/page-components/AiChat/AskTimDrawerButton.tsx Outdated
Comment on lines +97 to +102
if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
posthog.capture(PostHogEvents.CallToActionClicked, {
course,
label: course.title,
})
}
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CourseEnrollmentButton has an existing test suite, but the new PostHog tracking path isn’t covered. Add a test that sets NEXT_PUBLIC_POSTHOG_API_KEY, mocks usePostHog().capture, triggers a click in an authenticated scenario, and asserts cta_clicked is captured with the expected payload.

Copilot generated this review using guidance from repository custom instructions.
@rachellougee rachellougee added the Needs Review An open Pull Request that is ready for review label Apr 16, 2026
@gumaerc gumaerc self-assigned this Apr 16, 2026
Copy link
Copy Markdown
Contributor

@gumaerc gumaerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this and see tracking events flowing through, but there are a few things we should address:

Firstly, there is a bit of missing test coverage I think we should add, and I had AI summarize it:

  1. Home page UAI card click tracking

    • File: frontends/main/src/app-pages/HomePage/HomePage.test.tsx
    • Add one test in the existing UAI Announcement Card describe block:
      • Enable UniversalAI flag
      • Click Learn about Universal AI link
      • Assert mocked capture was called with cta_clicked and payload including label plus readableId
  2. Search page UAI banner click tracking

    • File: frontends/main/src/app-pages/SearchPage/SearchPage.test.tsx
    • Add one test in the existing UniversalAIBanner describe block:
      • Enable UniversalAISearchBanner flag with matching search term
      • Click Learn More link
      • Assert mocked capture was called with cta_clicked and payload including label plus readableId
  3. AskTIM recommendation button click tracking

  4. AskTIM syllabus button click tracking

  5. Carousel card click tracking

  6. Product page requirement card tracking

  7. Enrollment CTA tracking branch behavior

@@ -91,6 +94,14 @@ const CourseEnrollmentButton: React.FC<CourseEnrollmentButtonProps> = ({
if (me.isLoading) {
return
} else if (me.data?.is_authenticated) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this checks if the user is authenticated before firing the tracking event. Unauthenticated clicks should be tracked as well.

const handleClick: React.MouseEventHandler<HTMLButtonElement> = (e) => {
if (enrollments.isLoading || me.isLoading) {
return
} else if (me.data?.is_authenticated) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, we need to also track unauthenticated clicks.

@rachellougee rachellougee force-pushed the feat/posthog-event-tracking branch from 5ccb9f3 to f587709 Compare April 16, 2026 20:20
rachellougee and others added 2 commits April 16, 2026 16:34
…ssertion

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…e.error

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rachellougee rachellougee requested a review from gumaerc April 16, 2026 20:59
Copy link
Copy Markdown
Contributor

@gumaerc gumaerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM!

@rachellougee rachellougee merged commit 1e8bf8d into main Apr 16, 2026
14 checks passed
@rachellougee rachellougee deleted the feat/posthog-event-tracking branch April 16, 2026 21:20
@odlbot odlbot mentioned this pull request Apr 17, 2026
10 tasks
@odlbot odlbot mentioned this pull request Apr 20, 2026
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review An open Pull Request that is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants