Skip to content

feat(surveys): add active matching surveys subscription - #4794

Open
AyobamiH wants to merge 2 commits into
PostHog:mainfrom
AyobamiH:feat/1498-active-matching-surveys-subscription
Open

feat(surveys): add active matching surveys subscription#4794
AyobamiH wants to merge 2 commits into
PostHog:mainfrom
AyobamiH:feat/1498-active-matching-surveys-subscription

Conversation

@AyobamiH

@AyobamiH AyobamiH commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Problem

Closes #1498.

getActiveMatchingSurveys is intentionally a one-shot query. For API surveys targeted by an event or action, the matching set can change after the query returns, but consumers currently have no push-based way to observe that change.

Changes

  • Add posthog.onActiveMatchingSurveysChanged(callback), which returns an unsubscribe function.
  • Emit the initial matching set after surveys load, then re-evaluate after event/action activation, cancellation, consumption, and session-scoped activation expiry.
  • Keep getActiveMatchingSurveys one-shot to avoid changing existing callback semantics.
  • Register cached event/action survey definitions during initialization so subscriptions also work before a background refresh.
  • Suppress duplicate notifications when a repeated trigger does not change activation state.
  • Isolate subscriber errors so one callback cannot interrupt capture processing.
  • Add the public method to posthog-js, @posthog/types, and the production property-preservation list.

Test evidence

  • corepack pnpm --filter posthog-js test:unit: 6,060 passed, 8 skipped.
  • corepack pnpm --filter posthog-js test:functional: 10 passed.
  • corepack pnpm --filter posthog-js typecheck
  • corepack pnpm --filter posthog-js lint
  • corepack pnpm --filter posthog-js format:check
  • corepack pnpm --filter posthog-js build
  • corepack pnpm --filter posthog-js bundle-size:array upstream/main: +1.54 KiB minified, +0.30 KiB gzip, +0.18 KiB Brotli.
  • corepack pnpm --filter @posthog/types test:unit
  • corepack pnpm --filter @posthog/types lint
  • corepack pnpm --filter @posthog/types build
  • corepack pnpm format
  • corepack pnpm lint:oxlint
  • corepack pnpm check:public-api

Release info Sub-libraries affected

Libraries affected

  • All of them
  • posthog-js (web)
  • posthog-js-lite (web lite)
  • posthog-node
  • posthog-react-native
  • @posthog/react-native-plugin
  • @posthog/react
  • @posthog/ai
  • @posthog/convex
  • @posthog/next
  • @posthog/nextjs-config
  • @posthog/nuxt
  • @posthog/openfeature-node-provider
  • @posthog/openfeature-web-provider
  • @posthog/rollup-plugin
  • @posthog/webpack-plugin
  • @posthog/types
  • @posthog/browser-common

Checklist

  • Tests for new code
  • Accounted for the impact of any changes across different platforms
  • Accounted for backwards compatibility of any changes (no breaking changes!)
  • Took care not to unnecessarily increase the bundle size

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

AyobamiH directed the issue selection, implementation, and maintainer-style review. Codex in ChatGPT Work Mode assisted with repository investigation, implementation, and verification.

The separate subscription API follows the maintainer's recommendation and preserves the existing getter's one-shot behavior. During review, the regression was strengthened to use an API survey and the real SurveyManager matching path without manually registering trigger definitions. A second regression preserves initial-notification ordering when an existing onSurveysLoaded callback captures the activating event.

@AyobamiH
AyobamiH requested review from a team as code owners September 4, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API event targeting surveys should call getActiveMatchingSurveys callback when conditions are met

1 participant