diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx index 92b9555c71f33..5b6b8e5879c29 100644 --- a/frontend/src/scenes/surveys/surveyLogic.tsx +++ b/frontend/src/scenes/surveys/surveyLogic.tsx @@ -86,6 +86,8 @@ export interface QuestionResultsReady { [key: string]: boolean } +const getResponseField = (i: number): string => (i === 0 ? '$survey_response' : `$survey_response_${i}`) + export const surveyLogic = kea([ props({} as SurveyLogicProps), key(({ id }) => id), @@ -228,13 +230,12 @@ export const surveyLogic = kea([ ? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD') : dayjs().add(1, 'day').format('YYYY-MM-DD') - const surveyResponseField = - questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}` - const query: HogQLQuery = { kind: NodeKind.HogQLQuery, query: ` - SELECT JSONExtractString(properties, '${surveyResponseField}') AS survey_response, COUNT(survey_response) + SELECT + JSONExtractString(properties, '${getResponseField(questionIndex)}') AS survey_response, + COUNT(survey_response) FROM events WHERE event = 'survey sent' AND properties.$survey_id = '${props.id}' @@ -269,13 +270,12 @@ export const surveyLogic = kea([ ? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD') : dayjs().add(1, 'day').format('YYYY-MM-DD') - const surveyResponseField = - questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}` - const query: HogQLQuery = { kind: NodeKind.HogQLQuery, query: ` - SELECT JSONExtractString(properties, '${surveyResponseField}') AS survey_response, COUNT(survey_response) + SELECT + JSONExtractString(properties, '${getResponseField(questionIndex)}') AS survey_response, + COUNT(survey_response) FROM events WHERE event = 'survey sent' AND properties.$survey_id = '${props.id}' @@ -312,13 +312,12 @@ export const surveyLogic = kea([ ? dayjs(survey.end_date).add(1, 'day').format('YYYY-MM-DD') : dayjs().add(1, 'day').format('YYYY-MM-DD') - const surveyResponseField = - questionIndex === 0 ? '$survey_response' : `$survey_response_${questionIndex}` - const query: HogQLQuery = { kind: NodeKind.HogQLQuery, query: ` - SELECT count(), arrayJoin(JSONExtractArrayRaw(properties, '${surveyResponseField}')) AS choice + SELECT + count(), + arrayJoin(JSONExtractArrayRaw(properties, '${getResponseField(questionIndex)}')) AS choice FROM events WHERE event == 'survey sent' AND properties.$survey_id == '${survey.id}' @@ -599,7 +598,7 @@ export const surveyLogic = kea([ ], dataTableQuery: [ (s) => [s.survey, s.surveyResponseProperty], - (survey, surveyResponseProperty): DataTableNode | null => { + (survey): DataTableNode | null => { if (survey.id === 'new') { return null } @@ -608,7 +607,28 @@ export const surveyLogic = kea([ kind: NodeKind.DataTableNode, source: { kind: NodeKind.EventsQuery, - select: ['*', `properties.${surveyResponseProperty}`, 'timestamp', 'person'], + select: [ + '*', + ...survey.questions.map((q, i) => { + if (q.type === SurveyQuestionType.MultipleChoice) { + // Join array items into a string + return `coalesce(arrayStringConcat(JSONExtractArrayRaw(properties, '${getResponseField( + i + )}'), ', ')) -- ${q.question}` + } else if (q.type === SurveyQuestionType.Rating) { + // Decrement 1 due to 0 - 10 scale + return `coalesce(JSONExtractInt(properties, '${getResponseField(i)}') - 1) -- ${ + q.question + }` + } + + return `coalesce(JSONExtractString(properties, '${getResponseField(i)}')) -- ${ + q.question + }` + }), + 'timestamp', + 'person', + ], orderBy: ['timestamp DESC'], where: [`event == 'survey sent'`], after: createdAt,