Skip to content

fix: refactor canRenderSurvey and move logic to SurveyManager #1897

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

Merged
merged 24 commits into from
Apr 24, 2025

Conversation

lucasheriques
Copy link
Contributor

@lucasheriques lucasheriques commented Apr 11, 2025

Changes

both getActiveMatchingSurveys and canRenderSurvey share a lot of similarities around business logic. Since their goal is to get check if a survey is eligible for an user or not.

However, we had duplicated business logic happening on both.

So, I decided to remove canRenderSurvey from posthog-surveys.ts to extenstions/surveys.tsx, to centralize the logic in one place and also reduce the main bundle size, by only downloading it if there are surveys enabled.

I also did the same to getActiveMatchingSurveys. No need to have this logic be on posthog-surveys.ts since it's only relevant if surveys are actually loaded.

Checklist

  • Tests for new code (see advice on the tests we use)
  • Accounted for the impact of any changes across different browsers
  • Accounted for backwards compatibility of any changes (no breaking changes in posthog-js!)

Copy link

vercel bot commented Apr 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Apr 24, 2025 1:07pm

Copy link

github-actions bot commented Apr 11, 2025

Size Change: -4.05 kB (-0.11%)

Total Size: 3.68 MB

Filename Size Change
dist/all-external-dependencies.js 219 kB +2.94 kB (+1.36%)
dist/array.full.es5.js 307 kB -2 B (0%)
dist/array.full.js 386 kB -88 B (-0.02%)
dist/array.full.no-external.js 385 kB -88 B (-0.02%)
dist/array.js 196 kB -1.92 kB (-0.97%)
dist/array.no-external.js 195 kB -1.92 kB (-0.97%)
dist/main.js 197 kB -1.92 kB (-0.96%)
dist/module.full.js 386 kB -88 B (-0.02%)
dist/module.full.no-external.js 385 kB -88 B (-0.02%)
dist/module.js 196 kB -1.92 kB (-0.97%)
dist/module.no-external.js 195 kB -1.92 kB (-0.97%)
dist/surveys-preview.js 64.7 kB -2 B (0%)
dist/surveys.js 74.5 kB +2.94 kB (+4.11%)
ℹ️ View Unchanged
Filename Size
dist/customizations.full.js 11.6 kB
dist/dead-clicks-autocapture.js 14.4 kB
dist/exception-autocapture.js 10.4 kB
dist/external-scripts-loader.js 2.75 kB
dist/posthog-recorder.js 211 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

Copy link

github-actions bot commented Apr 14, 2025

@@ -552,74 +552,6 @@ describe('SurveyManager', () => {
})
})

describe('canRenderSurvey', () => {
Copy link
Contributor Author

@lucasheriques lucasheriques Apr 14, 2025

Choose a reason for hiding this comment

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

tests removed as I moved that method to posthog-surveys.test.ts to stop duplicating rendering logic

@lucasheriques lucasheriques self-assigned this Apr 14, 2025
@lucasheriques lucasheriques marked this pull request as ready for review April 14, 2025 23:22
@lucasheriques lucasheriques requested a review from a team as a code owner April 14, 2025 23:22
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR refactors the survey eligibility logic in posthog-js by centralizing it in the posthog-surveys.ts file, eliminating duplication between getActiveMatchingSurveys and canRenderSurvey functions.

  • Added new checkSurveyEligibility method in PostHogSurveys class that serves as a single source of truth for determining survey eligibility
  • Moved survey condition matching logic from extensions/surveys.tsx to posthog-surveys.ts with helper methods like _isSurveyConditionMatched and _internalFlagCheckSatisfied
  • Added utility functions in survey-utils.ts including doesSurveyMatchSelector, isSurveyRunning, doesSurveyActivateByEvent, and doesSurveyActivateByAction
  • Enhanced logging in survey-event-receiver.ts for better debugging of survey activation events
  • Added comprehensive tests in posthog-surveys.test.ts to verify the refactored eligibility logic works correctly

9 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@marandaneto
Copy link
Member

So, I decided to remove canRenderSurvey from the extensions/survey.tsx file, and move it to posthog-surveys.ts directly. While this slightly increases the bundle size for customers who are not using surveys (434 bytes), it reduces the overall size when surveys are loaded.

Plus, this centralizes the logic on wether or not a survey is eligible in a single function.

can we do it the other way around? so we get the refactoring gains without increasing the main bundle?

@lucasheriques lucasheriques changed the title fix: refactor canRenderSurvey fix: refactor canRenderSurvey and move logic to SurveyManager Apr 21, 2025
@lucasheriques lucasheriques added the bump patch Bump patch version when this PR gets merged label Apr 21, 2025
@lucasheriques
Copy link
Contributor Author

@marandaneto @ioannisj I decided to move more business logic to the SurveyManager instead, so we can better centralize the logic and also reduce the main bundle size. Now it has -2KB, which are only downloaded if we actually load surveys functionality.

@@ -14,6 +14,7 @@ import {
import { DeadClicksAutocapture, isDeadClicksEnabledForAutocapture } from './extensions/dead-clicks-autocapture'
import { ExceptionObserver } from './extensions/exception-autocapture'
import { errorToProperties } from './extensions/exception-autocapture/error-conversion'
import { HistoryAutocapture } from './extensions/history-autocapture'
Copy link
Member

Choose a reason for hiding this comment

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

Most of your PRs auto-format the imports, which adds noise to the PR review.
Can you double-check that your auto-format config is the same as everyone else's?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's caused by a setting I have on VS Code. I created a PR to prevent it from having effect in this repo

@marandaneto
Copy link
Member

@marandaneto @ioannisj I decided to move more business logic to the SurveyManager instead, so we can better centralize the logic and also reduce the main bundle size. Now it has -2KB, which are only downloaded if we actually load surveys functionality.

nice, left 2 comments otherwise LGTM

Co-authored-by: Manoel Aranda Neto <[email protected]>
@lucasheriques lucasheriques merged commit fa5ba3b into main Apr 24, 2025
27 checks passed
@lucasheriques lucasheriques deleted the fix/refactor-canRenderSurvey branch April 24, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged feature/surveys
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants