Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(surveys): 7-point likert scale didn't work with branching logic #1424

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/__tests__/surveys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,32 @@ describe('surveys', () => {
expect(surveys.getNextSurveyStep(survey, 0, 5)).toEqual(3)
})

// Response-based branching, scale 1-7
it('should branch out the negative/neutral/positive respondents correctly (scale 1-7)', () => {
survey.questions = [
{
question: 'How satisfied are you?',
type: SurveyQuestionType.Rating,
scale: 7,
branching: {
type: SurveyQuestionBranchingType.ResponseBased,
responseValues: { negative: 1, neutral: 2, positive: 3 },
},
},
{ type: SurveyQuestionType.Open, question: 'We apologize for your experience. Can you tell us more?' },
{ type: SurveyQuestionType.Open, question: 'What could we do to improve your experience?' },
{ type: SurveyQuestionType.Open, question: 'Great! What did you enjoy the most?' },
] as unknown as SurveyQuestion[]

expect(surveys.getNextSurveyStep(survey, 0, 1)).toEqual(1)
expect(surveys.getNextSurveyStep(survey, 0, 2)).toEqual(1)
expect(surveys.getNextSurveyStep(survey, 0, 3)).toEqual(1)
expect(surveys.getNextSurveyStep(survey, 0, 4)).toEqual(2)
expect(surveys.getNextSurveyStep(survey, 0, 5)).toEqual(3)
expect(surveys.getNextSurveyStep(survey, 0, 6)).toEqual(3)
expect(surveys.getNextSurveyStep(survey, 0, 7)).toEqual(3)
})

// Response-based branching, scale 0-10 (NPS)
it('should branch out detractors/passives/promoters correctly', () => {
survey.questions = [
Expand Down Expand Up @@ -1088,7 +1114,7 @@ describe('surveys', () => {
{ type: SurveyQuestionType.Open, question: 'Seems you are not completely happy. Tell us more!' },
{ type: SurveyQuestionType.Open, question: 'Glad to hear that. Tell us more!' },
] as unknown as SurveyQuestion[]
expect(() => surveys.getNextSurveyStep(survey, 0, 1)).toThrow('The scale must be one of: 3, 5, 10')
expect(() => surveys.getNextSurveyStep(survey, 0, 1)).toThrow('The scale must be one of: 3, 5, 7, 10')
})

it('should throw an error for a response value out of the valid range', () => {
Expand All @@ -1109,6 +1135,8 @@ describe('surveys', () => {
expect(() => surveys.getNextSurveyStep(survey, 0, 20)).toThrow('The response must be in range 1-3')
;(survey.questions[0] as RatingSurveyQuestion).scale = 5
expect(() => surveys.getNextSurveyStep(survey, 0, 20)).toThrow('The response must be in range 1-5')
;(survey.questions[0] as RatingSurveyQuestion).scale = 7
expect(() => surveys.getNextSurveyStep(survey, 0, 20)).toThrow('The response must be in range 1-7')
;(survey.questions[0] as RatingSurveyQuestion).scale = 10
expect(() => surveys.getNextSurveyStep(survey, 0, 20)).toThrow('The response must be in range 0-10')
})
Expand Down
8 changes: 7 additions & 1 deletion src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function getRatingBucketForResponseValue(responseValue: number, scale: number) {
}

return responseValue <= 2 ? 'negative' : responseValue === 3 ? 'neutral' : 'positive'
} else if (scale === 7) {
if (responseValue < 1 || responseValue > 7) {
throw new Error('The response must be in range 1-7')
}

return responseValue <= 3 ? 'negative' : responseValue === 4 ? 'neutral' : 'positive'
} else if (scale === 10) {
if (responseValue < 0 || responseValue > 10) {
throw new Error('The response must be in range 0-10')
Expand All @@ -48,7 +54,7 @@ function getRatingBucketForResponseValue(responseValue: number, scale: number) {
return responseValue <= 6 ? 'detractors' : responseValue <= 8 ? 'passives' : 'promoters'
}

throw new Error('The scale must be one of: 3, 5, 10')
throw new Error('The scale must be one of: 3, 5, 7, 10')
}

export class PostHogSurveys {
Expand Down
Loading