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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { PostHog } from './posthog-core'
import {
Survey,
SurveyCallback,
SurveyMatchType,
SurveyQuestionBranchingType,
SurveyQuestionType,
SurveyMatchType,
} from './posthog-surveys-types'
import { RemoteConfig } from './types'
import { Info } from './utils/event-utils'
Expand Down Expand Up @@ -298,6 +298,13 @@ export class PostHogSurveys {
}
}

private isSurveyFeatureFlagEnabled(flagKey: string | null) {
if (!flagKey) {
return true
}
return this.instance.featureFlags.isFeatureEnabled(flagKey)
}

getActiveMatchingSurveys(callback: SurveyCallback, forceReload = false) {
this.getSurveys((surveys) => {
const activeSurveys = surveys.filter((survey) => {
Expand Down Expand Up @@ -328,30 +335,20 @@ export class PostHogSurveys {
) {
return true
}
const linkedFlagCheck = survey.linked_flag_key
? this.instance.featureFlags.isFeatureEnabled(survey.linked_flag_key)
: true
const targetingFlagCheck = survey.targeting_flag_key
? this.instance.featureFlags.isFeatureEnabled(survey.targeting_flag_key)
: true
const linkedFlagCheck = this.isSurveyFeatureFlagEnabled(survey.linked_flag_key)
const targetingFlagCheck = this.isSurveyFeatureFlagEnabled(survey.targeting_flag_key)

const hasEvents =
survey.conditions?.events &&
survey.conditions?.events?.values &&
survey.conditions?.events?.values.length > 0
const hasEvents = (survey.conditions?.events?.values?.length ?? 0) > 0
const hasActions = (survey.conditions?.actions?.values?.length ?? 0) > 0

const hasActions =
survey.conditions?.actions &&
survey.conditions?.actions?.values &&
survey.conditions?.actions?.values.length > 0
const eventBasedTargetingFlagCheck =
hasEvents || hasActions ? activatedSurveys?.includes(survey.id) : true

const overrideInternalTargetingFlagCheck = this._canActivateRepeatedly(survey)
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)
Comment on lines 348 to +350
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


const flagsCheck = this.checkFlags(survey)
return (
linkedFlagCheck &&
Expand Down
Loading