feat: add PostHog tracking for onboarding CTA interactions#3362
Conversation
OpenAPI ChangesNo changes detected Unexpected changes? Ensure your branch is up-to-date with |
There was a problem hiding this comment.
Pull request overview
Adds PostHog analytics instrumentation to the onboarding flow so onboarding CTA interactions (Next/Finish) emit a standardized cta_clicked event with step context, aligning onboarding tracking with existing CTA tracking patterns elsewhere in frontends/main.
Changes:
- Capture
PostHogEvents.CallToActionClickedon onboarding form submission with{ label, step, location: "onboarding" }, gated byNEXT_PUBLIC_POSTHOG_API_KEY. - Add Jest/RTL test coverage to verify event payloads for both “Next” and “Finish”, and verify no capture when PostHog is not configured.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| frontends/main/src/app-pages/OnboardingPage/OnboardingPage.tsx | Emits cta_clicked on onboarding Next/Finish submissions with step + location metadata. |
| frontends/main/src/app-pages/OnboardingPage/OnboardingPage.test.tsx | Adds tests that mock PostHog and assert capture behavior/payload (and gating when env var is unset). |
| onSubmit: async (values) => { | ||
| const label = activeStep < NUM_STEPS - 1 ? "Next" : "Finish" | ||
| if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { | ||
| posthog.capture(PostHogEvents.CallToActionClicked, { |
There was a problem hiding this comment.
posthog.capture fires before mutateAsync, so if the mutation throws and error the event is already recorded even though the step wasn't actually completed. Quick fix just move this after:
if (formik.dirty) {
await mutateAsync({
...values,
topic_interests: values.topic_interests.map((id) => parseInt(id)),
})
}
| validationSchema: ProfileSchema, | ||
| onSubmit: async (values) => { | ||
| const label = activeStep < NUM_STEPS - 1 ? "Next" : "Finish" | ||
| if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY) { |
There was a problem hiding this comment.
I'm not sure you need this guard here. posthog.capture is safe to call unconditionally.
There was a problem hiding this comment.
I kept it here because Copilot pointed out in my first PR that this PostHog capture isn’t guarded by NEXT_PUBLIC_POSTHOG_API_KEY, while most other captures in frontends/main check that env var before calling posthog.capture.”. I don’t think that guard is necessary.
daniellefrappier18
left a comment
There was a problem hiding this comment.
See my comment about timing but otherwise looks good.
What are the relevant tickets?
https://github.com/mitodl/hq/issues/10916#issuecomment-4499456757
Description (What does it do?)
adds PostHog tracking for onboarding CTA interactions
Screenshots (if appropriate):
How can this be tested?
cta_clicked eventis captured for the page http://open.odl.local:8062/onboardingAdditional Context