Skip to content

Commit

Permalink
chore: small readability refactor to getActiveMatchingSurveys
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasheriques committed Feb 5, 2025
1 parent c46550a commit 3d2c2ea
Showing 1 changed file with 15 additions and 18 deletions.
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)

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

0 comments on commit 3d2c2ea

Please sign in to comment.