Skip to content
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

chore: small readability refactor to getActiveMatchingSurveys #1718

Merged

Conversation

lucasheriques
Copy link
Contributor

@lucasheriques lucasheriques commented Feb 5, 2025

Changes

small readability refactor for getActiveMatchingSurveys

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!)

@lucasheriques lucasheriques requested a review from a team as a code owner February 5, 2025 22:04
Copy link

vercel bot commented Feb 5, 2025

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

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Feb 5, 2025 10:08pm

@lucasheriques lucasheriques added the bump patch Bump patch version when this PR gets merged label Feb 5, 2025
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 modifies the survey targeting logic in posthog-js by refactoring the getActiveMatchingSurveys method for improved readability and changing how internal targeting flags are evaluated.

  • Changed internal targeting flag logic in src/posthog-surveys.ts to use OR instead of AND, allowing surveys to show if either override check passes or feature flag is enabled
  • Extracted feature flag checks into new private method isSurveyFeatureFlagEnabled for better code organization
  • Simplified event/action condition checks using nullish coalescing operator
  • Reordered imports for better code structure

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

Comment on lines 348 to +350
const internalTargetingFlagCheck =
survey.internal_targeting_flag_key && !overrideInternalTargetingFlagCheck
? this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)
: true
overrideInternalTargetingFlagCheck ||
this.isSurveyFeatureFlagEnabled(survey.internal_targeting_flag_key)
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: This changes the behavior of internal targeting flags. Previously required flag check if no override, now passes if either condition is true. May cause surveys to show more frequently than intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is incorrect

in the first version, we had:

When overrideInternalTargetingFlagCheck is true:

  • First version: returns true (second part of the ternary)
  • Second version: returns true (first part of OR condition)

When overrideInternalTargetingFlagCheck is false (i.e. we call isSurveyFeatureFlagEnabled):

If survey.internal_targeting_flag_key is null:

  • First version: returns true (second part of the ternary)
  • Second version: returns true (since isSurveyFeatureFlagEnabled returns true)

If survey.internal_targeting_flag_key is defined:

  • First version: returns isFeatureEnabled(survey.internal_targeting_flag_key)
  • Second version: returns isFeatureEnabled(survey.internal_targeting_flag_key)

Copy link
Member

@marandaneto marandaneto Feb 6, 2025

Choose a reason for hiding this comment

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

do we have unit tests for that so the change will break if not correct?
one thing to pay attention to is that the former uses && and you use ||.
the other thing is that your description says: 'When overrideInternalTargetingFlagCheck is true', but the former does '!overrideInternalTargetingFlagCheck', so I'm not confident that it's the same without running the code, can you double check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, there are unit tests for getActiveMatchingSurveys here, which is why I relied on them to keep the same functionality

Copy link

github-actions bot commented Feb 5, 2025

Size Change: -2.15 kB (-0.07%)

Total Size: 3.3 MB

Filename Size Change
dist/array.full.es5.js 267 kB -193 B (-0.07%)
dist/array.full.js 370 kB -217 B (-0.06%)
dist/array.full.no-external.js 369 kB -217 B (-0.06%)
dist/array.js 183 kB -217 B (-0.12%)
dist/array.no-external.js 181 kB -217 B (-0.12%)
dist/main.js 183 kB -217 B (-0.12%)
dist/module.full.js 370 kB -217 B (-0.06%)
dist/module.full.no-external.js 369 kB -217 B (-0.06%)
dist/module.js 183 kB -217 B (-0.12%)
dist/module.no-external.js 181 kB -217 B (-0.12%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 215 kB
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 69.4 kB
dist/surveys.js 72.4 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@lucasheriques
Copy link
Contributor Author

@pauldambra slowly killing off some bytes from surveys part 🙏

Copy link

@ioannisj ioannisj left a comment

Choose a reason for hiding this comment

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

LG

@lucasheriques lucasheriques merged commit 8e216bf into main Feb 6, 2025
33 checks passed
@lucasheriques lucasheriques deleted the chore/small-refactor-to-get-active-matching-surveys branch February 6, 2025 13:05
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants