:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import {
MakeLogicType,
actions,
afterMount,
connect,
events,
kea,
key,
listeners,
path,
props,
reducers,
selectors,
} from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { loaders } from 'kea-loaders'
import { actionToUrl, router, urlToAction } from 'kea-router'
import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
import { isApprovalRequiredError } from 'lib/api-error'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { FEATURE_FLAGS } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { FeatureFlagsSet, featureFlagLogic as enabledFlagLogic } from 'lib/logic/featureFlagLogic'
import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
import { isObject } from 'lib/utils/guards'
import { HogQLQueryString } from 'lib/utils/hogql'
import { hasFormErrors, objectClean } from 'lib/utils/objects'
import { allOperatorsMapping } from 'lib/utils/operators'
import { showApprovalRequiredToast } from 'scenes/approvals/ApprovalRequiredBanner'
import { maxGlobalLogic } from 'scenes/max/maxGlobalLogic'
import { projectLogic } from 'scenes/projectLogic'
import { Scene } from 'scenes/sceneTypes'
import {
branchingConfigToDropdownValue,
buildDeleteIndexMap,
buildReorderIndexMap,
canQuestionHaveResponseBasedBranching,
createBranchingConfig,
getDefaultBranchingType,
remapBranchingIndices,
} from 'scenes/surveys/components/question-branching/utils'
import { getDemoDataForSurvey } from 'scenes/surveys/utils/demoDataGenerator'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { refreshTreeItem } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import {
CompareFilter,
DataTableNode,
InsightVizNode,
NodeKind,
ProductIntentContext,
ProductKey,
} from '~/queries/schema/schema-general'
import { SurveyAnalysisQuestionGroup, SurveyAnalysisResponseItem } from '~/queries/schema/schema-surveys'
import {
ActivityScope,
AnyPropertyFilter,
BaseMathType,
Breadcrumb,
ChoiceQuestionProcessedResponses,
ChoiceQuestionResponseData,
ConsolidatedSurveyResults,
CyclotronJobFiltersType,
EventPropertyFilter,
FeatureFlagFilters,
HogFunctionType,
IntervalType,
LinkSurveyQuestion,
MultipleSurveyQuestion,
OpenQuestionProcessedResponses,
OpenQuestionResponseData,
ProjectTreeRef,
PropertyFilterType,
PropertyOperator,
RatingSurveyQuestion,
ResponsesByQuestion,
Survey,
SurveyEventName,
SurveyEventProperties,
SurveyEventStats,
SurveyMatchType,
SurveyQuestion,
SurveyQuestionBase,
SurveyQuestionBranchingType,
SurveyQuestionType,
SurveyRates,
SurveySchedule,
SurveyStats,
} from '~/types'
import { surveysGenerateTranslationsCreate } from 'products/surveys/frontend/generated/api'
import { reportSurveyCreated, reportSurveyEdited } from 'products/surveys/frontend/surveyUsage'
import type { ProductIntentProperties } from '../../lib/utils/product-intents'
import type {
AccessControlLevel,
BasicSurveyQuestion,
FeatureFlagBasicType,
PropertyDefinition,
SurveyAppearance,
SurveyDisplayConditions,
SurveyQuestionDescriptionContentType,
SurveyType,
TeamPublicType,
TeamType,
UserType,
} from '../../types'
import {
BACK_BUTTON_DEFAULT_TEXT,
INTRO_SCREEN_DEFAULT_BUTTON_TEXT,
INTRO_SCREEN_TEXT_FIELDS,
LOADING_SURVEY_RESULTS_TOAST_ID,
NEW_SURVEY,
NewSurvey,
SURVEY_CREATED_SOURCE,
SURVEY_RATING_SCALE,
TRANSLATION_NEEDED_PLACEHOLDER,
defaultSurveyAppearance,
defaultSurveyFieldValues,
} from './constants'
import { getSurveyStatus, surveysLogic } from './surveysLogic'
import type { SurveyDataState } from './surveysLogic'
import { buildChoiceTranslationMap } from './surveyTranslationUtils'
import { SurveyFeatureWarning, getSurveyWarnings } from './surveyVersionRequirements'
import type { TeamSdkVersions } from './surveyVersionRequirements'
import {
DATE_FORMAT,
type OpenEndedColumnMap,
type SurveyQueryFilters,
type SurveyResponseColumn,
type SurveyResponseOutcome,
buildAggregateQuery,
buildOpenEndedQuery,
buildSurveyResponsesQuery,
buildSurveyResponsesExportQuery,
buildSurveyResponseStatsQuery,
buildSurveyRespondentQuery,
buildSurveyOptionalBooleanPropertyFilter,
buildSurveyTimestampFilter,
calculateSurveyRates,
createAnswerFilterHogQLExpression,
getResponseFieldWithId,
getSurveyEndDateForQuery,
getSurveyResponseOutcomeBreakdown,
getSurveyStartDateForQuery,
isSurveyRunning,
isThumbQuestion,
sanitizeSurvey,
sanitizeSurveyAppearance,
surveyResponseColumnId,
validateSurveyAppearance,
v1TargetingFlagFilters,
} from './utils'
export type SurveyBaseStatTuple = [
eventName: string,
totalCount: number,
uniquePersons: number,
firstSeen: string | null,
lastSeen: string | null,
outcomeCounts?: [number, number, number],
] // [event_name, total_count, unique_persons, first_seen, last_seen, outcome_counts]
export type SurveyBaseStatsResult = SurveyBaseStatTuple[] | null
export type DismissedAndSentCountResult = number | null
export type TranslationValidationError = {
language: string
questionIndex: number
field: string
error: string
}
type SurveyTranslationField = keyof NonNullable<Survey['translations']>[string]
type QuestionTranslation = NonNullable<SurveyQuestion['translations']>[string]
type QuestionTextTranslationField = Exclude<keyof QuestionTranslation, 'choices'>
type SurveyTranslationDraftQuestion = {
id?: string | null
type?: SurveyQuestionType
question?: string
description?: string | null
buttonText?: string
choices?: string[]
lowerBoundLabel?: string
upperBoundLabel?: string
link?: string | null
translations?: SurveyQuestion['translations']
}
type SurveyTranslationDraftPayload = {
name?: string
description?: string | null
type?: Survey['type']
appearance?: {
thankYouMessageHeader?: string
thankYouMessageDescription?: string
thankYouMessageCloseButtonText?: string
introScreenHeader?: string
introScreenDescription?: string
introScreenButtonText?: string
backButtonText?: string
}
questions?: SurveyTranslationDraftQuestion[]
translations?: Survey['translations']
}
type TranslationFieldCheck<T extends string> = {
key: T
defaultValue?: string | null
}
const SURVEY_QUERY_TAG_BASE = { scene: 'Survey' as const, productKey: 'surveys' as const }
const DRAFT_TRANSLATION_QUESTION_ID_PREFIX = '__draft_question_'
const SURVEY_NOTIFICATION_LIST_LIMIT = 100
function getSurveyIdsFromNotificationFilters(filters?: CyclotronJobFiltersType | null): Set<string> {
const surveyIds = new Set<string>()
for (const event of filters?.events ?? []) {
for (const property of event.properties ?? []) {
if (property.key !== SurveyEventProperties.SURVEY_ID) {
continue
}
const value = property.value
if (Array.isArray(value)) {
value.forEach((surveyId) => {
if (typeof surveyId === 'string') {
surveyIds.add(surveyId)
}
})
} else if (typeof value === 'string') {
surveyIds.add(value)
}
}
}
return surveyIds
}
function getTranslationDraftQuestionId(question: SurveyQuestion, index: number): string {
return question.id || `${DRAFT_TRANSLATION_QUESTION_ID_PREFIX}${index}`
}
export function getSurveyTranslationDraftPayload(survey: Survey | NewSurvey): SurveyTranslationDraftPayload {
return {
name: survey.name,
description: survey.description,
type: survey.type,
appearance: {
thankYouMessageHeader: survey.appearance?.thankYouMessageHeader,
thankYouMessageDescription: survey.appearance?.thankYouMessageDescription,
thankYouMessageCloseButtonText: survey.appearance?.thankYouMessageCloseButtonText,
introScreenHeader: survey.appearance?.introScreenHeader,
introScreenDescription: survey.appearance?.introScreenDescription,
introScreenButtonText: survey.appearance?.displayIntroScreen
? survey.appearance.introScreenButtonText || INTRO_SCREEN_DEFAULT_BUTTON_TEXT
: survey.appearance?.introScreenButtonText,
backButtonText: survey.appearance?.allowGoBack
? survey.appearance.backButtonText || BACK_BUTTON_DEFAULT_TEXT
: undefined,
},
questions: survey.questions.map((question, index): SurveyTranslationDraftQuestion => {
const draftQuestion: SurveyTranslationDraftQuestion = {
id: getTranslationDraftQuestionId(question, index),
type: question.type,
question: question.question,
description: question.description,
buttonText: question.buttonText,
translations: question.translations,
}
if ('choices' in question) {
draftQuestion.choices = question.choices
}
if ('lowerBoundLabel' in question) {
draftQuestion.lowerBoundLabel = question.lowerBoundLabel
}
if ('upperBoundLabel' in question) {
draftQuestion.upperBoundLabel = question.upperBoundLabel
}
if ('link' in question) {
draftQuestion.link = question.link
}
return draftQuestion
}),
translations: survey.translations,
}
}
const SURVEY_QUERY_TAGS = {
baseStats: { ...SURVEY_QUERY_TAG_BASE, name: 'survey_base_stats' as const },
dismissedAndSent: {
...SURVEY_QUERY_TAG_BASE,
name: 'survey_dismissed_sent_overlap' as const,
},
aggregateResults: { ...SURVEY_QUERY_TAG_BASE, name: 'survey_results_aggregate' as const },
openEndedResults: { ...SURVEY_QUERY_TAG_BASE, name: 'survey_results_open_ended' as const },
}
const isChoiceSurveyQuestion = (question: SurveyQuestion): question is MultipleSurveyQuestion =>
question.type === SurveyQuestionType.SingleChoice || question.type === SurveyQuestionType.MultipleChoice
const isLinkSurveyQuestion = (question: SurveyQuestion): question is LinkSurveyQuestion =>
question.type === SurveyQuestionType.Link
const isRatingSurveyQuestion = (question: SurveyQuestion): question is RatingSurveyQuestion =>
question.type === SurveyQuestionType.Rating
const DEFAULT_OPERATORS: Record<SurveyQuestionType, { label: string; value: PropertyOperator }> = {
[SurveyQuestionType.Open]: {
label: allOperatorsMapping[PropertyOperator.IContains],
value: PropertyOperator.IContains,
},
[SurveyQuestionType.Rating]: {
label: allOperatorsMapping[PropertyOperator.Exact],
value: PropertyOperator.Exact,
},
[SurveyQuestionType.SingleChoice]: {
label: allOperatorsMapping[PropertyOperator.Exact],
value: PropertyOperator.Exact,
},
[SurveyQuestionType.MultipleChoice]: {
label: allOperatorsMapping[PropertyOperator.IContains],
value: PropertyOperator.IContains,
},
[SurveyQuestionType.Link]: {
label: allOperatorsMapping[PropertyOperator.Exact],
value: PropertyOperator.Exact,
},
}
export type SurveyDemoData = ReturnType<typeof getDemoDataForSurvey>
export enum SurveyTab {
SUMMARY = 'summary',
RESPONSES = 'responses',
NOTIFICATIONS = 'notifications',
HISTORY = 'history',
}
export enum SurveyEditSection {
Steps = 'steps',
Widget = 'widget',
Presentation = 'presentation',
Appearance = 'appearance',
Customization = 'customization',
DisplayConditions = 'DisplayConditions',
Scheduling = 'scheduling',
CompletionConditions = 'CompletionConditions',
}
export interface SurveyLogicProps {
/** Either a UUID or 'new'. */
id: string
}
export interface SurveyMetricsQueries {
surveysShown: DataTableNode
surveysDismissed: DataTableNode
}
export interface SurveyRatingResults {
[key: number]: {
data: number[]
total: number
}
}
export interface SurveyRecurringNPSResults {
[key: number]: {
data: number[]
total: number
}
}
export interface SurveySingleChoiceResults {
[key: number]: {
labels: string[]
data: number[]
total: number
}
}
export interface SurveyMultipleChoiceResults {
[key: number]: {
labels: string[]
data: number[]
}
}
export interface SurveyOpenTextResults {
[key: number]: {
events: { distinct_id: string; properties: Record<string, any>; personProperties: Record<string, any> }[]
}
}
export interface QuestionResultsReady {
[key: string]: boolean
}
export type DataCollectionType = 'until_stopped' | 'until_limit' | 'until_adaptive_limit'
export interface SurveyDateRange {
date_from: string | null
date_to: string | null
}
type AggregateRow = [string, string, number]
type AggregateEntries = [string, number][]
function processChoiceQuestion(
question: MultipleSurveyQuestion,
entries: AggregateEntries,
questionType: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice
): ChoiceQuestionProcessedResponses {
const totalEntry = entries.find(([l]) => l === '__total__')
const dataEntries = entries.filter(([l]) => l !== '__total__')
const choiceMap = buildChoiceTranslationMap(question)
let total = 0
const noResponseEntry = entries.find(([l]) => l === '__no_response__')
const noResponseCount = noResponseEntry ? noResponseEntry[1] : 0
const filteredEntries = dataEntries.filter(([l]) => l !== '__no_response__')
// Normalise each response to its base-language choice so answers given in different
// languages aggregate under one option instead of splitting into separate rows.
const countsByLabel = new Map<string, number>()
for (const [label, count] of filteredEntries) {
const normalizedLabel = choiceMap.get(label) ?? label
countsByLabel.set(normalizedLabel, (countsByLabel.get(normalizedLabel) ?? 0) + count)
if (questionType === SurveyQuestionType.SingleChoice) {
total += count
}
}
const data: ChoiceQuestionResponseData[] = [...countsByLabel.entries()]
.map(([label, value]) => ({ label, value, isPredefined: choiceMap.has(label) }))
.sort((a, b) => b.value - a.value)
if (questionType === SurveyQuestionType.MultipleChoice && totalEntry) {
total = totalEntry[1]
}
// Zero-fill predefined choices (excluding open choice)
question.choices?.forEach((choice: string, choiceIndex: number) => {
const isOpenChoice = question.hasOpenChoice && choiceIndex === question.choices.length - 1
if (!isOpenChoice && !data.some((d) => d.label === choice)) {
data.push({ label: choice, value: 0, isPredefined: true })
}
})
return {
type: questionType,
data,
totalResponses: total,
noResponseCount,
}
}
function processRatingQuestion(
question: RatingSurveyQuestion,
entries: AggregateEntries
): ChoiceQuestionProcessedResponses {
const scaleSize = question.scale === SURVEY_RATING_SCALE.NPS_10_POINT ? 11 : question.scale
const counts = new Array(scaleSize).fill(0)
let total = 0
entries.forEach(([label, count]) => {
const parsedValue = parseInt(label, 10)
if (isNaN(parsedValue)) {
return
}
let arrayIndex: number
let isValid = false
if (question.scale === SURVEY_RATING_SCALE.NPS_10_POINT) {
// NPS scale: 0-10 (11 values)
isValid = parsedValue >= 0 && parsedValue <= 10
arrayIndex = parsedValue
} else {
// Regular rating scales: 1-N (N values, but we use 0-based indexing)
// For a 5-point scale, accept ratings 1-5 and map them to indices 0-4
isValid = parsedValue >= 1 && parsedValue <= question.scale
arrayIndex = parsedValue - 1 // Convert 1-based to 0-based
}
if (isValid) {
counts[arrayIndex] = count
total += count
}
})
const data = counts.map((count, index) => {
// For display labels:
// - NPS (scale 10): show 0-10
// - Regular scales: show 1-N (convert from 0-based index)
const label = question.scale === SURVEY_RATING_SCALE.NPS_10_POINT ? index.toString() : (index + 1).toString()
return {
label,
value: count,
isPredefined: true,
}
})
return {
type: SurveyQuestionType.Rating,
data,
totalResponses: total,
noResponseCount: 0,
}
}
function processOpenQuestion(entries: AggregateEntries): OpenQuestionProcessedResponses {
const total = entries.find(([l]) => l === '__total__')?.[1] ?? 0
return { type: SurveyQuestionType.Open, data: [], totalResponses: total }
}
export function processResultsForSurveyQuestions(
questions: SurveyQuestion[],
rows: AggregateRow[] | null
): ResponsesByQuestion {
if (!rows) {
return {}
}
const grouped: Record<string, AggregateEntries> = {}
for (const [qid, label, count] of rows) {
if (!grouped[qid]) {
grouped[qid] = []
}
grouped[qid].push([label, count])
}
const responsesByQuestion: ResponsesByQuestion = {}
questions.forEach((question) => {
// Skip questions without IDs or Link questions
if (!question.id || !grouped[question.id] || question.type === SurveyQuestionType.Link) {
return
}
const entries = grouped[question.id]
switch (question.type) {
case SurveyQuestionType.SingleChoice:
case SurveyQuestionType.MultipleChoice:
responsesByQuestion[question.id] = processChoiceQuestion(question, entries, question.type)
break
case SurveyQuestionType.Rating:
responsesByQuestion[question.id] = processRatingQuestion(question, entries)
break
case SurveyQuestionType.Open:
responsesByQuestion[question.id] = processOpenQuestion(entries)
break
}
})
return responsesByQuestion
}
function collectOpenChoiceResponses(
question: MultipleSurveyQuestion,
questionType: SurveyQuestionType,
rows: any[][],
columnIndex: number,
distinctIdIdx: number,
timestampIdx: number
): ChoiceQuestionResponseData[] {
const choiceMap = buildChoiceTranslationMap(question)
const otherData: ChoiceQuestionResponseData[] = []
for (const row of rows) {
const rawValue = row[columnIndex]
if (!rawValue) {
continue
}
// Multiple choice values come as string arrays, single choice as a single string
let choices: string[]
if (questionType === SurveyQuestionType.MultipleChoice) {
choices = (rawValue as string[]).map((v) => v.replace(/^['"]+|['"]+$/g, ''))
} else {
choices = [rawValue as string]
}
for (const choice of choices) {
if (choice && !choiceMap.has(choice)) {
otherData.push({
label: choice,
value: 1,
isPredefined: false,
distinctId: row[distinctIdIdx] as string,
timestamp: row[timestampIdx] as string,
})
}
}
}
return otherData
}
export function processOpenEndedResults(
questions: SurveyQuestion[],
columnMap: OpenEndedColumnMap,
rows: any[][] | null
): ResponsesByQuestion {
if (!rows) {
return {}
}
const numCols = Object.keys(columnMap).length
const distinctIdIdx = numCols
const timestampIdx = numCols + 1
const sessionIdIdx = numCols + 2
const result: ResponsesByQuestion = {}
for (const [questionId, { columnIndex, type }] of Object.entries(columnMap)) {
if (type === SurveyQuestionType.Open) {
const data: OpenQuestionResponseData[] = []
for (const row of rows) {
const value = row[columnIndex] as string
if (!value) {
continue
}
data.push({
distinctId: row[distinctIdIdx] as string,
response: value,
timestamp: row[timestampIdx] as string,
sessionId: (row[sessionIdIdx] as string) || undefined,
})
}
result[questionId] = { type: SurveyQuestionType.Open, data, totalResponses: data.length }
} else {
const question = questions.find((q) => q.id === questionId) as MultipleSurveyQuestion | undefined
if (!question) {
continue
}
const otherData = collectOpenChoiceResponses(question, type, rows, columnIndex, distinctIdIdx, timestampIdx)
if (otherData.length > 0) {
result[questionId] = { type, data: otherData, totalResponses: 0, noResponseCount: 0 }
}
}
}
return result
}
export function mergeResponsesByQuestion(
aggregate: ResponsesByQuestion,
openEnded: ResponsesByQuestion
): ResponsesByQuestion {
const merged = { ...aggregate }
for (const [qid, openData] of Object.entries(openEnded)) {
const agg = merged[qid]
if (!agg) {
merged[qid] = openData
} else if (openData.type === SurveyQuestionType.Open) {
merged[qid] = { ...openData, totalResponses: agg.totalResponses }
} else {
const aggChoice = agg as ChoiceQuestionProcessedResponses
const predefinedFromAggregate = aggChoice.data.filter((d) => d.isPredefined)
merged[qid] = { ...aggChoice, data: [...predefinedFromAggregate, ...openData.data] }
}
}
return merged
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyLogicValues {
enabledFlags: FeatureFlagsSet // enabledFlagLogic
dataProcessingAccepted: boolean // maxGlobalLogic
propertyDefinitionsByType: (type: string, groupTypeIndex?: number | null) => PropertyDefinition[] // propertyDefinitionsModel
data: SurveyDataState // surveysLogic
teamSdkVersions: TeamSdkVersions // surveysLogic
currentTeam: TeamPublicType | TeamType | null // teamLogic
user: UserType | null // userLogic
activeAnswerFiltersCount: number
activeResultsFilterCount: number
activeTab: SurveyTab
aiGeneratedTranslationFields: string[]
answerFilterHogQLExpression: string
answerFilters: EventPropertyFilter[]
archivedResponseUuids: Set<string>
archivedResponseUuidsLoading: boolean
archivedResponsesFilter: string
archivedResponsesPropertyFilter: Array<{
key: string
type: PropertyFilterType.HogQL
}>
breadcrumbs: Breadcrumb[]
compareFilter: CompareFilter
consolidatedSurveyResults: any
consolidatedSurveyResultsLoading: boolean
dataCollectionType: DataCollectionType
dataTableQuery: DataTableNode | null
dateRange: SurveyDateRange | null
defaultAnswerFilters: EventPropertyFilter[]
defaultInterval: IntervalType
derivedDataCollectionType: DataCollectionType
descriptionContentType: (questionIndex: number) => SurveyQuestionDescriptionContentType | undefined
deviceTypesMatchTypeValidationError: string | null
editingLanguage: string | null
enrichedConsolidatedSurveyResults: ConsolidatedSurveyResults
expandedResponseUuids: Set<string>
filterSurveyStatsByDistinctId: boolean
flagPropertyErrors: any
formattedOpenEndedResponses: SurveyAnalysisQuestionGroup[]
generatingTranslationDrafts: boolean
getBranchingDropdownValue: (questionIndex: number, question: SurveyQuestion) => string
getResponseBasedBranchingDropdownValue: (
questionIndex: number,
question: MultipleSurveyQuestion | RatingSurveyQuestion,
response: any
) => any
hasActiveAnswerFilters: boolean
hasActiveDateRange: boolean
hasActiveFilters: boolean
hasBranchingLogic: boolean
hasCycle: false
hasTargetingSet: boolean
hasTranslationValidationErrors: boolean
interval: IntervalType | null
isAdaptiveLimitFFEnabled: boolean
isAnyResultsLoading: boolean
isEditingSurvey: boolean
isSurveyHeadlineEnabled: boolean
isSurveyRunning: boolean
isSurveySubmitting: boolean
isSurveyValid: boolean
personNames: Record<string, string>
processedSurveyStats: SurveyStats | null
projectTreeRef: ProjectTreeRef
propertyFilters: AnyPropertyFilter[]
responseColumns: SurveyResponseColumn[]
responsesExportQuery: DataTableNode | null
resultsFiltersExpanded: boolean
resultsRequeryInProgress: boolean
reusableSurveyNotifications: HogFunctionType[]
reusableSurveyNotificationsLoading: boolean
selectedPageIndex: number | null
selectedSection: SurveyEditSection | null
showArchivedResponses: boolean
showSurveyErrors: boolean
showSurveyRepeatSchedule: boolean
sidePanelContext: SidePanelSceneContext | null
survey: NewSurvey | Survey
surveyAllErrors: Record<string, any>
surveyAsInsightURL: string
surveyBaseStats: any
surveyBaseStatsInternal: SurveyBaseStatsResult
surveyBaseStatsLoading: boolean
surveyChanged: boolean
surveyDemoData: SurveyDemoData
surveyDismissedAndSentCount: any
surveyDismissedAndSentCountInternal: DismissedAndSentCountResult
surveyDismissedAndSentCountLoading: boolean
surveyErrors: DeepPartialMap<NewSurvey | Survey, ValidationErrorType>
surveyHasErrors: boolean
surveyHeadline: {
has_more: boolean
headline: string
responses_sampled: number
} | null
surveyHeadlineLoading: boolean
surveyLoading: boolean
surveyManualErrors: Record<string, any>
surveyMissing: boolean
surveyNotifications: HogFunctionType[]
surveyNotificationsLoading: boolean
surveyRates: SurveyRates | null
surveyRepeatedActivationAvailable: boolean
surveyResponseOutcomes: SurveyResponseOutcome[] | null
surveyShufflingQuestionsAvailable: boolean
surveyTouched: boolean
surveyTouches: Record<string, boolean>
surveyUsesAdaptiveLimit: boolean
surveyUsesLimit: boolean
surveyValidationErrors: DeepPartialMap<NewSurvey | Survey, ValidationErrorType>
surveyWarnings: SurveyFeatureWarning[]
targetingFlagFilters: FeatureFlagFilters | undefined
timestampFilter: string
translationErrorsByQuestion: (questionIndex: number) => TranslationValidationError[]
translationErrorsForField: (questionIndex: number, fieldPath: string) => TranslationValidationError | undefined
translationValidationErrors: TranslationValidationError[]
urlMatchTypeValidationError: string | null
urlSearchParams: {
answerFilters: string | undefined
date_from: string | undefined
date_to: string | undefined
propertyFilters: string | undefined
}
writingHTMLDescription: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyLogicActions {
loadSurveys: () => any // surveysLogic
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
addResponseColumn: (column: SurveyResponseColumn) => {
column: SurveyResponseColumn
}
archiveResponse: (responseUuid: string) => {
responseUuid: string
}
archiveSurvey: () => {
value: true
}
clearAiGeneratedTranslationField: (path: string) => {
path: string
}
clearFilters: () => {
value: true
}
createSurvey: (surveyPayload: Partial<Survey>) => Partial<Survey>
createSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
createSurveySuccess: (
survey: Survey,
payload?: Partial<Survey>
) => {
survey: Survey
payload?: Partial<Survey>
}
deleteBranchingLogic: () => {
value: true
}
deleteSurveyNotification: (notification: HogFunctionType) => {
notification: HogFunctionType
}
editingSurvey: (editing: boolean) => {
editing: boolean
}
generateTranslationDrafts: (
language: string,
overwrite?: boolean
) => {
language: string
overwrite: boolean
}
launchSurvey: () => any
launchSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
launchSurveySuccess: (
survey: Survey,
payload?: any
) => {
survey: Survey
payload?: any
}
loadArchivedResponseUuids: () => any
loadArchivedResponseUuidsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadArchivedResponseUuidsSuccess: (
archivedResponseUuids: Set<string>,
payload?: any
) => {
archivedResponseUuids: Set<string>
payload?: any
}
loadConsolidatedSurveyResults: () => any
loadConsolidatedSurveyResultsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadConsolidatedSurveyResultsSuccess: (
consolidatedSurveyResults: ConsolidatedSurveyResults,
payload?: any
) => {
consolidatedSurveyResults: ConsolidatedSurveyResults
payload?: any
}
loadReusableSurveyNotifications: () => any
loadReusableSurveyNotificationsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadReusableSurveyNotificationsSuccess: (
reusableSurveyNotifications: HogFunctionType[],
payload?: any
) => {
reusableSurveyNotifications: HogFunctionType[]
payload?: any
}
loadSurvey: () => any
loadSurveyBaseStats: () => any
loadSurveyBaseStatsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSurveyBaseStatsSuccess: (
surveyBaseStats: SurveyBaseStatsResult,
payload?: any
) => {
surveyBaseStats: SurveyBaseStatsResult
payload?: any
}
loadSurveyDismissedAndSentCount: () => any
loadSurveyDismissedAndSentCountFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSurveyDismissedAndSentCountSuccess: (
surveyDismissedAndSentCount: DismissedAndSentCountResult,
payload?: any
) => {
surveyDismissedAndSentCount: DismissedAndSentCountResult
payload?: any
}
loadSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSurveyHeadline: (forceRefresh?: boolean) => boolean
loadSurveyHeadlineFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSurveyHeadlineSuccess: (
surveyHeadline: {
has_more: boolean
headline: string
responses_sampled: number
} | null,
payload?: boolean
) => {
surveyHeadline: {
has_more: boolean
headline: string
responses_sampled: number
} | null
payload?: boolean
}
loadSurveyNotifications: () => any
loadSurveyNotificationsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSurveyNotificationsSuccess: (
surveyNotifications: HogFunctionType[],
payload?: any
) => {
surveyNotifications: HogFunctionType[]
payload?: any
}
loadSurveySuccess: (
survey:
| Survey
| {
appearance: SurveyAppearance | null
archived: boolean
base_language?: string | null | undefined
conditions: SurveyDisplayConditions | null
current_iteration?: number | null | undefined
description: string
enable_iframe_embedding?: boolean | null | undefined
enable_partial_responses?: boolean | null | undefined
end_date: string | null
form_content?: Record<string, unknown> | null | undefined
headline_response_count?: number | null | undefined
headline_summary?: string | null | undefined
id: 'new'
iteration_count?: number | null | undefined
iteration_frequency_days?: number | null | undefined
iteration_start_dates?: string[] | undefined
linked_flag: FeatureFlagBasicType | null
linked_flag_id: number | null
name: string
questions: (
| BasicSurveyQuestion
| LinkSurveyQuestion
| MultipleSurveyQuestion
| RatingSurveyQuestion
)[]
response_sampling_interval?: number | null | undefined
response_sampling_interval_type?: string | null | undefined
response_sampling_limit?: number | null | undefined
response_sampling_start_date?: string | null | undefined
responses_limit: number | null
schedule?: SurveySchedule | null | undefined
start_date: string | null
targeting_flag: FeatureFlagBasicType | null
targeting_flag_filters?: FeatureFlagFilters | undefined
translations?:
| Record<
string,
{
backButtonText?: string | undefined
introScreenButtonText?: string | undefined
introScreenDescription?: string | undefined
introScreenHeader?: string | undefined
name?: string | undefined
submitButtonText?: string | undefined
thankYouMessageCloseButtonText?: string | undefined
thankYouMessageDescription?: string | undefined
thankYouMessageHeader?: string | undefined
}
>
| null
| undefined
type: SurveyType
user_access_level: AccessControlLevel
},
payload?: any
) => {
survey:
| Survey
| {
appearance: SurveyAppearance | null
archived: boolean
base_language?: string | null | undefined
conditions: SurveyDisplayConditions | null
current_iteration?: number | null | undefined
description: string
enable_iframe_embedding?: boolean | null | undefined
enable_partial_responses?: boolean | null | undefined
end_date: string | null
form_content?: Record<string, unknown> | null | undefined
headline_response_count?: number | null | undefined
headline_summary?: string | null | undefined
id: 'new'
iteration_count?: number | null | undefined
iteration_frequency_days?: number | null | undefined
iteration_start_dates?: string[] | undefined
linked_flag: FeatureFlagBasicType | null
linked_flag_id: number | null
name: string
questions: (
| BasicSurveyQuestion
| LinkSurveyQuestion
| MultipleSurveyQuestion
| RatingSurveyQuestion
)[]
response_sampling_interval?: number | null | undefined
response_sampling_interval_type?: string | null | undefined
response_sampling_limit?: number | null | undefined
response_sampling_start_date?: string | null | undefined
responses_limit: number | null
schedule?: SurveySchedule | null | undefined
start_date: string | null
targeting_flag: FeatureFlagBasicType | null
targeting_flag_filters?: FeatureFlagFilters | undefined
translations?:
| Record<
string,
{
backButtonText?: string | undefined
introScreenButtonText?: string | undefined
introScreenDescription?: string | undefined
introScreenHeader?: string | undefined
name?: string | undefined
submitButtonText?: string | undefined
thankYouMessageCloseButtonText?: string | undefined
thankYouMessageDescription?: string | undefined
thankYouMessageHeader?: string | undefined
}
>
| null
| undefined
type: SurveyType
user_access_level: AccessControlLevel
}
payload?: any
}
markResultsRequeryCompleted: () => {
value: true
}
moveQuestion: (
oldIndex: number,
newIndex: number
) => {
newIndex: number
oldIndex: number
}
removeQuestion: (questionIndex: number) => {
questionIndex: number
}
removeResponseColumn: (column: SurveyResponseColumn) => {
column: SurveyResponseColumn
}
resetBranchingForQuestion: (questionIndex: any) => {
questionIndex: any
}
resetSurvey: (values?: NewSurvey | Survey) => {
values?: NewSurvey | Survey
}
resetSurveyAdaptiveSampling: () => {
value: true
}
resetSurveyResponseLimits: () => {
value: true
}
resetTargeting: () => {
value: true
}
resumeSurvey: () => any
resumeSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
resumeSurveySuccess: (
survey: Survey,
payload?: any
) => {
survey: Survey
payload?: any
}
setActiveTab: (tab: SurveyTab) => {
tab: SurveyTab
}
setAiGeneratedTranslationFields: (paths: string[]) => {
paths: string[]
}
setAnswerFilters: (
filters: EventPropertyFilter[],
reloadResults?: boolean
) => {
filters: EventPropertyFilter[]
reloadResults: boolean
}
setBaseStatsResults: (results: SurveyBaseStatsResult) => {
results: SurveyBaseStatsResult
}
setCompareFilter: (compareFilter: CompareFilter) => {
compareFilter: CompareFilter
}
setDataCollectionType: (dataCollectionType: DataCollectionType) => {
dataCollectionType: DataCollectionType
}
setDateRange: (
dateRange: SurveyDateRange,
reloadResults?: boolean
) => {
dateRange: SurveyDateRange
reloadResults: boolean
}
setDefaultForQuestionType: (
idx: number,
surveyQuestion: SurveyQuestion,
type: SurveyQuestionType
) => {
idx: number
surveyQuestion: SurveyQuestion
type: SurveyQuestionType
}
setDismissedAndSentCount: (count: DismissedAndSentCountResult) => {
count: DismissedAndSentCountResult
}
setEditingLanguage: (language: string | null) => {
language: string | null
}
setFilterSurveyStatsByDistinctId: (filterByDistinctId: boolean) => {
filterByDistinctId: boolean
}
setFlagPropertyErrors: (errors: any) => {
errors: any
}
setGeneratingTranslationDrafts: (generating: boolean) => {
generating: boolean
}
setInterval: (interval: IntervalType) => {
interval: IntervalType
}
setMultipleSurveyQuestion: (
questionIndex: number,
question: MultipleSurveyQuestion,
type: SurveyQuestionType.MultipleChoice | SurveyQuestionType.SingleChoice
) => {
question: MultipleSurveyQuestion
questionIndex: number
type: SurveyQuestionType.MultipleChoice | SurveyQuestionType.SingleChoice
}
setPersonNames: (personNames: Record<string, string>) => {
personNames: Record<string, string>
}
setPropertyFilters: (
propertyFilters: AnyPropertyFilter[],
reloadResults?: boolean
) => {
propertyFilters: AnyPropertyFilter[]
reloadResults: boolean
}
setQuestionBranchingType: (
questionIndex: any,
type: any,
specificQuestionIndex: any
) => {
questionIndex: any
specificQuestionIndex: any
type: any
}
setResponseBasedBranchingForQuestion: (
questionIndex: any,
responseValue: any,
nextStep: any,
specificQuestionIndex: any
) => {
nextStep: any
questionIndex: any
responseValue: any
specificQuestionIndex: any
}
setResponseExpanded: (
uuid: string,
expanded: boolean
) => {
expanded: boolean
uuid: string
}
setResultsFiltersExpanded: (expanded: boolean) => {
expanded: boolean
}
setSelectedPageIndex: (idx: number | null) => {
idx: number | null
}
setSelectedSection: (section: SurveyEditSection | null) => {
section: SurveyEditSection | null
}
setShowArchivedResponses: (show: boolean) => {
show: boolean
}
setSurveyManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setSurveyMissing: () => {
value: true
}
setSurveyValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setSurveyValues: (values: DeepPartial<NewSurvey | Survey>) => {
values: DeepPartial<NewSurvey | Survey>
}
setWritingHTMLDescription: (writingHTML: boolean) => {
writingHTML: boolean
}
startResultsRequery: () => {
value: true
}
stopSurvey: () => any
stopSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
stopSurveySuccess: (
survey: Survey,
payload?: any
) => {
survey: Survey
payload?: any
}
submitSurvey: () => {
value: boolean
}
submitSurveyFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitSurveyRequest: (survey: NewSurvey | Survey) => {
survey: NewSurvey | Survey
}
submitSurveySuccess: (survey: NewSurvey | Survey) => {
survey: NewSurvey | Survey
}
toggleResponseExpansion: (uuid: string) => {
uuid: string
}
toggleSurveyNotificationEnabled: (
notificationId: string,
enabled: boolean
) => {
enabled: boolean
notificationId: string
}
touchSurveyField: (key: string) => {
key: string
}
unarchiveResponse: (responseUuid: string) => {
responseUuid: string
}
updateSurvey: (
surveyPayload: Partial<Survey> & {
intentContext?: ProductIntentContext
}
) => Partial<Survey> & {
intentContext?: ProductIntentContext
}
updateSurveyFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updateSurveySuccess: (
survey: Survey,
payload?: Partial<Survey> & {
intentContext?: ProductIntentContext
}
) => {
survey: Survey
payload?: Partial<Survey> & {
intentContext?: ProductIntentContext
}
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
enrichedConsolidatedSurveyResults: (
consolidatedSurveyResults: any,
personNames: Record<string, string>
) => ConsolidatedSurveyResults
timestampFilter: (survey: NewSurvey | Survey, dateRange: SurveyDateRange | null) => string
archivedResponsesFilter: (showArchivedResponses: boolean, archivedResponseUuids: Set<string>) => string
archivedResponsesPropertyFilter: (
showArchivedResponses: boolean,
archivedResponseUuids: Set<string>
) => Array<{
key: string
type: PropertyFilterType.HogQL
}>
isAdaptiveLimitFFEnabled: (enabledFlags: FeatureFlagsSet) => boolean
isSurveyHeadlineEnabled: (enabledFlags: FeatureFlagsSet) => boolean
isAnyResultsLoading: (
archivedResponseUuidsLoading: boolean,
surveyBaseStatsLoading: boolean,
surveyDismissedAndSentCountLoading: boolean,
consolidatedSurveyResultsLoading: boolean
) => boolean
defaultAnswerFilters: (survey: NewSurvey | Survey) => EventPropertyFilter[]
activeAnswerFiltersCount: (answerFilters: EventPropertyFilter[]) => number
hasActiveAnswerFilters: (activeAnswerFiltersCount: number) => boolean
activeResultsFilterCount: (
activeAnswerFiltersCount: number,
propertyFilters: AnyPropertyFilter[],
showArchivedResponses: boolean
) => number
hasActiveDateRange: (dateRange: SurveyDateRange | null, survey: NewSurvey | Survey) => boolean
hasActiveFilters: (
hasActiveAnswerFilters: boolean,
propertyFilters: AnyPropertyFilter[],
hasActiveDateRange: boolean
) => boolean
isSurveyRunning: (survey: NewSurvey | Survey) => boolean
surveyUsesLimit: (survey: NewSurvey | Survey) => boolean
surveyUsesAdaptiveLimit: (survey: NewSurvey | Survey) => boolean
derivedDataCollectionType: (
surveyUsesAdaptiveLimit: boolean,
surveyUsesLimit: boolean,
isAdaptiveLimitFFEnabled: boolean
) => DataCollectionType
surveyShufflingQuestionsAvailable: (survey: NewSurvey | Survey) => boolean
showSurveyRepeatSchedule: (survey: NewSurvey | Survey) => boolean
descriptionContentType: (
survey: NewSurvey | Survey
) => (questionIndex: number) => SurveyQuestionDescriptionContentType | undefined
surveyRepeatedActivationAvailable: (survey: NewSurvey | Survey) => boolean
hasTargetingSet: (survey: NewSurvey | Survey) => boolean
breadcrumbs: (survey: NewSurvey | Survey) => Breadcrumb[]
sidePanelContext: (survey: NewSurvey | Survey) => SidePanelSceneContext | null
projectTreeRef: (arg: string) => ProjectTreeRef
answerFilterHogQLExpression: (survey: NewSurvey | Survey, answerFilters: EventPropertyFilter[]) => string
dataTableQuery: (
survey: NewSurvey | Survey,
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
timestampFilter: string,
archivedResponsesFilter: string,
responseColumns: SurveyResponseColumn[]
) => DataTableNode | null
responsesExportQuery: (
survey: NewSurvey | Survey,
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
timestampFilter: string,
archivedResponsesFilter: string,
responseColumns: SurveyResponseColumn[]
) => DataTableNode | null
targetingFlagFilters: (survey: NewSurvey | Survey) => FeatureFlagFilters | undefined
urlMatchTypeValidationError: (survey: NewSurvey | Survey) => string | null
urlSearchParams: (
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
dateRange: SurveyDateRange | null,
survey: NewSurvey | Survey
) => {
answerFilters: string | undefined
date_from: string | undefined
date_to: string | undefined
propertyFilters: string | undefined
}
deviceTypesMatchTypeValidationError: (survey: NewSurvey | Survey) => string | null
getBranchingDropdownValue: (
survey: NewSurvey | Survey
) => (questionIndex: number, question: SurveyQuestion) => string
getResponseBasedBranchingDropdownValue: (
survey: NewSurvey | Survey
) => (questionIndex: number, question: MultipleSurveyQuestion | RatingSurveyQuestion, response: any) => any
hasCycle: (survey: NewSurvey | Survey) => false
hasBranchingLogic: (survey: NewSurvey | Survey) => boolean
translationValidationErrors: (survey: NewSurvey | Survey) => TranslationValidationError[]
hasTranslationValidationErrors: (translationValidationErrors: TranslationValidationError[]) => boolean
translationErrorsByQuestion: (
translationValidationErrors: TranslationValidationError[],
editingLanguage: string | null
) => (questionIndex: number) => TranslationValidationError[]
translationErrorsForField: (
translationValidationErrors: TranslationValidationError[],
editingLanguage: string | null
) => (questionIndex: number, fieldPath: string) => TranslationValidationError | undefined
surveyAsInsightURL: (survey: NewSurvey | Survey) => string
defaultInterval: (survey: NewSurvey | Survey) => IntervalType
surveyResponseOutcomes: (surveyBaseStatsInternal: SurveyBaseStatsResult) => SurveyResponseOutcome[] | null
processedSurveyStats: (
surveyBaseStatsInternal: SurveyBaseStatsResult,
surveyDismissedAndSentCountInternal: DismissedAndSentCountResult
) => SurveyStats | null
surveyRates: (processedSurveyStats: SurveyStats | null) => SurveyRates | null
surveyDemoData: (survey: NewSurvey | Survey) => SurveyDemoData
formattedOpenEndedResponses: (
enrichedConsolidatedSurveyResults: ConsolidatedSurveyResults,
survey: NewSurvey | Survey
) => SurveyAnalysisQuestionGroup[]
surveyWarnings: (
survey: NewSurvey | Survey,
teamSdkVersions: Partial<Record<import('./surveyVersionRequirements').SurveySdkType, string | null>>
) => SurveyFeatureWarning[]
}
}
export type surveyLogicType = MakeLogicType<surveyLogicValues, surveyLogicActions, SurveyLogicProps, surveyLogicMeta>
export const surveyLogic = kea<surveyLogicType>([
props({} as SurveyLogicProps),
key(({ id }) => id),
path((key) => ['scenes', 'surveys', 'surveyLogic', key]),
connect(() => ({
actions: [surveysLogic, ['loadSurveys'], teamLogic, ['addProductIntent']],
values: [
enabledFlagLogic,
['featureFlags as enabledFlags'],
surveysLogic,
['data', 'teamSdkVersions'],
userLogic,
['user'],
teamLogic,
['currentTeam'],
propertyDefinitionsModel,
['propertyDefinitionsByType'],
maxGlobalLogic,
['dataProcessingAccepted'],
],
})),
actions({
setActiveTab: (tab: SurveyTab) => ({ tab }),
setEditingLanguage: (language: string | null) => ({ language }),
setSurveyMissing: true,
editingSurvey: (editing: boolean) => ({ editing }),
setDefaultForQuestionType: (idx: number, surveyQuestion: SurveyQuestion, type: SurveyQuestionType) => ({
idx,
surveyQuestion,
type,
}),
setQuestionBranchingType: (questionIndex, type, specificQuestionIndex) => ({
questionIndex,
type,
specificQuestionIndex,
}),
setMultipleSurveyQuestion: (
questionIndex: number,
question: MultipleSurveyQuestion,
type: SurveyQuestionType.MultipleChoice | SurveyQuestionType.SingleChoice
) => ({
questionIndex,
question,
type,
}),
setResponseBasedBranchingForQuestion: (questionIndex, responseValue, nextStep, specificQuestionIndex) => ({
questionIndex,
responseValue,
nextStep,
specificQuestionIndex,
}),
setDataCollectionType: (dataCollectionType: DataCollectionType) => ({
dataCollectionType,
}),
resetBranchingForQuestion: (questionIndex) => ({ questionIndex }),
deleteBranchingLogic: true,
moveQuestion: (oldIndex: number, newIndex: number) => ({ oldIndex, newIndex }),
removeQuestion: (questionIndex: number) => ({ questionIndex }),
archiveSurvey: true,
setWritingHTMLDescription: (writingHTML: boolean) => ({ writingHTML }),
setSelectedPageIndex: (idx: number | null) => ({ idx }),
setSelectedSection: (section: SurveyEditSection | null) => ({ section }),
resetTargeting: true,
resetSurveyAdaptiveSampling: true,
resetSurveyResponseLimits: true,
setFlagPropertyErrors: (errors: any) => ({ errors }),
setPropertyFilters: (propertyFilters: AnyPropertyFilter[], reloadResults: boolean = true) => ({
propertyFilters,
reloadResults,
}),
setAnswerFilters: (filters: EventPropertyFilter[], reloadResults: boolean = true) => ({
filters,
reloadResults,
}),
setDateRange: (dateRange: SurveyDateRange, reloadResults: boolean = true) => ({ dateRange, reloadResults }),
clearFilters: true,
setResultsFiltersExpanded: (expanded: boolean) => ({ expanded }),
setInterval: (interval: IntervalType) => ({ interval }),
setCompareFilter: (compareFilter: CompareFilter) => ({ compareFilter }),
setFilterSurveyStatsByDistinctId: (filterByDistinctId: boolean) => ({ filterByDistinctId }),
addResponseColumn: (column: SurveyResponseColumn) => ({ column }),
removeResponseColumn: (column: SurveyResponseColumn) => ({ column }),
setResponseExpanded: (uuid: string, expanded: boolean) => ({ uuid, expanded }),
toggleResponseExpansion: (uuid: string) => ({ uuid }),
setBaseStatsResults: (results: SurveyBaseStatsResult) => ({ results }),
setDismissedAndSentCount: (count: DismissedAndSentCountResult) => ({ count }),
setShowArchivedResponses: (show: boolean) => ({ show }),
archiveResponse: (responseUuid: string) => ({ responseUuid }),
unarchiveResponse: (responseUuid: string) => ({ responseUuid }),
startResultsRequery: true,
markResultsRequeryCompleted: true,
toggleSurveyNotificationEnabled: (notificationId: string, enabled: boolean) => ({
notificationId,
enabled,
}),
deleteSurveyNotification: (notification: HogFunctionType) => ({ notification }),
setPersonNames: (personNames: Record<string, string>) => ({ personNames }),
generateTranslationDrafts: (language: string, overwrite: boolean = true) => ({ language, overwrite }),
setGeneratingTranslationDrafts: (generating: boolean) => ({ generating }),
setAiGeneratedTranslationFields: (paths: string[]) => ({ paths }),
clearAiGeneratedTranslationField: (path: string) => ({ path }),
}),
loaders(({ props, actions, values }) => ({
surveyHeadline: [
null as { headline: string; responses_sampled: number; has_more: boolean } | null,
{
loadSurveyHeadline: async (forceRefresh: boolean = false) => {
if (props.id === NEW_SURVEY.id || !values.survey?.start_date) {
return null
}
const result = await api.surveys.getSummaryHeadline(props.id, forceRefresh)
if (result) {
actions.setSurveyValue('headline_summary', result.headline)
actions.setSurveyValue('headline_response_count', result.responses_sampled)
}
return result
},
},
],
survey: {
loadSurvey: async () => {
if (props.id && props.id !== 'new') {
try {
const survey = await api.surveys.get(props.id)
// patch surveys with a potentially null appearance...
// pending root cause on _how_ these get to be null
if (!survey.appearance) {
survey.appearance = defaultSurveyAppearance
}
const currentFilters = values.answerFilters
posthog.capture('survey viewed', {
name: survey.name,
id: survey.id,
created_at: survey.created_at,
start_date: survey.start_date,
end_date: survey.end_date,
})
// Initialize answer filters for all questions - first for index-based, then for id-based
actions.setAnswerFilters(
survey.questions.map((question) => {
const { indexBasedKey, idBasedKey } = getResponseFieldWithId(0, question.id)
const currentFilterForQuestion = currentFilters.find(
(filter) => filter.key === idBasedKey
)
return {
key: idBasedKey || indexBasedKey,
operator:
currentFilterForQuestion?.operator ?? DEFAULT_OPERATORS[question.type].value,
type: PropertyFilterType.Event as const,
value: currentFilterForQuestion?.value ?? [],
}
}),
false
)
if (!values.dateRange) {
actions.setDateRange(
{
date_from: getSurveyStartDateForQuery(survey),
date_to: getSurveyEndDateForQuery(survey),
},
false
)
}
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_VIEWED,
metadata: {
survey_id: survey.id,
survey_status: getSurveyStatus(survey),
},
})
return survey
} catch (error: any) {
if (error.status === 404) {
actions.setSurveyMissing()
return { ...NEW_SURVEY }
}
throw error
}
}
if (props.id === 'new' && router.values.hashParams.fromTemplate) {
const templatedSurvey = { ...values.survey }
templatedSurvey.appearance = {
...defaultSurveyAppearance,
...teamLogic.values.currentTeam?.survey_config?.appearance,
}
return templatedSurvey
}
const newSurvey = { ...NEW_SURVEY }
newSurvey.appearance = {
...defaultSurveyAppearance,
...teamLogic.values.currentTeam?.survey_config?.appearance,
}
return newSurvey
},
createSurvey: async (surveyPayload: Partial<Survey>) => {
const response = await api.surveys.create(surveyPayload)
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_CREATED,
metadata: {
survey_id: response.id,
source: SURVEY_CREATED_SOURCE.SURVEY_FORM,
},
})
return response
},
updateSurvey: async (surveyPayload: Partial<Survey> & { intentContext?: ProductIntentContext }) => {
const response = await api.surveys.update(props.id, surveyPayload)
if (surveyPayload.intentContext) {
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: surveyPayload.intentContext,
metadata: {
survey_id: values.survey.id,
},
})
}
refreshTreeItem('survey', props.id)
return response
},
launchSurvey: async () => {
const startDate = dayjs()
const response = await api.surveys.update(props.id, { start_date: startDate.toISOString() })
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_LAUNCHED,
metadata: {
survey_id: response.id,
},
})
return response
},
stopSurvey: async () => {
const response = await api.surveys.update(props.id, { end_date: dayjs().toISOString() })
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_COMPLETED,
metadata: {
survey_id: response.id,
},
})
return response
},
resumeSurvey: async () => {
const response = await api.surveys.update(props.id, { end_date: null })
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_RESUMED,
metadata: {
survey_id: response.id,
},
})
return response
},
},
surveyBaseStats: {
loadSurveyBaseStats: async (): Promise<SurveyBaseStatsResult> => {
if (props.id === NEW_SURVEY.id || !values.survey?.start_date) {
return null
}
const responseStats = buildSurveyResponseStatsQuery(values.survey as Survey, {
timestampFilter: values.timestampFilter,
answerFilters: values.answerFilters,
archivedResponsesFilter: values.archivedResponsesFilter,
})
const query = `
-- QUERYING BASE STATS
SELECT event as event_name, count() as total_count,
count(DISTINCT person_id) as unique_persons,
min(timestamp) as first_seen, max(timestamp) as last_seen,
tuple(0, 0, 0) as outcome_counts
FROM events
WHERE event IN ('${SurveyEventName.SHOWN}', '${SurveyEventName.DISMISSED}')
AND properties.\`${SurveyEventProperties.SURVEY_ID}\` = '${props.id}'
${values.timestampFilter}
${values.archivedResponsesFilter}
AND {filters}
AND (event != '${SurveyEventName.DISMISSED}' OR ${buildSurveyOptionalBooleanPropertyFilter(SurveyEventProperties.SURVEY_PARTIALLY_COMPLETED, 'true')})
GROUP BY event
UNION ALL
${responseStats}` as HogQLQueryString
const response = await api.queryHogQL(query, SURVEY_QUERY_TAGS.baseStats, {
queryParams: {
filters: {
properties: values.propertyFilters,
},
},
})
const results = (response.results as SurveyBaseStatsResult | undefined) ?? null
actions.setBaseStatsResults(results)
actions.loadConsolidatedSurveyResults()
return results
},
},
surveyDismissedAndSentCount: {
loadSurveyDismissedAndSentCount: async (): Promise<DismissedAndSentCountResult> => {
if (props.id === NEW_SURVEY.id || !values.survey?.start_date) {
return null
}
const respondents = buildSurveyRespondentQuery(values.survey as Survey, {
timestampFilter: values.timestampFilter,
answerFilters: values.answerFilters,
archivedResponsesFilter: values.archivedResponsesFilter,
})
const query = `
-- QUERYING DISMISSED AND SENT COUNT
SELECT count(DISTINCT person_id)
FROM events
WHERE event = '${SurveyEventName.DISMISSED}'
AND properties.\`${SurveyEventProperties.SURVEY_ID}\` = '${props.id}'
${values.timestampFilter}
${values.archivedResponsesFilter}
AND ${buildSurveyOptionalBooleanPropertyFilter(SurveyEventProperties.SURVEY_PARTIALLY_COMPLETED, 'true')}
AND {filters}
AND person_id IN (${respondents})` as HogQLQueryString
const response = await api.queryHogQL(query, SURVEY_QUERY_TAGS.dismissedAndSent, {
queryParams: {
filters: {
properties: values.propertyFilters, // Property filters applied in WHERE
},
},
})
const count = response.results?.[0]?.[0] ?? 0
actions.setDismissedAndSentCount(count)
return count as DismissedAndSentCountResult
},
},
consolidatedSurveyResults: {
loadConsolidatedSurveyResults: async (): Promise<ConsolidatedSurveyResults> => {
if (props.id === NEW_SURVEY.id || !values.survey?.start_date) {
return { responsesByQuestion: {} }
}
const survey = values.survey as Survey
const queryFilters: SurveyQueryFilters = {
timestampFilter: values.timestampFilter,
answerFilters: values.answerFilters,
archivedResponsesFilter: values.archivedResponsesFilter,
}
const queryParams = {
queryParams: { filters: { properties: values.propertyFilters } },
}
const aggregateQuery = buildAggregateQuery(survey, queryFilters)
const openEndedResult = buildOpenEndedQuery(survey, queryFilters)
const startMs = performance.now()
let aggregateDuration = 0
let openEndedDuration = 0
const [aggregateResponse, openEndedResponse] = await Promise.all([
aggregateQuery
? api
.queryHogQL(
aggregateQuery as HogQLQueryString,
SURVEY_QUERY_TAGS.aggregateResults,
queryParams
)
.then((r) => {
aggregateDuration = performance.now() - startMs
return r
})
: Promise.resolve({ results: null }),
openEndedResult
? api
.queryHogQL(
openEndedResult.query as HogQLQueryString,
SURVEY_QUERY_TAGS.openEndedResults,
queryParams
)
.then((r) => {
openEndedDuration = performance.now() - startMs
return r
})
: Promise.resolve({ results: null }),
])
const endMs = performance.now()
posthog.capture('survey consolidated results query completed', {
name: survey.name,
id: survey.id,
duration: endMs - startMs,
aggregate_duration: aggregateDuration,
open_ended_duration: openEndedDuration,
})
const aggregate = processResultsForSurveyQuestions(survey.questions, aggregateResponse.results)
const openEnded = openEndedResult
? processOpenEndedResults(survey.questions, openEndedResult.columnMap, openEndedResponse.results)
: {}
return { responsesByQuestion: mergeResponsesByQuestion(aggregate, openEnded) }
},
},
archivedResponseUuids: [
new Set<string>(),
{
loadArchivedResponseUuids: async (): Promise<Set<string>> => {
if (props.id === NEW_SURVEY.id) {
return new Set()
}
const uuids = await api.surveys.getArchivedResponseUuids(props.id)
return new Set(uuids)
},
},
],
surveyNotifications: [
[] as HogFunctionType[],
{
loadSurveyNotifications: async (): Promise<HogFunctionType[]> => {
if (props.id === NEW_SURVEY.id) {
return []
}
const response = await api.hogFunctions.list({
filter_groups: [
{
events: [
{
id: SurveyEventName.SENT,
type: 'events',
properties: [
{
key: SurveyEventProperties.SURVEY_ID,
type: PropertyFilterType.Event,
value: props.id,
operator: PropertyOperator.Exact,
},
],
},
],
},
],
types: ['destination'],
full: true,
})
return response.results
},
},
],
reusableSurveyNotifications: [
[] as HogFunctionType[],
{
loadReusableSurveyNotifications: async (): Promise<HogFunctionType[]> => {
if (props.id === NEW_SURVEY.id) {
return []
}
const response = await api.hogFunctions.list({
filter_groups: [
{
events: [{ id: SurveyEventName.SENT, type: 'events' }],
},
],
types: ['destination'],
limit: SURVEY_NOTIFICATION_LIST_LIMIT,
full: true,
})
return response.results.filter((notification) => {
if (notification.deleted) {
return false
}
const surveyIds = getSurveyIdsFromNotificationFilters(notification.filters)
return !surveyIds.has(props.id)
})
},
},
],
})),
listeners(({ actions, values, cache, props }) => {
const maybeCompleteResultsRequery = (): void => {
if (cache.resultsRequeryCompletionTimer) {
clearTimeout(cache.resultsRequeryCompletionTimer)
}
cache.resultsRequeryCompletionTimer = setTimeout(() => {
cache.resultsRequeryCompletionTimer = null
const mountedLogic = surveyLogic.findMounted(props)
if (!mountedLogic?.values.resultsRequeryInProgress || mountedLogic.values.isAnyResultsLoading) {
return
}
mountedLogic.actions.markResultsRequeryCompleted()
}, 0)
}
const reloadAllSurveyResults = (): void => {
if (cache.reloadDebounceTimer) {
clearTimeout(cache.reloadDebounceTimer)
}
if (!values.resultsRequeryInProgress) {
actions.startResultsRequery()
}
cache.reloadDebounceTimer = setTimeout(() => {
actions.loadSurveyBaseStats()
actions.loadSurveyDismissedAndSentCount()
}, 300)
}
return {
addResponseColumn: ({ column }) => {
posthog.capture('survey response column added', { column_type: column.type })
},
removeResponseColumn: ({ column }) => {
posthog.capture('survey response column removed', { column_type: column.type })
},
createSurveySuccess: ({ survey }) => {
lemonToast.success(<>Survey {survey.name} created</>)
actions.loadSurveys()
router.actions.replace(urls.survey(survey.id))
reportSurveyCreated(survey)
globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.CreateSurvey)
const surveyType = survey.type ? `${survey.type} ` : ''
tryShowMCPHint('surveys.create', {
derivedPrompt: survey.name ? `Create a ${surveyType}survey called ${survey.name}` : undefined,
})
},
updateSurveyFailure: ({ errorObject }) => {
// The global error handler skips approval 409s, so without this a targeting change that
// waits for approval looks like a save that did nothing.
if (isApprovalRequiredError(errorObject)) {
showApprovalRequiredToast(errorObject.data.change_request_id, undefined, errorObject.data.code)
// The backend saved none of the edits, so drop the form state that shows them as saved.
actions.loadSurvey()
}
},
updateSurveySuccess: ({ survey }) => {
lemonToast.success(<>Survey {survey.name} updated</>)
actions.editingSurvey(false)
reportSurveyEdited(survey)
actions.loadSurveys()
},
launchSurveySuccess: ({ survey }) => {
lemonToast.success(<>Survey {survey.name} launched</>)
globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.LaunchSurvey)
actions.loadSurveys()
},
stopSurveySuccess: () => {
actions.loadSurveys()
},
resumeSurveySuccess: () => {
actions.loadSurveys()
},
archiveSurvey: () => {
const updates: Partial<Survey> & { intentContext?: ProductIntentContext } = {
archived: true,
intentContext: ProductIntentContext.SURVEY_ARCHIVED,
}
if (values.isSurveyRunning) {
updates.end_date = dayjs().toISOString()
}
actions.updateSurvey(updates)
},
loadSurveySuccess: () => {
// Initialize dataCollectionType from survey data (using selector pattern for consistency)
actions.setDataCollectionType(values.derivedDataCollectionType)
if (values.survey.id !== NEW_SURVEY.id && values.survey.start_date) {
// Load archived UUIDs first — stats are triggered by loadArchivedResponseUuidsSuccess
// so that the archivedResponsesFilter is populated before stats queries run
actions.loadArchivedResponseUuids()
}
},
loadArchivedResponseUuidsSuccess: () => {
// Initial survey load fetches archived UUIDs before any results requery is active.
// Archive/unarchive flows update this state manually and trigger the results reload explicitly.
if (
values.survey.id !== NEW_SURVEY.id &&
values.survey.start_date &&
!values.resultsRequeryInProgress
) {
actions.loadSurveyBaseStats()
actions.loadSurveyDismissedAndSentCount()
}
},
loadConsolidatedSurveyResultsSuccess: async ({ consolidatedSurveyResults }) => {
const distinctIds = new Set<string>()
for (const data of Object.values(consolidatedSurveyResults.responsesByQuestion)) {
for (const r of data.data) {
const id = 'distinctId' in r ? r.distinctId : undefined
if (id) {
distinctIds.add(id)
}
}
}
if (distinctIds.size === 0) {
maybeCompleteResultsRequery()
return
}
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
maybeCompleteResultsRequery()
return
}
try {
const allIds = Array.from(distinctIds)
const BATCH_SIZE = 200
const personNames: Record<string, string> = {}
for (let i = 0; i < allIds.length; i += BATCH_SIZE) {
const batch = allIds.slice(i, i + BATCH_SIZE)
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. personsBatchByDistinctIdsCreate() from 'products/persons/frontend/generated/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
const response = await api.create(`api/projects/${teamId}/persons/batch_by_distinct_ids/`, {
distinct_ids: batch,
})
for (const [distinctId, person] of Object.entries(
response.results as Record<string, { name: string }>
)) {
if (person.name) {
personNames[distinctId] = person.name
}
}
}
if (Object.keys(personNames).length > 0) {
actions.setPersonNames(personNames)
}
} catch {
// Person enrichment is best-effort — don't block survey results
}
maybeCompleteResultsRequery()
},
loadConsolidatedSurveyResultsFailure: () => {
maybeCompleteResultsRequery()
},
loadSurveyBaseStatsSuccess: () => {
if (values.isSurveyHeadlineEnabled && values.dataProcessingAccepted) {
const currentCount = values.processedSurveyStats?.[SurveyEventName.SENT]?.total_count ?? 0
const cachedCount = values.survey.headline_response_count ?? 0
if (currentCount > 0) {
const needsGeneration = !values.survey.headline_summary
const isStale = currentCount > cachedCount + 5
if (needsGeneration || isStale) {
actions.loadSurveyHeadline(true)
}
}
}
maybeCompleteResultsRequery()
},
loadSurveyBaseStatsFailure: () => {
maybeCompleteResultsRequery()
},
loadSurveyDismissedAndSentCountSuccess: () => {
maybeCompleteResultsRequery()
},
loadSurveyDismissedAndSentCountFailure: () => {
maybeCompleteResultsRequery()
},
startResultsRequery: () => {
lemonToast.loading('Refreshing results...', {
toastId: LOADING_SURVEY_RESULTS_TOAST_ID,
autoClose: false,
})
},
markResultsRequeryCompleted: () => {
lemonToast.dismiss(LOADING_SURVEY_RESULTS_TOAST_ID)
},
resetSurveyResponseLimits: () => {
actions.setSurveyValue('responses_limit', null)
},
resetSurveyAdaptiveSampling: () => {
actions.setSurveyValues({
response_sampling_interval: null,
response_sampling_interval_type: null,
response_sampling_limit: null,
response_sampling_start_date: null,
response_sampling_daily_limits: null,
})
},
generateTranslationDrafts: async ({ language, overwrite }) => {
if (values.survey.id === NEW_SURVEY.id) {
lemonToast.error('Save the survey before generating translations')
return
}
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
lemonToast.error('Select a project before generating translations')
return
}
actions.setGeneratingTranslationDrafts(true)
try {
const result = await surveysGenerateTranslationsCreate(String(teamId), values.survey.id, {
target_language: language,
overwrite,
survey: getSurveyTranslationDraftPayload(values.survey),
})
const translations = { ...values.survey.translations }
for (const [translationLanguage, translationPatch] of Object.entries(result.translations)) {
translations[translationLanguage] = {
...translations[translationLanguage],
...translationPatch,
}
}
const patchesById = new Map(result.questions.map((question) => [question.id, question]))
const questions = values.survey.questions.map((question, index) => {
const patch = patchesById.get(getTranslationDraftQuestionId(question, index))
if (!patch) {
return question
}
const questionTranslations = { ...question.translations }
for (const [translationLanguage, translationPatch] of Object.entries(patch.translations)) {
questionTranslations[translationLanguage] = {
...questionTranslations[translationLanguage],
...translationPatch,
}
}
return {
...question,
translations: questionTranslations,
}
})
actions.setSurveyValues({ translations, questions })
actions.setAiGeneratedTranslationFields(result.generated_field_paths)
lemonToast.success('Generated translation drafts')
} catch (error) {
lemonToast.error('Failed to generate translations')
posthog.captureException(error, {
action: 'generate-survey-translations',
survey: values.survey.id,
})
} finally {
actions.setGeneratingTranslationDrafts(false)
}
},
resetTargeting: () => {
actions.setSurveyValue('linked_flag_id', NEW_SURVEY.linked_flag_id)
actions.setSurveyValue('targeting_flag_filters', NEW_SURVEY.targeting_flag_filters)
actions.setSurveyValue('linked_flag', NEW_SURVEY.linked_flag)
actions.setSurveyValue('targeting_flag', NEW_SURVEY.targeting_flag)
actions.setSurveyValue('conditions', NEW_SURVEY.conditions)
actions.setSurveyValue('remove_targeting_flag', true)
actions.setSurveyValue('responses_limit', NEW_SURVEY.responses_limit)
actions.setSurveyValues({
iteration_count: NEW_SURVEY.iteration_count,
iteration_frequency_days: NEW_SURVEY.iteration_frequency_days,
})
actions.setFlagPropertyErrors(null)
},
submitSurveyFailure: async () => {
// When errors occur, scroll to the error, but wait for errors to be set in the DOM first
if (hasFormErrors(values.flagPropertyErrors) || values.urlMatchTypeValidationError) {
actions.setSelectedSection(SurveyEditSection.DisplayConditions)
} else if (
values.surveyErrors.questions != null &&
!values.surveyErrors.questions.every((q) => q.question === false)
) {
actions.setSelectedSection(SurveyEditSection.Steps)
const page = values.surveyErrors.questions.findIndex((q) => q.question !== false)
if (page >= 0) {
actions.setSelectedPageIndex(page)
}
} else if (hasFormErrors(values.surveyErrors?.appearance)) {
actions.setSelectedSection(SurveyEditSection.Customization)
} else {
actions.setSelectedSection(SurveyEditSection.Steps)
}
setTimeout(
() =>
document
.querySelector(`.Field--error`)
?.scrollIntoView({ block: 'center', behavior: 'smooth' }),
5
)
},
setPropertyFilters: ({ reloadResults }) => {
if (reloadResults) {
reloadAllSurveyResults()
}
},
setAnswerFilters: ({ reloadResults }) => {
if (reloadResults) {
reloadAllSurveyResults()
}
},
setDateRange: ({ reloadResults }) => {
if (reloadResults) {
reloadAllSurveyResults()
}
},
clearFilters: () => {
const survey = values.survey as Survey
actions.setAnswerFilters(values.defaultAnswerFilters, false)
actions.setPropertyFilters([], false)
actions.setDateRange(
{
date_from: getSurveyStartDateForQuery(survey),
date_to: getSurveyEndDateForQuery(survey),
},
false
)
reloadAllSurveyResults()
},
setShowArchivedResponses: () => {
reloadAllSurveyResults()
},
archiveResponse: async ({ responseUuid }) => {
try {
actions.startResultsRequery()
await api.surveys.archiveResponse(values.survey.id, responseUuid)
const updatedUuids = new Set<string>(values.archivedResponseUuids)
updatedUuids.add(responseUuid)
actions.loadArchivedResponseUuidsSuccess(updatedUuids)
actions.loadSurveyBaseStats()
actions.loadSurveyDismissedAndSentCount()
lemonToast.success('Response archived')
} catch (error) {
actions.markResultsRequeryCompleted()
lemonToast.error('Failed to archive response')
posthog.captureException(error, {
action: 'archive-survey-response',
survey: values.survey.id,
response: responseUuid,
})
actions.loadArchivedResponseUuids()
}
},
unarchiveResponse: async ({ responseUuid }) => {
try {
actions.startResultsRequery()
await api.surveys.unarchiveResponse(values.survey.id, responseUuid)
const updatedUuids = new Set<string>(values.archivedResponseUuids)
updatedUuids.delete(responseUuid)
actions.loadArchivedResponseUuidsSuccess(updatedUuids)
actions.loadSurveyBaseStats()
actions.loadSurveyDismissedAndSentCount()
lemonToast.success('Response unarchived')
} catch (error) {
actions.markResultsRequeryCompleted()
lemonToast.error('Failed to unarchive response')
posthog.captureException(error, {
action: 'unarchive-survey-response',
survey: values.survey.id,
response: responseUuid,
})
actions.loadArchivedResponseUuids()
}
},
toggleSurveyNotificationEnabled: async ({ notificationId, enabled }) => {
const updatedNotifications = values.surveyNotifications.map((notification) =>
notification.id === notificationId ? { ...notification, enabled } : notification
)
actions.loadSurveyNotificationsSuccess(updatedNotifications)
try {
await api.hogFunctions.update(notificationId, { enabled })
} catch (error) {
lemonToast.error('Failed to update notification')
actions.loadSurveyNotifications()
posthog.captureException(error, {
action: 'toggle-survey-notification',
survey: values.survey.id,
notification: notificationId,
})
}
},
deleteSurveyNotification: async ({ notification }) => {
const previous = values.surveyNotifications
// Optimistically remove the row; restore on undo or on a swallowed API error.
actions.loadSurveyNotificationsSuccess(previous.filter((n) => n.id !== notification.id))
let callbackFired = false
await deleteWithUndo({
endpoint: `projects/${projectLogic.values.currentProjectId}/hog_functions`,
object: { id: notification.id, name: notification.name },
callback: (undo) => {
callbackFired = true
if (undo) {
actions.loadSurveyNotifications()
}
},
})
if (!callbackFired) {
// deleteWithUndo swallows API errors and only fires the callback on success.
actions.loadSurveyNotificationsSuccess(previous)
}
},
}
}),
events(({ cache }) => ({
beforeUnmount: () => {
if (cache.reloadDebounceTimer) {
clearTimeout(cache.reloadDebounceTimer)
cache.reloadDebounceTimer = null
}
if (cache.resultsRequeryCompletionTimer) {
clearTimeout(cache.resultsRequeryCompletionTimer)
cache.resultsRequeryCompletionTimer = null
}
},
})),
reducers({
activeTab: [
SurveyTab.SUMMARY as SurveyTab,
{
setActiveTab: (_, { tab }) => tab,
},
],
personNames: [
{} as Record<string, string>,
{
setPersonNames: (state, { personNames }) => ({ ...state, ...personNames }),
},
],
expandedResponseUuids: [
new Set<string>(),
{
setResponseExpanded: (state, { uuid, expanded }) => {
if (expanded === state.has(uuid)) {
return state
}
const next = new Set(state)
if (expanded) {
next.add(uuid)
} else {
next.delete(uuid)
}
return next
},
toggleResponseExpansion: (state, { uuid }) => {
const next = new Set(state)
if (next.has(uuid)) {
next.delete(uuid)
} else {
next.add(uuid)
}
return next
},
},
],
editingLanguage: [
null as string | null,
{
setEditingLanguage: (_, { language }) => language,
resetSurvey: () => null,
loadSurveySuccess: () => null,
},
],
aiGeneratedTranslationFields: [
[] as string[],
{
setAiGeneratedTranslationFields: (_, { paths }) => paths,
clearAiGeneratedTranslationField: (state, { path }) => state.filter((fieldPath) => fieldPath !== path),
loadSurveySuccess: () => [],
},
],
generatingTranslationDrafts: [
false,
{
setGeneratingTranslationDrafts: (_, { generating }) => generating,
},
],
resultsFiltersExpanded: [false, { setResultsFiltersExpanded: (_, { expanded }) => expanded }],
showArchivedResponses: [
false,
{ persist: true },
{
setShowArchivedResponses: (_, { show }) => show,
clearFilters: () => false,
},
],
filterSurveyStatsByDistinctId: [
true,
{ persist: true },
{
setFilterSurveyStatsByDistinctId: (_, { filterByDistinctId }) => filterByDistinctId,
},
],
responseColumns: [
[] as SurveyResponseColumn[],
{ persist: true },
{
addResponseColumn: (state, { column }) =>
state.some((existing) => surveyResponseColumnId(existing) === surveyResponseColumnId(column))
? state
: [...state, column],
removeResponseColumn: (state, { column }) =>
state.filter((existing) => surveyResponseColumnId(existing) !== surveyResponseColumnId(column)),
},
],
resultsRequeryInProgress: [
false,
{
startResultsRequery: () => true,
markResultsRequeryCompleted: () => false,
loadSurveySuccess: () => false,
resetSurvey: () => false,
},
],
isEditingSurvey: [
false,
{
editingSurvey: (_, { editing }) => editing,
},
],
surveyMissing: [
false,
{
setSurveyMissing: () => true,
},
],
dataCollectionType: [
'until_stopped' as DataCollectionType,
{
setDataCollectionType: (_, { dataCollectionType }) => dataCollectionType,
},
],
propertyFilters: [
[] as AnyPropertyFilter[],
{ persist: true },
{
setPropertyFilters: (_, { propertyFilters }) => propertyFilters,
},
],
survey: [
{ ...NEW_SURVEY } as NewSurvey | Survey,
{
setDefaultForQuestionType: (state, { idx, type, surveyQuestion }) => {
const question =
defaultSurveyFieldValues[surveyQuestion.type].questions[0].question !== surveyQuestion.question
? surveyQuestion.question
: defaultSurveyFieldValues[type].questions[0].question
const description =
defaultSurveyFieldValues[surveyQuestion.type].questions[0].description !==
surveyQuestion.description
? surveyQuestion.description
: defaultSurveyFieldValues[type].questions[0].description
const thankYouMessageHeader =
defaultSurveyFieldValues[surveyQuestion.type].appearance.thankYouMessageHeader !==
state.appearance?.thankYouMessageHeader
? state.appearance?.thankYouMessageHeader
: defaultSurveyFieldValues[type].appearance.thankYouMessageHeader
const newQuestions = [...state.questions]
const q = {
...surveyQuestion,
}
if (q.type === SurveyQuestionType.MultipleChoice || q.type === SurveyQuestionType.SingleChoice) {
delete q.hasOpenChoice
}
// Clean up translations when question type changes
const cleanedTranslations = q.translations
? Object.entries(q.translations).reduce(
(acc, [lang, trans]) => {
const cleanedTrans = { ...trans }
// Remove fields that don't apply to the new type
if (
type !== SurveyQuestionType.SingleChoice &&
type !== SurveyQuestionType.MultipleChoice
) {
delete cleanedTrans.choices
}
if (type !== SurveyQuestionType.Link) {
delete cleanedTrans.link
}
if (type !== SurveyQuestionType.Rating) {
delete cleanedTrans.lowerBoundLabel
delete cleanedTrans.upperBoundLabel
}
acc[lang] = cleanedTrans
return acc
},
{} as Record<string, any>
)
: undefined
// Get the new question with default values for the new type
const newQuestionDefaults = defaultSurveyFieldValues[type].questions[0] as SurveyQuestionBase
const newChoices = (newQuestionDefaults as MultipleSurveyQuestion).choices || []
// Initialize choices for new single/multiple choice questions in translations
const choicesInitializedTranslations = cleanedTranslations
? Object.entries(cleanedTranslations).reduce(
(acc, [lang, trans]) => {
const cleanedTrans = { ...trans }
if (
(type === SurveyQuestionType.SingleChoice ||
type === SurveyQuestionType.MultipleChoice) &&
!cleanedTrans.choices
) {
cleanedTrans.choices = newChoices
}
acc[lang] = cleanedTrans
return acc
},
{} as Record<string, any>
)
: cleanedTranslations
const nextQuestion = {
...q,
...newQuestionDefaults,
question,
description,
translations: choicesInitializedTranslations,
}
newQuestions[idx] =
type === SurveyQuestionType.SingleChoice || type === SurveyQuestionType.MultipleChoice
? ({ ...nextQuestion, choices: newChoices } as SurveyQuestion)
: (nextQuestion as SurveyQuestion)
return {
...state,
questions: newQuestions,
appearance: {
...state.appearance,
...defaultSurveyFieldValues[type].appearance,
thankYouMessageHeader,
},
}
},
setQuestionBranchingType: (state, { questionIndex, type, specificQuestionIndex }) => {
const newQuestions = [...state.questions]
const question = newQuestions[questionIndex]
// Validate response-based branching is only used with compatible question types
if (
type === SurveyQuestionBranchingType.ResponseBased &&
!canQuestionHaveResponseBasedBranching(question)
) {
question.branching = undefined
lemonToast.error(
<>
Response-based branching is not supported for {question.type} questions. Removing
branching logic from this question.
</>
)
} else {
// Use centralized branching config creation
question.branching = createBranchingConfig(type, specificQuestionIndex)
}
newQuestions[questionIndex] = question
return {
...state,
questions: newQuestions,
}
},
setResponseBasedBranchingForQuestion: (
state,
{ questionIndex, responseValue, nextStep, specificQuestionIndex }
) => {
const newQuestions = [...state.questions]
const question = newQuestions[questionIndex]
// Use centralized validation for response-based branching compatibility
if (!canQuestionHaveResponseBasedBranching(question)) {
throw new Error(
`Survey question type must be ${SurveyQuestionType.Rating} or ${SurveyQuestionType.SingleChoice} for response-based branching`
)
}
if (question.branching?.type !== SurveyQuestionBranchingType.ResponseBased) {
throw new Error(
`Survey question branching type must be ${SurveyQuestionBranchingType.ResponseBased}`
)
}
if ('responseValues' in question.branching) {
if (nextStep === SurveyQuestionBranchingType.NextQuestion) {
// Remove the response mapping to default to next question
delete question.branching.responseValues[responseValue]
} else if (nextStep === SurveyQuestionBranchingType.End) {
// Map response to end survey
question.branching.responseValues[responseValue] = SurveyQuestionBranchingType.End
} else if (nextStep === SurveyQuestionBranchingType.SpecificQuestion) {
// Map response to specific question index
question.branching.responseValues[responseValue] = specificQuestionIndex
}
}
newQuestions[questionIndex] = question
return {
...state,
questions: newQuestions,
}
},
resetBranchingForQuestion: (state, { questionIndex }) => {
const newQuestions = [...state.questions]
const question = newQuestions[questionIndex]
delete question.branching
newQuestions[questionIndex] = question
return {
...state,
questions: newQuestions,
}
},
deleteBranchingLogic: (state) => {
const newQuestions = [...state.questions]
newQuestions.forEach((question) => {
delete question.branching
})
return {
...state,
questions: newQuestions,
}
},
setMultipleSurveyQuestion: (state, { questionIndex, question, type }) => {
const newQuestions = [...state.questions]
const newQuestion: MultipleSurveyQuestion = {
...question,
type,
}
newQuestions[questionIndex] = newQuestion
return {
...state,
questions: newQuestions,
}
},
moveQuestion: (state, { oldIndex, newIndex }) => {
if (oldIndex === newIndex) {
return state
}
const reordered = [...state.questions]
const [moved] = reordered.splice(oldIndex, 1)
reordered.splice(newIndex, 0, moved)
const indexMap = buildReorderIndexMap(state.questions.length, oldIndex, newIndex)
return {
...state,
questions: remapBranchingIndices(reordered, indexMap),
}
},
removeQuestion: (state, { questionIndex }) => {
const filtered = state.questions.filter((_, i) => i !== questionIndex)
const indexMap = buildDeleteIndexMap(state.questions.length, questionIndex)
return {
...state,
questions: remapBranchingIndices(filtered, indexMap),
}
},
},
],
selectedPageIndex: [
0 as number | null,
{
setSelectedPageIndex: (_, { idx }) => idx,
},
],
selectedSection: [
SurveyEditSection.Steps as SurveyEditSection | null,
{
setSelectedSection: (_, { section }) => section,
},
],
writingHTMLDescription: [
false,
{
setWritingHTMLDescription: (_, { writingHTML }) => writingHTML,
},
],
flagPropertyErrors: [
null as any,
{
setFlagPropertyErrors: (_, { errors }) => errors,
},
],
answerFilters: [
[] as EventPropertyFilter[],
{ persist: true },
{
setAnswerFilters: (_, { filters }) => filters,
},
],
dateRange: [
null as SurveyDateRange | null,
{
setDateRange: (_, { dateRange }) => dateRange,
},
],
interval: [
null as IntervalType | null,
{
setInterval: (_, { interval }) => interval,
},
],
compareFilter: [
{ compare: true } as CompareFilter,
{
setCompareFilter: (_, { compareFilter }) => compareFilter,
},
],
surveyBaseStatsInternal: [
null as SurveyBaseStatsResult,
{
setBaseStatsResults: (_, { results }) => results,
loadSurveySuccess: () => null,
resetSurvey: () => null,
},
],
surveyDismissedAndSentCountInternal: [
null as DismissedAndSentCountResult,
{
setDismissedAndSentCount: (_, { count }) => count,
loadSurveySuccess: () => null,
resetSurvey: () => null,
},
],
}),
selectors({
enrichedConsolidatedSurveyResults: [
(s) => [s.consolidatedSurveyResults, s.personNames],
(results: ConsolidatedSurveyResults, personNames: Record<string, string>): ConsolidatedSurveyResults => {
if (!results?.responsesByQuestion || Object.keys(personNames).length === 0) {
return results
}
const enriched: ResponsesByQuestion = {}
for (const [qid, data] of Object.entries(results.responsesByQuestion)) {
enriched[qid] = {
...data,
data: data.data.map((r) => {
const id = 'distinctId' in r ? r.distinctId : undefined
return id && personNames[id] ? { ...r, personDisplayName: personNames[id] } : r
}),
} as typeof data
}
return { responsesByQuestion: enriched }
},
],
timestampFilter: [
(s) => [s.survey, s.dateRange],
(survey: Survey, dateRange: SurveyDateRange): string => {
return buildSurveyTimestampFilter(survey, dateRange)
},
],
archivedResponsesFilter: [
(s) => [s.showArchivedResponses, s.archivedResponseUuids],
(showArchivedResponses: boolean, archivedUuids: Set<string>): string => {
if (showArchivedResponses || !archivedUuids || archivedUuids.size === 0) {
return ''
}
// UUIDs are pre-validated by Django's UUIDField when stored in SurveyResponseArchive
const uuidList = Array.from(archivedUuids)
.map((uuid) => `'${uuid}'`)
.join(', ')
return `AND uuid NOT IN (${uuidList})`
},
],
archivedResponsesPropertyFilter: [
(s) => [s.showArchivedResponses, s.archivedResponseUuids],
(
showArchivedResponses: boolean,
archivedUuids: Set<string>
): Array<{ type: PropertyFilterType.HogQL; key: string }> => {
if (showArchivedResponses || !archivedUuids || archivedUuids.size === 0) {
return []
}
// UUIDs are pre-validated by Django's UUIDField when stored in SurveyResponseArchive
const uuidList = Array.from(archivedUuids)
.map((uuid) => `'${uuid}'`)
.join(', ')
return [
{
type: PropertyFilterType.HogQL,
key: `uuid NOT IN (${uuidList})`,
},
]
},
],
isAdaptiveLimitFFEnabled: [
(s) => [s.enabledFlags],
(enabledFlags: FeatureFlagsSet): boolean => {
return !!enabledFlags[FEATURE_FLAGS.SURVEYS_ADAPTIVE_LIMITS]
},
],
isSurveyHeadlineEnabled: [
(s) => [s.enabledFlags],
(enabledFlags: FeatureFlagsSet): boolean => {
return !!enabledFlags[FEATURE_FLAGS.SURVEY_HEADLINE_SUMMARY]
},
],
isAnyResultsLoading: [
(s) => [
s.archivedResponseUuidsLoading,
s.surveyBaseStatsLoading,
s.surveyDismissedAndSentCountLoading,
s.consolidatedSurveyResultsLoading,
],
(
archivedResponseUuidsLoading: boolean,
surveyBaseStatsLoading: boolean,
surveyDismissedAndSentCountLoading: boolean,
consolidatedSurveyResultsLoading: boolean
) => {
return (
archivedResponseUuidsLoading ||
consolidatedSurveyResultsLoading ||
surveyBaseStatsLoading ||
surveyDismissedAndSentCountLoading
)
},
],
defaultAnswerFilters: [
(s) => [s.survey],
(survey: Survey): EventPropertyFilter[] => {
return survey.questions.map((question) => {
const { indexBasedKey, idBasedKey } = getResponseFieldWithId(0, question.id)
return {
key: idBasedKey || indexBasedKey,
operator: DEFAULT_OPERATORS[question.type].value,
type: PropertyFilterType.Event as const,
value: [],
}
})
},
],
activeAnswerFiltersCount: [
(s) => [s.answerFilters],
(answerFilters: EventPropertyFilter[]): number =>
answerFilters.filter((filter) => {
if (filter.value === undefined || filter.value === null || filter.value === '') {
return false
}
return Array.isArray(filter.value) ? filter.value.length > 0 : true
}).length,
],
hasActiveAnswerFilters: [
(s) => [s.activeAnswerFiltersCount],
(activeAnswerFiltersCount: number): boolean => activeAnswerFiltersCount > 0,
],
activeResultsFilterCount: [
(s) => [s.activeAnswerFiltersCount, s.propertyFilters, s.showArchivedResponses],
(
activeAnswerFiltersCount: number,
propertyFilters: AnyPropertyFilter[],
showArchivedResponses: boolean
): number => activeAnswerFiltersCount + propertyFilters.length + Number(showArchivedResponses),
],
hasActiveDateRange: [
(s) => [s.dateRange, s.survey],
(dateRange: SurveyDateRange | null, survey: Survey): boolean => {
const surveyStartDate = getSurveyStartDateForQuery(survey)
const surveyEndDate = getSurveyEndDateForQuery(survey)
return !!dateRange && (dateRange.date_from !== surveyStartDate || dateRange.date_to !== surveyEndDate)
},
],
hasActiveFilters: [
(s) => [s.hasActiveAnswerFilters, s.propertyFilters, s.hasActiveDateRange],
(
hasActiveAnswerFilters: boolean,
propertyFilters: AnyPropertyFilter[],
hasActiveDateRange: boolean
): boolean => {
return hasActiveAnswerFilters || propertyFilters.length > 0 || hasActiveDateRange
},
],
isSurveyRunning: [
(s) => [s.survey],
(survey: Survey): boolean => {
return isSurveyRunning(survey)
},
],
surveyUsesLimit: [
(s) => [s.survey],
(survey: Survey): boolean => {
return !!(survey.responses_limit && survey.responses_limit > 0)
},
],
surveyUsesAdaptiveLimit: [
(s) => [s.survey],
(survey: Survey): boolean => {
return !!(
survey.response_sampling_interval &&
survey.response_sampling_interval > 0 &&
survey.response_sampling_interval_type !== '' &&
survey.response_sampling_limit &&
survey.response_sampling_limit > 0
)
},
],
derivedDataCollectionType: [
(s) => [s.surveyUsesAdaptiveLimit, s.surveyUsesLimit, s.isAdaptiveLimitFFEnabled],
(
surveyUsesAdaptiveLimit: boolean,
surveyUsesLimit: boolean,
isAdaptiveLimitFFEnabled: boolean
): DataCollectionType => {
if (isAdaptiveLimitFFEnabled && surveyUsesAdaptiveLimit) {
return 'until_adaptive_limit'
} else if (surveyUsesLimit) {
return 'until_limit'
}
return 'until_stopped'
},
],
surveyShufflingQuestionsAvailable: [
(s) => [s.survey],
(survey: Survey): boolean => {
return survey.questions.length > 1
},
],
showSurveyRepeatSchedule: [(s) => [s.survey], (survey: Survey) => survey.schedule === SurveySchedule.Recurring],
descriptionContentType: [
(s) => [s.survey],
(survey: Survey) => (questionIndex: number) => {
return survey.questions[questionIndex].descriptionContentType
},
],
surveyRepeatedActivationAvailable: [
(s) => [s.survey],
(survey: Survey): boolean =>
survey.conditions?.events?.values != undefined && survey.conditions?.events?.values?.length > 0,
],
hasTargetingSet: [
(s) => [s.survey],
(survey: Survey): boolean => {
const hasLinkedFlag =
!!survey.linked_flag_id || (survey.linked_flag && Object.keys(survey.linked_flag).length > 0)
const hasTargetingFlag =
(survey.targeting_flag && Object.keys(survey.targeting_flag).length > 0) ||
(survey.targeting_flag_filters && Object.keys(survey.targeting_flag_filters).length > 0)
const hasOtherConditions = survey.conditions && Object.keys(survey.conditions).length > 0
return !!hasLinkedFlag || !!hasTargetingFlag || !!hasOtherConditions
},
],
breadcrumbs: [
(s) => [s.survey],
(survey: Survey): Breadcrumb[] => [
{
key: Scene.Surveys,
name: 'Surveys',
path: urls.surveys(),
iconType: 'survey',
},
{
key: [Scene.Survey, survey?.id || 'new'],
name: survey.name,
iconType: 'survey',
},
],
],
[SIDE_PANEL_CONTEXT_KEY]: [
(s) => [s.survey],
(survey: Survey): SidePanelSceneContext | null => {
return survey?.id && survey.id !== 'new'
? {
activity_scope: ActivityScope.SURVEY,
activity_item_id: `${survey.id}`,
access_control_resource: 'survey',
access_control_resource_id: `${survey.id}`,
}
: null
},
],
projectTreeRef: [
() => [(_, props: SurveyLogicProps) => props.id],
(id: string): ProjectTreeRef => {
return { type: 'survey', ref: id === 'new' ? null : String(id) }
},
],
answerFilterHogQLExpression: [
(s) => [s.survey, s.answerFilters],
(survey: Survey, answerFilters: EventPropertyFilter[]): string => {
return createAnswerFilterHogQLExpression(answerFilters, survey)
},
],
dataTableQuery: [
(s) => [
s.survey,
s.propertyFilters,
s.answerFilters,
s.timestampFilter,
s.archivedResponsesFilter,
s.responseColumns,
],
(
survey: Survey,
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
timestampFilter: string,
archivedResponsesFilter: string,
responseColumns: SurveyResponseColumn[]
): DataTableNode | null => {
if (survey.id === 'new') {
return null
}
return {
kind: NodeKind.DataTableNode,
source: {
kind: NodeKind.HogQLQuery,
query: buildSurveyResponsesQuery(
survey,
{
answerFilters,
timestampFilter,
archivedResponsesFilter,
},
responseColumns
),
filters: { properties: propertyFilters },
},
hiddenColumns: ['response'],
showExport: true,
showReload: true,
showOpenEditorButton: false,
showRecordingColumn: false,
showEventFilter: false,
showPropertyFilter: false,
showTimings: false,
contextKey: `survey:${survey.id}`,
}
},
],
responsesExportQuery: [
(s) => [
s.survey,
s.propertyFilters,
s.answerFilters,
s.timestampFilter,
s.archivedResponsesFilter,
s.responseColumns,
],
(
survey: Survey,
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
timestampFilter: string,
archivedResponsesFilter: string,
responseColumns: SurveyResponseColumn[]
): DataTableNode | null => {
if (survey.id === 'new') {
return null
}
const query = buildSurveyResponsesExportQuery(
survey,
{
answerFilters,
timestampFilter,
archivedResponsesFilter,
},
responseColumns
)
return { ...query, source: { ...query.source, filters: { properties: propertyFilters } } }
},
],
targetingFlagFilters: [
(s) => [s.survey],
(survey: NewSurvey | Survey): FeatureFlagFilters | undefined => {
if (survey.targeting_flag_filters) {
return {
...survey.targeting_flag_filters,
groups: survey.targeting_flag_filters.groups,
multivariate: null,
payloads: {},
feature_enrollment: undefined,
}
}
return v1TargetingFlagFilters(survey)
},
],
urlMatchTypeValidationError: [
(s) => [s.survey],
(survey: NewSurvey | Survey): string | null => {
if (
survey.conditions?.url &&
[SurveyMatchType.Regex, SurveyMatchType.NotRegex].includes(
survey.conditions?.urlMatchType || SurveyMatchType.Exact
)
) {
try {
new RegExp(survey.conditions.url)
} catch (e: any) {
return e.message
}
}
return null
},
],
urlSearchParams: [
(s) => [s.propertyFilters, s.answerFilters, s.dateRange, s.survey],
(
propertyFilters: AnyPropertyFilter[],
answerFilters: EventPropertyFilter[],
dateRange: SurveyDateRange | null,
survey: Survey
) => {
const defaultDateFrom = getSurveyStartDateForQuery(survey)
const defaultDateTo = getSurveyEndDateForQuery(survey)
const nonEmptyAnswerFilters = answerFilters?.filter((filter) => {
const value = filter.value
if (Array.isArray(value)) {
return value.length > 0
}
return value !== null && value !== undefined && value !== ''
})
const isDefaultDateRange =
dateRange?.date_from === defaultDateFrom && dateRange?.date_to === defaultDateTo
return objectClean({
...router.values.searchParams,
propertyFilters: propertyFilters?.length > 0 ? JSON.stringify(propertyFilters) : undefined,
answerFilters:
nonEmptyAnswerFilters?.length > 0 ? JSON.stringify(nonEmptyAnswerFilters) : undefined,
date_from: !isDefaultDateRange && dateRange?.date_from ? dateRange.date_from : undefined,
date_to: !isDefaultDateRange && dateRange?.date_to ? dateRange.date_to : undefined,
})
},
],
deviceTypesMatchTypeValidationError: [
(s) => [s.survey],
(survey: Survey): string | null => {
if (
survey.conditions?.deviceTypes &&
[SurveyMatchType.Regex, SurveyMatchType.NotRegex].includes(
survey.conditions?.deviceTypesMatchType || SurveyMatchType.Exact
)
) {
try {
new RegExp(survey.conditions.deviceTypes?.at(0) || '')
} catch (e: any) {
return e.message
}
}
return null
},
],
getBranchingDropdownValue: [
(s) => [s.survey],
(survey: NewSurvey | Survey) => (questionIndex: number, question: SurveyQuestion) => {
if (question.branching?.type) {
const { type } = question.branching
if (type === SurveyQuestionBranchingType.SpecificQuestion) {
const nextQuestionIndex = question.branching.index
return branchingConfigToDropdownValue(type, nextQuestionIndex)
}
return type
}
// No branching specified, default to Next question / Confirmation message
return getDefaultBranchingType(questionIndex, survey.questions.length)
},
],
getResponseBasedBranchingDropdownValue: [
(s) => [s.survey],
(survey: NewSurvey | Survey) =>
(questionIndex: number, question: RatingSurveyQuestion | MultipleSurveyQuestion, response) => {
if (!question.branching || !('responseValues' in question.branching)) {
return SurveyQuestionBranchingType.NextQuestion
}
// If a value is mapped onto an integer, we're redirecting to a specific question
if (Number.isInteger(question.branching.responseValues[response])) {
const nextQuestionIndex = question.branching.responseValues[response]
return `${SurveyQuestionBranchingType.SpecificQuestion}:${nextQuestionIndex}`
}
// If any other value is present (practically only Confirmation message), return that value
if (question.branching?.responseValues?.[response]) {
return question.branching.responseValues[response]
}
// No branching specified, default to Next question / Confirmation message
if (questionIndex < survey.questions.length - 1) {
return SurveyQuestionBranchingType.NextQuestion
}
return SurveyQuestionBranchingType.End
},
],
hasCycle: [
(s) => [s.survey],
(survey: NewSurvey | Survey) => {
const graph = new Map()
survey.questions.forEach((question, fromIndex: number) => {
if (!graph.has(fromIndex)) {
graph.set(fromIndex, new Set())
}
if (question.branching?.type === SurveyQuestionBranchingType.End) {
return
} else if (
question.branching?.type === SurveyQuestionBranchingType.SpecificQuestion &&
Number.isInteger(question.branching.index)
) {
const toIndex = question.branching.index
graph.get(fromIndex).add(toIndex)
return
} else if (
question.branching?.type === SurveyQuestionBranchingType.ResponseBased &&
isObject(question.branching?.responseValues)
) {
// The responses the SDK can select; a key outside this list routes nobody.
let responses: (string | number)[] = []
if (question.type === SurveyQuestionType.SingleChoice) {
responses = question.choices.map((_, choiceIndex) => choiceIndex)
} else if (isRatingSurveyQuestion(question)) {
// The SDK keys rating responses by bucket. An unknown scale keeps every key.
if (question.scale === 2) {
responses = ['positive', 'negative']
} else if (question.scale === 10) {
responses = ['detractors', 'passives', 'promoters']
} else if ([3, 5, 7].includes(question.scale)) {
responses = ['negative', 'neutral', 'positive']
}
}
const { responseValues } = question.branching
const destinations =
responses.length > 0
? responses.map((response) => responseValues[String(response)])
: Object.values(responseValues)
for (const toIndex of destinations) {
if (Number.isInteger(toIndex)) {
graph.get(fromIndex).add(toIndex)
}
}
// The SDK falls through to the next question only when the selected response
// has no destination, so a question that routes every response never gets
// there. An optional question is the exception, because a skip routes nowhere.
if (
!question.optional &&
responses.length > 0 &&
responses.every((response) => {
const destination = responseValues[String(response)]
return Number.isInteger(destination) || destination === SurveyQuestionBranchingType.End
})
) {
return
}
}
// No branching - still need to connect the next question
if (fromIndex < survey.questions.length - 1) {
const toIndex = fromIndex + 1
graph.get(fromIndex).add(toIndex)
}
})
let cycleDetected = false
function dfs(node: number, seen: number[]): void {
if (cycleDetected) {
return
}
for (const neighbor of graph.get(node) || []) {
if (seen.includes(neighbor)) {
cycleDetected = true
return
}
dfs(neighbor, seen.concat(neighbor))
}
}
dfs(0, [0])
return cycleDetected
},
],
hasBranchingLogic: [
(s) => [s.survey],
(survey: NewSurvey | Survey) =>
survey.questions.some((question) => question.branching && Object.keys(question.branching).length > 0),
],
translationValidationErrors: [
(s) => [s.survey],
(survey: NewSurvey | Survey): TranslationValidationError[] => {
const errors: TranslationValidationError[] = []
const surveyLevelFieldChecks: TranslationFieldCheck<SurveyTranslationField>[] = [
{ key: 'name', defaultValue: survey.name },
{ key: 'thankYouMessageHeader', defaultValue: survey.appearance?.thankYouMessageHeader },
{
key: 'thankYouMessageDescription',
defaultValue: survey.appearance?.thankYouMessageDescription,
},
{
key: 'thankYouMessageCloseButtonText',
defaultValue: survey.appearance?.thankYouMessageCloseButtonText,
},
...(survey.appearance?.displayIntroScreen
? INTRO_SCREEN_TEXT_FIELDS.map((key) => ({
key,
defaultValue:
key === 'introScreenButtonText'
? survey.appearance?.[key] || INTRO_SCREEN_DEFAULT_BUTTON_TEXT
: survey.appearance?.[key],
}))
: []),
...(survey.appearance?.allowGoBack
? [
{
key: 'backButtonText' as const,
defaultValue: survey.appearance.backButtonText || BACK_BUTTON_DEFAULT_TEXT,
},
]
: []),
]
// Get all languages
const languages = new Set<string>()
if (survey.translations) {
Object.keys(survey.translations).forEach((lang) => languages.add(lang))
}
survey.questions.forEach((q) => {
if (q.translations) {
Object.keys(q.translations).forEach((lang) => languages.add(lang))
}
})
// First collect fields that have translations but empty defaults
const fieldsWithEmptyDefaults = new Set<string>()
languages.forEach((lang) => {
const trans = survey.translations?.[lang]
if (trans) {
surveyLevelFieldChecks.forEach(({ key, defaultValue }) => {
const value = trans[key]
// Only check if default is explicitly empty (not undefined)
const defaultIsExplicitlyEmpty =
defaultValue !== undefined &&
(typeof defaultValue !== 'string' || defaultValue.trim() === '')
const translationHasValue =
value !== undefined && typeof value === 'string' && value.trim() !== ''
// Track fields with translations but explicitly empty defaults
if (defaultIsExplicitlyEmpty && translationHasValue) {
fieldsWithEmptyDefaults.add(key)
}
})
}
})
// Add errors for empty default fields that have translations
if (fieldsWithEmptyDefaults.size > 0) {
surveyLevelFieldChecks.forEach(({ key, defaultValue }) => {
const defaultIsExplicitlyEmpty =
defaultValue !== undefined &&
(typeof defaultValue !== 'string' || defaultValue.trim() === '')
if (defaultIsExplicitlyEmpty && fieldsWithEmptyDefaults.has(key)) {
errors.push({
language: 'default',
questionIndex: -1,
field: key,
error: 'Cannot be empty (has translation)',
})
}
})
}
// Validate survey-level translations
languages.forEach((lang) => {
const trans = survey.translations?.[lang]
if (trans) {
surveyLevelFieldChecks.forEach(({ key, defaultValue }) => {
const value = trans[key]
const defaultHasValue =
defaultValue && typeof defaultValue === 'string' && defaultValue.trim() !== ''
if (value === TRANSLATION_NEEDED_PLACEHOLDER) {
errors.push({
language: lang,
questionIndex: -1,
field: key,
error: `Contains placeholder "${TRANSLATION_NEEDED_PLACEHOLDER}"`,
})
}
// Only validate empty translation strings if default has a value
if (
defaultHasValue &&
value !== undefined &&
typeof value === 'string' &&
value.trim() === ''
) {
errors.push({
language: lang,
questionIndex: -1,
field: key,
error: 'Cannot be empty',
})
}
})
}
})
// Validate question-level translations
survey.questions.forEach((question, qIndex) => {
// Validate default choices for empty strings
if (isChoiceSurveyQuestion(question) && question.choices && Array.isArray(question.choices)) {
question.choices.forEach((choice, choiceIndex) => {
if (typeof choice === 'string' && choice.trim() === '') {
errors.push({
language: 'default',
questionIndex: qIndex,
field: `choices[${choiceIndex}]`,
error: 'Cannot be empty',
})
}
})
}
if (!question.translations) {
return
}
const textFieldChecks: TranslationFieldCheck<QuestionTextTranslationField>[] = [
{ key: 'question', defaultValue: question.question },
{ key: 'description', defaultValue: question.description },
{ key: 'buttonText', defaultValue: question.buttonText },
...(isRatingSurveyQuestion(question)
? [
{ key: 'lowerBoundLabel' as const, defaultValue: question.lowerBoundLabel },
{ key: 'upperBoundLabel' as const, defaultValue: question.upperBoundLabel },
]
: []),
]
// First collect fields that have translations but empty defaults
const fieldsWithEmptyDefaults = new Set<string>()
Object.values(question.translations).forEach((trans) => {
textFieldChecks.forEach(({ key, defaultValue }) => {
const value = trans[key]
// Only check if default is explicitly empty (not undefined)
const defaultIsExplicitlyEmpty =
defaultValue !== undefined &&
(typeof defaultValue !== 'string' || defaultValue.trim() === '')
const translationHasValue =
value !== undefined && typeof value === 'string' && value.trim() !== ''
// Track fields with translations but explicitly empty defaults
if (defaultIsExplicitlyEmpty && translationHasValue) {
fieldsWithEmptyDefaults.add(key)
}
})
})
// Add errors for empty default fields that have translations
if (fieldsWithEmptyDefaults.size > 0) {
textFieldChecks.forEach(({ key, defaultValue }) => {
const defaultIsExplicitlyEmpty =
defaultValue !== undefined &&
(typeof defaultValue !== 'string' || defaultValue.trim() === '')
if (defaultIsExplicitlyEmpty && fieldsWithEmptyDefaults.has(key)) {
errors.push({
language: 'default',
questionIndex: qIndex,
field: key,
error: 'Cannot be empty (has translation)',
})
}
})
}
Object.entries(question.translations).forEach(([lang, trans]) => {
// Check text fields
textFieldChecks.forEach(({ key, defaultValue }) => {
const value = trans[key]
const defaultHasValue =
defaultValue && typeof defaultValue === 'string' && defaultValue.trim() !== ''
if (value === TRANSLATION_NEEDED_PLACEHOLDER) {
errors.push({
language: lang,
questionIndex: qIndex,
field: key,
error: `Contains placeholder "${TRANSLATION_NEEDED_PLACEHOLDER}"`,
})
}
// Only validate empty translation strings if default has a value
if (
defaultHasValue &&
value !== undefined &&
typeof value === 'string' &&
value.trim() === ''
) {
errors.push({
language: lang,
questionIndex: qIndex,
field: key,
error: 'Cannot be empty',
})
}
})
// Check link field
if (isLinkSurveyQuestion(question) && 'link' in trans) {
const linkDefaultHasValue = typeof question.link === 'string' && question.link.trim() !== ''
const linkValue = trans.link
if (linkValue === TRANSLATION_NEEDED_PLACEHOLDER) {
errors.push({
language: lang,
questionIndex: qIndex,
field: 'link',
error: `Contains placeholder "${TRANSLATION_NEEDED_PLACEHOLDER}"`,
})
} else if (typeof linkValue === 'string') {
const trimmedLink = linkValue.trim()
if (linkDefaultHasValue && trimmedLink === '') {
errors.push({
language: lang,
questionIndex: qIndex,
field: 'link',
error: 'Cannot be empty',
})
} else if (trimmedLink !== '' && !trimmedLink.match(/^(https:\/\/|mailto:)/)) {
errors.push({
language: lang,
questionIndex: qIndex,
field: 'link',
error: 'Must start with https:// or mailto:',
})
}
}
}
// Check choices array
if (isChoiceSurveyQuestion(question) && trans.choices && Array.isArray(trans.choices)) {
trans.choices.forEach((choice, choiceIndex) => {
if (choice === TRANSLATION_NEEDED_PLACEHOLDER) {
errors.push({
language: lang,
questionIndex: qIndex,
field: `choices[${choiceIndex}]`,
error: `Contains placeholder "${TRANSLATION_NEEDED_PLACEHOLDER}"`,
})
}
if (typeof choice === 'string' && choice.trim() === '') {
errors.push({
language: lang,
questionIndex: qIndex,
field: `choices[${choiceIndex}]`,
error: 'Cannot be empty',
})
}
})
}
})
})
// Also validate default question links
survey.questions.forEach((question, qIndex) => {
const link = isLinkSurveyQuestion(question) && question.link ? question.link.trim() : ''
if (link && !link.match(/^(https:\/\/|mailto:)/)) {
errors.push({
language: 'default',
questionIndex: qIndex,
field: 'link',
error: 'Must start with https:// or mailto:',
})
}
})
return errors
},
],
hasTranslationValidationErrors: [
(s) => [s.translationValidationErrors],
(errors: TranslationValidationError[]): boolean => errors.length > 0,
],
translationErrorsByQuestion: [
(s) => [s.translationValidationErrors, s.editingLanguage],
(
errors: TranslationValidationError[],
editingLanguage: string | null
): ((questionIndex: number) => TranslationValidationError[]) => {
return (questionIndex: number) => {
const targetLanguage = editingLanguage === null ? 'default' : editingLanguage
return errors.filter((e) => e.questionIndex === questionIndex && e.language === targetLanguage)
}
},
],
translationErrorsForField: [
(s) => [s.translationValidationErrors, s.editingLanguage],
(
errors: TranslationValidationError[],
editingLanguage: string | null
): ((questionIndex: number, fieldPath: string) => TranslationValidationError | undefined) => {
return (questionIndex: number, fieldPath: string) => {
const targetLanguage = editingLanguage === null ? 'default' : editingLanguage
return errors.find(
(e) =>
e.questionIndex === questionIndex && e.field === fieldPath && e.language === targetLanguage
)
}
},
],
surveyAsInsightURL: [
(s) => [s.survey],
(survey: NewSurvey | Survey) => {
const query: InsightVizNode = {
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
properties: [
{
key: SurveyEventProperties.SURVEY_ID,
value: survey.id,
operator: PropertyOperator.Exact,
type: PropertyFilterType.Event,
},
],
series: [
{
kind: NodeKind.EventsNode,
event: SurveyEventName.SENT,
name: SurveyEventName.SENT,
math: BaseMathType.TotalCount,
},
{
kind: NodeKind.EventsNode,
event: SurveyEventName.SHOWN,
name: SurveyEventName.SHOWN,
math: BaseMathType.TotalCount,
},
{
kind: NodeKind.EventsNode,
event: SurveyEventName.DISMISSED,
name: SurveyEventName.DISMISSED,
math: BaseMathType.TotalCount,
},
],
},
}
return urls.insightNew({ query })
},
],
defaultInterval: [
(s) => [s.survey],
(survey: Survey): IntervalType => {
const start = dayjs(survey.created_at).utc().startOf('day').format(DATE_FORMAT)
const end = getSurveyEndDateForQuery(survey)
const diffInDays = dayjs(end).diff(dayjs(start), 'days')
const diffInWeeks = dayjs(end).diff(dayjs(start), 'weeks')
if (diffInDays < 2) {
return 'hour'
}
if (diffInWeeks <= 4) {
return 'day'
}
if (diffInWeeks <= 12) {
return 'week'
}
return 'month'
},
],
surveyResponseOutcomes: [
(s) => [s.surveyBaseStatsInternal],
(baseStats: SurveyBaseStatsResult): SurveyResponseOutcome[] | null => {
const counts = baseStats?.find(([eventName]) => eventName === SurveyEventName.SENT)?.[5]
return counts ? getSurveyResponseOutcomeBreakdown(counts) : null
},
],
processedSurveyStats: [
(s) => [s.surveyBaseStatsInternal, s.surveyDismissedAndSentCountInternal],
(
baseStatsResults: SurveyBaseStatsResult,
dismissedAndSentCount: DismissedAndSentCountResult
): SurveyStats | null => {
if (!baseStatsResults) {
return null
}
const defaultEventStats: Omit<SurveyEventStats, 'first_seen' | 'last_seen'> = {
total_count: 0,
unique_persons: 0,
unique_persons_only_seen: 0,
total_count_only_seen: 0,
}
const stats: SurveyStats = {
[SurveyEventName.SHOWN]: { ...defaultEventStats, first_seen: null, last_seen: null },
[SurveyEventName.DISMISSED]: { ...defaultEventStats, first_seen: null, last_seen: null },
[SurveyEventName.SENT]: { ...defaultEventStats, first_seen: null, last_seen: null },
}
// Process base results
baseStatsResults.forEach(([eventName, totalCount, uniquePersons, firstSeen, lastSeen]) => {
const eventStats: SurveyEventStats = {
total_count: totalCount,
unique_persons: uniquePersons,
first_seen: firstSeen ? dayjs(firstSeen).toISOString() : null,
last_seen: lastSeen ? dayjs(lastSeen).toISOString() : null,
unique_persons_only_seen: 0,
total_count_only_seen: 0,
}
if (eventName === SurveyEventName.SHOWN) {
stats[SurveyEventName.SHOWN] = eventStats
} else if (eventName === SurveyEventName.DISMISSED) {
stats[SurveyEventName.DISMISSED] = eventStats
} else if (eventName === SurveyEventName.SENT) {
stats[SurveyEventName.SENT] = eventStats
}
})
// Adjust dismissed unique count
const adjustedDismissedUnique = Math.max(
0,
stats[SurveyEventName.DISMISSED].unique_persons - (dismissedAndSentCount ?? 0)
)
stats[SurveyEventName.DISMISSED].unique_persons = adjustedDismissedUnique
// Calculate derived 'only_seen' counts
const uniqueShown = stats[SurveyEventName.SHOWN].unique_persons
const uniqueDismissed = stats[SurveyEventName.DISMISSED].unique_persons
const uniqueSent = stats[SurveyEventName.SENT].unique_persons
const totalShown = stats[SurveyEventName.SHOWN].total_count
const totalDismissed = stats[SurveyEventName.DISMISSED].total_count
const totalSent = stats[SurveyEventName.SENT].total_count
stats[SurveyEventName.SHOWN].unique_persons_only_seen = Math.max(
0,
uniqueShown - uniqueDismissed - uniqueSent
)
stats[SurveyEventName.SHOWN].total_count_only_seen = Math.max(
0,
totalShown - totalDismissed - totalSent
)
return stats
},
],
surveyRates: [
(s) => [s.processedSurveyStats],
(processedSurveyStats: SurveyStats | null): SurveyRates | null => {
return calculateSurveyRates(processedSurveyStats)
},
],
surveyDemoData: [
(s) => [s.survey],
(survey: Survey | NewSurvey): SurveyDemoData => {
return getDemoDataForSurvey(survey)
},
],
formattedOpenEndedResponses: [
(s) => [s.enrichedConsolidatedSurveyResults, s.survey],
(
consolidatedResults: ConsolidatedSurveyResults,
survey: Survey | NewSurvey
): SurveyAnalysisQuestionGroup[] => {
if (!consolidatedResults?.responsesByQuestion || !survey.questions) {
return []
}
// Helper function to extract response info
const extractResponseInfo = (
response: OpenQuestionResponseData | ChoiceQuestionResponseData
): { timestamp: string } => ({
timestamp: response.timestamp ?? '',
})
const responsesByQuestion: SurveyAnalysisQuestionGroup[] = []
Object.entries(consolidatedResults.responsesByQuestion).forEach(([questionId, processedData]) => {
const question = survey.questions.find((q) => q.id === questionId)
if (!question) {
return
}
const questionResponses: SurveyAnalysisResponseItem[] = []
if (processedData.type === SurveyQuestionType.Open) {
// Pure open questions
const openData = processedData as OpenQuestionProcessedResponses
openData.data.forEach((response) => {
if (response.response?.trim()) {
const responseInfo = extractResponseInfo(response)
questionResponses.push({
responseText: response.response.trim(),
...responseInfo,
isOpenEnded: true,
})
}
})
} else if (
processedData.type === SurveyQuestionType.SingleChoice ||
processedData.type === SurveyQuestionType.MultipleChoice
) {
// Choice questions with open input (isPredefined = false)
const choiceData = processedData as ChoiceQuestionProcessedResponses
choiceData.data.forEach((item) => {
if (!item.isPredefined && item.label?.trim()) {
const responseInfo = extractResponseInfo(item)
questionResponses.push({
responseText: item.label.trim(),
...responseInfo,
isOpenEnded: true,
})
}
})
}
// Only add question if it has open-ended responses
if (questionResponses.length > 0) {
responsesByQuestion.push({
questionName: question.question,
questionId,
responses: questionResponses,
})
}
})
return responsesByQuestion
},
],
surveyWarnings: [
(s) => [s.survey, s.teamSdkVersions],
(
survey: NewSurvey | Survey,
teamSdkVersions: import('./surveyVersionRequirements').TeamSdkVersions
): SurveyFeatureWarning[] => {
return getSurveyWarnings(survey as Survey, teamSdkVersions)
},
],
}),
forms(({ actions, props, values }) => ({
survey: {
defaults: {
...NEW_SURVEY,
appearance: {
...defaultSurveyAppearance,
...teamLogic.values.currentTeam?.survey_config?.appearance,
},
} as NewSurvey | Survey,
errors: ({ name, questions, appearance, type }) => {
const sanitizedAppearance = sanitizeSurveyAppearance(appearance)
return {
name: !name && 'Please enter a name.',
questions: questions.map((question) => {
const questionErrors = {
question: !question.question && 'Please enter a question label.',
}
if (question.type === SurveyQuestionType.Link) {
if (question.link) {
if (question.link.startsWith('mailto:')) {
const emailRegex = /^mailto:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
if (!emailRegex.test(question.link)) {
return {
...questionErrors,
link: 'Please enter a valid mailto link (e.g., mailto:[email protected]).',
}
}
} else {
try {
const url = new URL(question.link)
if (url.protocol !== 'https:') {
return {
...questionErrors,
link: 'Only HTTPS links are supported for security reasons.',
}
}
} catch {
return {
...questionErrors,
link: 'Please enter a valid HTTPS URL.',
}
}
}
}
}
if (question.type === SurveyQuestionType.Rating) {
// Thumb questions (emoji + 2-point scale) hide the bound-label inputs in the editor,
// so requiring them here would silently block save with no visible error.
const requiresBoundLabels = !isThumbQuestion(question)
return {
...questionErrors,
display: !question.display && 'Please choose a display type.',
scale: !question.scale && 'Please choose a scale.',
lowerBoundLabel:
requiresBoundLabels &&
!question.lowerBoundLabel &&
'Please enter a lower bound label.',
upperBoundLabel:
requiresBoundLabels &&
!question.upperBoundLabel &&
'Please enter an upper bound label.',
}
} else if (
question.type === SurveyQuestionType.SingleChoice ||
question.type === SurveyQuestionType.MultipleChoice
) {
return {
...questionErrors,
choices:
!question.choices?.length || question.choices.some((choice) => !choice.trim())
? 'Please ensure all choices are non-empty.'
: undefined,
}
}
return questionErrors
}),
// release conditions controlled using a PureField in the form
targeting_flag_filters: values.flagPropertyErrors,
// controlled using a PureField in the form
urlMatchType: values.urlMatchTypeValidationError,
appearance:
sanitizedAppearance &&
validateSurveyAppearance(
sanitizedAppearance,
questions.some((q) => q.type === SurveyQuestionType.Rating),
type
),
}
},
submit: (surveyPayload) => {
if (values.hasCycle) {
posthog.capture('survey cycle detected', {
name: values.survey.name,
id: values.survey.id,
start_date: values.survey.start_date,
end_date: values.survey.end_date,
})
lemonToast.error('Your survey contains an endless cycle. Please revisit your branching rules.')
return
}
const payload = sanitizeSurvey(surveyPayload, { keepEmptyConditions: true })
// when the survey is being submitted, we should turn off editing mode
actions.editingSurvey(false)
if (props.id && props.id !== 'new') {
actions.updateSurvey(payload)
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEY_EDITED,
metadata: {
survey_id: values.survey.id,
},
})
} else {
actions.createSurvey({ ...payload, _create_in_folder: 'Unfiled/Surveys' })
}
},
},
})),
urlToAction(({ actions, props, values }) => ({
[urls.survey(props.id ?? 'new')]: (_, searchParams, { fromTemplate }, { method }) => {
// Sync active tab from URL
const tabFromUrl = searchParams.tab
if (tabFromUrl && Object.values(SurveyTab).includes(tabFromUrl) && tabFromUrl !== values.activeTab) {
actions.setActiveTab(tabFromUrl as SurveyTab)
} else if (searchParams.activity && values.activeTab !== SurveyTab.HISTORY) {
actions.setActiveTab(SurveyTab.HISTORY)
} else if (!tabFromUrl && !searchParams.activity && values.activeTab !== SurveyTab.SUMMARY) {
actions.setActiveTab(SurveyTab.SUMMARY)
}
// Preserve unsaved edits whenever we re-enter the same survey URL — covers
// both explicit opt-in navigations (e.g. guided↔full editor switch) and
// implicit re-entries like tab switching, which also dispatch a PUSH.
const shouldPreserveLocalChanges = values.surveyChanged && values.survey.id === (props.id ?? NEW_SURVEY.id)
// Parse filters from URL params
if (searchParams.propertyFilters) {
try {
const parsedPropertyFilters = JSON.parse(searchParams.propertyFilters)
if (Array.isArray(parsedPropertyFilters) && parsedPropertyFilters.length > 0) {
actions.setPropertyFilters(parsedPropertyFilters)
}
} catch (e) {
console.error('Failed to parse propertyFilters from URL:', e)
}
}
if (searchParams.answerFilters) {
try {
const parsedAnswerFilters = JSON.parse(searchParams.answerFilters)
if (Array.isArray(parsedAnswerFilters) && parsedAnswerFilters.length > 0) {
const mergedFilters =
values.answerFilters.length > 0
? values.answerFilters.map((existingFilter) => {
const urlFilter = parsedAnswerFilters.find(
(f: EventPropertyFilter) => f.key === existingFilter.key
)
return urlFilter ?? existingFilter
})
: parsedAnswerFilters
actions.setAnswerFilters(mergedFilters, false)
}
} catch (e) {
console.error('Failed to parse answerFilters from URL:', e)
}
}
if (searchParams.date_from || searchParams.date_to) {
actions.setDateRange(
{
date_from: searchParams.date_from || null,
date_to: searchParams.date_to || null,
},
false
)
}
// We always set the editingSurvey to true when we create a new survey
if (props.id === 'new') {
actions.editingSurvey(true)
}
// If the URL was pushed (user clicked on a link), reset the scene's data.
// This avoids resetting form fields if you click back/forward.
if (method === 'PUSH') {
if (shouldPreserveLocalChanges) {
if (searchParams.edit) {
actions.editingSurvey(true)
}
return
}
// When pushing to `/new` and the id matches the new survey's id, do not load the survey again
if (props.id === 'new' && values.survey.id === NEW_SURVEY.id && !fromTemplate) {
return
}
if (props.id) {
actions.loadSurvey()
} else {
actions.resetSurvey()
}
}
if (searchParams.edit) {
actions.editingSurvey(true)
}
},
})),
actionToUrl(({ values }) => ({
setActiveTab: ({ tab }) => {
const searchParams = { ...router.values.searchParams }
if (tab === SurveyTab.SUMMARY) {
delete searchParams['tab']
} else {
searchParams['tab'] = tab
}
delete searchParams['activity']
return [router.values.location.pathname, searchParams, router.values.hashParams, { replace: true }]
},
editingSurvey: ({ editing }) => {
const searchParams = router.values.searchParams
if (editing) {
searchParams['edit'] = true
} else {
delete searchParams['edit']
}
return [router.values.location.pathname, router.values.searchParams, router.values.hashParams]
},
setPropertyFilters: () => [
router.values.location.pathname,
values.urlSearchParams,
router.values.hashParams,
{ replace: true },
],
setAnswerFilters: () => [
router.values.location.pathname,
values.urlSearchParams,
router.values.hashParams,
{ replace: true },
],
setDateRange: () => [
router.values.location.pathname,
values.urlSearchParams,
router.values.hashParams,
{ replace: true },
],
})),
afterMount(({ props, actions, values }) => {
// Preserve any in-memory edits when re-mounting on the same survey id (e.g.
// navigating between the guided wizard and the full editor). No URL flag —
// surveyChanged is in-memory only, so a fresh session can never trigger this.
const shouldPreserveLocalChanges = values.surveyChanged && values.survey.id === props.id
if (props.id !== 'new' && !shouldPreserveLocalChanges) {
actions.loadSurvey()
actions.loadSurveyNotifications()
actions.loadReusableSurveyNotifications()
}
if (props.id === 'new' && !shouldPreserveLocalChanges) {
actions.resetSurvey()
}
}),
])