From 3d2c2ea3ffec8325a5f0000bff84e570edc106c7 Mon Sep 17 00:00:00 2001 From: Lucas Faria Date: Wed, 5 Feb 2025 18:28:52 -0300 Subject: [PATCH] chore: small readability refactor to getActiveMatchingSurveys --- src/posthog-surveys.ts | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/posthog-surveys.ts b/src/posthog-surveys.ts index 7fe4a1b04..5b7ac45a6 100644 --- a/src/posthog-surveys.ts +++ b/src/posthog-surveys.ts @@ -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' @@ -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) => { @@ -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 &&