-
Notifications
You must be signed in to change notification settings - Fork 5
Posthog webview experiments #2890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mwvolo
wants to merge
18
commits into
main
Choose a base branch
from
posthog-webview-experiments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e7f1e83
only trigger playwright tests on a push to main
mwvolo 57f65f9
feat: add PostHog experiment toolkit (read flags, capture events)
mwvolo ce167f7
feat: generic flag-aware nav resolver + key-based Products A/B + K12 …
mwvolo fc9f11b
Merge branch 'main' into posthog-webview-experiments
mwvolo 62ba65b
fix: add required blank lines to satisfy padding-line-between-stateme…
Copilot c562478
Apply suggestions from code review
mwvolo a5e5375
Merge branch 'main' into posthog-webview-experiments
mwvolo eb81135
Merge branch 'main' into posthog-webview-experiments
mwvolo b291148
Merge branch 'main' into posthog-webview-experiments
mwvolo 153132e
Merge branch 'main' into posthog-webview-experiments
mwvolo bc807e7
Drop k12 nav experiment, measure streamlined_nav, add PostHog convers…
mwvolo bdd1e2f
Fix coverage: test SearchButton click and posthog no-op cases
Copilot bdd3a1e
fix: restore osweb coverage for posthog tracking paths
Copilot 4318476
Merge branch 'main' into posthog-webview-experiments
mwvolo 186f385
form engagements
mwvolo 994af59
fix: add test coverage for afterSubmit callback and toggleToc closing…
Copilot e03cc92
Merge remote-tracking branch 'origin/main' into posthog-webview-exper…
Copilot 1ec723c
Fix FlagName typing for streamlined_nav flag
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import React from 'react'; | ||
|
|
||
| export type FlagValue = string | boolean | undefined; | ||
|
|
||
| type PostHogClient = { | ||
| getFeatureFlag: (key: string) => FlagValue; | ||
| onFeatureFlags: (cb: () => void) => void; | ||
| capture: (event: string, properties?: Record<string, unknown>) => void; | ||
| }; | ||
|
|
||
| /** PostHog is loaded by GTM; it may not be present yet (pre-consent). */ | ||
| function getPostHog(): PostHogClient | undefined { | ||
| return (window as unknown as {posthog?: PostHogClient}).posthog; | ||
| } | ||
|
|
||
| /** Read an experiment/feature-flag variant. Reading it auto-fires the | ||
| * `$feature_flag_called` exposure event in PostHog. */ | ||
| export function getExperimentVariant(flagKey: string): FlagValue { | ||
| return getPostHog()?.getFeatureFlag(flagKey); | ||
| } | ||
|
|
||
| /** Fire a goal/auxiliary event. Safe no-op when PostHog is absent. */ | ||
| export function captureEvent(event: string, properties?: Record<string, unknown>) { | ||
| getPostHog()?.capture(event, properties); | ||
| } | ||
|
|
||
| /** Hook form: returns the variant, re-rendering once PostHog's flags resolve. */ | ||
| export function useExperiment(flagKey: string): FlagValue { | ||
| const [variant, setVariant] = React.useState<FlagValue>(() => | ||
| getExperimentVariant(flagKey) | ||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| const ph = getPostHog(); | ||
|
|
||
| if (!ph) { | ||
| return; | ||
| } | ||
| ph.onFeatureFlags(() => setVariant(ph.getFeatureFlag(flagKey))); | ||
| }, [flagKey]); | ||
|
|
||
| return variant; | ||
| } | ||
|
|
||
| /** Subscribe to PostHog flag resolution and return a synchronous variant | ||
| * reader. Re-renders the caller once flags load, so callers can read any | ||
| * number of flags (e.g. while filtering a list) without breaking hooks rules. */ | ||
| export function useExperimentReader(): (flag: string) => FlagValue { | ||
| const [, forceRender] = React.useReducer((n: number) => n + 1, 0); | ||
|
|
||
| React.useEffect(() => { | ||
| const ph = getPostHog(); | ||
|
|
||
| if (!ph) { | ||
| return; | ||
| } | ||
| ph.onFeatureFlags(() => forceRender()); | ||
| }, []); | ||
|
mwvolo marked this conversation as resolved.
|
||
| return getExperimentVariant; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/app/layouts/default/header/menus/main-menu/nav-experiments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type {FlagValue} from '~/helpers/posthog'; | ||
|
|
||
| export const NAV_PRODUCTS_LABEL_FLAG = 'nav-products-label'; | ||
| export const NAV_K12_ITEM_FLAG = 'nav-k12-item'; | ||
|
|
||
| /** Stable CMS `key` of the dropdown under the Products/Tools A/B. | ||
| * CONFIRM this matches the `key` set on that dropdown in Wagtail. */ | ||
| export const PRODUCTS_DROPDOWN_KEY = 'products-dropdown'; | ||
| const TOOLS_LABEL = 'Tools'; | ||
|
|
||
| /** Flag metadata any CMS menu node may carry (snake_case, matching the | ||
| * oxmenus API — same casing as `partial_url`). */ | ||
| export type FlagAwareNode = { | ||
| key?: string; | ||
| feature_flag?: string; | ||
| flag_value?: string; | ||
| }; | ||
|
|
||
| /** Generic visibility gate from a node's optional flag metadata. | ||
| * - no feature_flag → always visible | ||
| * - flag_value provided → visible iff String(variant) === flag_value | ||
| * - flag_value blank → visible iff the flag is truthy | ||
| * Nodes gated ON by a flag appear once PostHog's flags resolve (fine for | ||
| * additive items); author controls/defaults WITHOUT a feature_flag so they | ||
| * render immediately. */ | ||
| export function isNodeVisible( | ||
| node: FlagAwareNode, | ||
| getVariant: (flag: string) => FlagValue | ||
| ): boolean { | ||
| const flag = node.feature_flag; | ||
|
|
||
| if (!flag) { | ||
| return true; | ||
| } | ||
| const variant = getVariant(flag); | ||
|
|
||
| if (node.flag_value) { | ||
| return String(variant) === node.flag_value; | ||
| } | ||
| return Boolean(variant); | ||
| } | ||
|
|
||
| /** A/B label swap for the Products dropdown, keyed by stable `key` | ||
| * (not the display label, so editors can rename freely). */ | ||
| export function dropdownLabel( | ||
| node: {key?: string; name?: string}, | ||
| productsVariant: FlagValue | ||
| ): string { | ||
| if (node.key === PRODUCTS_DROPDOWN_KEY && productsVariant === 'tools') { | ||
| return TOOLS_LABEL; | ||
| } | ||
| return node.name ?? ''; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import React from 'react'; | ||
| import {render, screen, act} from '@testing-library/preact'; | ||
| import { | ||
| getExperimentVariant, | ||
| captureEvent, | ||
| useExperiment, | ||
| useExperimentReader | ||
| } from '~/helpers/posthog'; | ||
|
|
||
| type FakePostHog = { | ||
| getFeatureFlag: jest.Mock; | ||
| onFeatureFlags: jest.Mock; | ||
| capture: jest.Mock; | ||
| }; | ||
|
|
||
| function installPostHog(overrides: Partial<FakePostHog> = {}) { | ||
| const ph: FakePostHog = { | ||
| getFeatureFlag: jest.fn(), | ||
| onFeatureFlags: jest.fn(), | ||
| capture: jest.fn(), | ||
| ...overrides | ||
| }; | ||
| (window as unknown as {posthog?: FakePostHog}).posthog = ph; | ||
| return ph; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| delete (window as unknown as {posthog?: unknown}).posthog; | ||
| }); | ||
|
|
||
| describe('posthog helper', () => { | ||
| it('getExperimentVariant returns undefined when posthog is absent', () => { | ||
| expect(getExperimentVariant('nav-products-label')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('getExperimentVariant reads the flag', () => { | ||
| installPostHog({getFeatureFlag: jest.fn().mockReturnValue('tools')}); | ||
| expect(getExperimentVariant('nav-products-label')).toBe('tools'); | ||
| }); | ||
|
|
||
| it('captureEvent no-ops without posthog', () => { | ||
| expect(() => captureEvent('thing_clicked')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('captureEvent forwards to posthog', () => { | ||
| const ph = installPostHog(); | ||
| captureEvent('thing_clicked', {a: 1}); | ||
| expect(ph.capture).toHaveBeenCalledWith('thing_clicked', {a: 1}); | ||
| }); | ||
|
|
||
| it('useExperiment returns control then updates when flags resolve', async () => { | ||
|
mwvolo marked this conversation as resolved.
Outdated
|
||
| const ph = installPostHog({ | ||
| getFeatureFlag: jest | ||
| .fn() | ||
| .mockReturnValueOnce(undefined) | ||
| .mockReturnValue('tools') | ||
| }); | ||
|
|
||
| function Probe() { | ||
| const variant = useExperiment('nav-products-label'); | ||
| return <span>{String(variant)}</span>; | ||
| } | ||
|
|
||
| render(<Probe />); | ||
| screen.getByText('undefined'); | ||
|
|
||
| const cb = ph.onFeatureFlags.mock.calls[0][0] as () => void; | ||
| act(() => cb()); | ||
| await screen.findByText('tools'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('useExperimentReader', () => { | ||
| it('returns a reader and re-renders when flags resolve', async () => { | ||
| const ph = { | ||
| getFeatureFlag: jest.fn().mockReturnValueOnce(undefined).mockReturnValue('on'), | ||
| onFeatureFlags: jest.fn(), | ||
| capture: jest.fn() | ||
| }; | ||
| (window as unknown as {posthog?: typeof ph}).posthog = ph; | ||
|
|
||
| function Probe() { | ||
| const getVariant = useExperimentReader(); | ||
| return <span>{String(getVariant('f'))}</span>; | ||
| } | ||
| const {findByText, getByText} = render(<Probe />); | ||
| getByText('undefined'); | ||
| const cb = ph.onFeatureFlags.mock.calls[0][0] as () => void; | ||
| act(() => cb()); | ||
| await findByText('on'); | ||
| delete (window as unknown as {posthog?: unknown}).posthog; | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.