1
1
import { SURVEYS } from './constants'
2
2
import { getSurveySeenStorageKeys } from './extensions/surveys/surveys-utils'
3
3
import { PostHog } from './posthog-core'
4
- import {
5
- Survey ,
6
- SurveyCallback ,
7
- SurveyMatchType ,
8
- SurveyQuestionBranchingType ,
9
- SurveyQuestionType ,
10
- } from './posthog-surveys-types'
4
+ import { Survey , SurveyCallback , SurveyMatchType } from './posthog-surveys-types'
11
5
import { RemoteConfig } from './types'
12
6
import { Info } from './utils/event-utils'
13
7
import { assignableWindow , document , userAgent , window } from './utils/globals'
@@ -32,109 +26,6 @@ export const surveyValidationMap: Record<SurveyMatchType, (targets: string[], va
32
26
is_not : ( targets , value ) => targets . every ( ( target ) => value !== target ) ,
33
27
}
34
28
35
- function getRatingBucketForResponseValue ( responseValue : number , scale : number ) {
36
- if ( scale === 3 ) {
37
- if ( responseValue < 1 || responseValue > 3 ) {
38
- throw new Error ( 'The response must be in range 1-3' )
39
- }
40
-
41
- return responseValue === 1 ? 'negative' : responseValue === 2 ? 'neutral' : 'positive'
42
- } else if ( scale === 5 ) {
43
- if ( responseValue < 1 || responseValue > 5 ) {
44
- throw new Error ( 'The response must be in range 1-5' )
45
- }
46
-
47
- return responseValue <= 2 ? 'negative' : responseValue === 3 ? 'neutral' : 'positive'
48
- } else if ( scale === 7 ) {
49
- if ( responseValue < 1 || responseValue > 7 ) {
50
- throw new Error ( 'The response must be in range 1-7' )
51
- }
52
-
53
- return responseValue <= 3 ? 'negative' : responseValue === 4 ? 'neutral' : 'positive'
54
- } else if ( scale === 10 ) {
55
- if ( responseValue < 0 || responseValue > 10 ) {
56
- throw new Error ( 'The response must be in range 0-10' )
57
- }
58
-
59
- return responseValue <= 6 ? 'detractors' : responseValue <= 8 ? 'passives' : 'promoters'
60
- }
61
-
62
- throw new Error ( 'The scale must be one of: 3, 5, 7, 10' )
63
- }
64
-
65
- export function getNextSurveyStep (
66
- survey : Survey ,
67
- currentQuestionIndex : number ,
68
- response : string | string [ ] | number | null
69
- ) {
70
- const question = survey . questions [ currentQuestionIndex ]
71
- const nextQuestionIndex = currentQuestionIndex + 1
72
-
73
- if ( ! question . branching ?. type ) {
74
- if ( currentQuestionIndex === survey . questions . length - 1 ) {
75
- return SurveyQuestionBranchingType . End
76
- }
77
-
78
- return nextQuestionIndex
79
- }
80
-
81
- if ( question . branching . type === SurveyQuestionBranchingType . End ) {
82
- return SurveyQuestionBranchingType . End
83
- } else if ( question . branching . type === SurveyQuestionBranchingType . SpecificQuestion ) {
84
- if ( Number . isInteger ( question . branching . index ) ) {
85
- return question . branching . index
86
- }
87
- } else if ( question . branching . type === SurveyQuestionBranchingType . ResponseBased ) {
88
- // Single choice
89
- if ( question . type === SurveyQuestionType . SingleChoice ) {
90
- // :KLUDGE: for now, look up the choiceIndex based on the response
91
- // TODO: once QuestionTypes.MultipleChoiceQuestion is refactored, pass the selected choiceIndex into this method
92
- const selectedChoiceIndex = question . choices . indexOf ( `${ response } ` )
93
-
94
- if ( question . branching ?. responseValues ?. hasOwnProperty ( selectedChoiceIndex ) ) {
95
- const nextStep = question . branching . responseValues [ selectedChoiceIndex ]
96
-
97
- // Specific question
98
- if ( Number . isInteger ( nextStep ) ) {
99
- return nextStep
100
- }
101
-
102
- if ( nextStep === SurveyQuestionBranchingType . End ) {
103
- return SurveyQuestionBranchingType . End
104
- }
105
-
106
- return nextQuestionIndex
107
- }
108
- } else if ( question . type === SurveyQuestionType . Rating ) {
109
- if ( typeof response !== 'number' || ! Number . isInteger ( response ) ) {
110
- throw new Error ( 'The response type must be an integer' )
111
- }
112
-
113
- const ratingBucket = getRatingBucketForResponseValue ( response , question . scale )
114
-
115
- if ( question . branching ?. responseValues ?. hasOwnProperty ( ratingBucket ) ) {
116
- const nextStep = question . branching . responseValues [ ratingBucket ]
117
-
118
- // Specific question
119
- if ( Number . isInteger ( nextStep ) ) {
120
- return nextStep
121
- }
122
-
123
- if ( nextStep === SurveyQuestionBranchingType . End ) {
124
- return SurveyQuestionBranchingType . End
125
- }
126
-
127
- return nextQuestionIndex
128
- }
129
- }
130
-
131
- return nextQuestionIndex
132
- }
133
-
134
- logger . warn ( 'Falling back to next question index due to unexpected branching type' )
135
- return nextQuestionIndex
136
- }
137
-
138
29
function defaultMatchType ( matchType ?: SurveyMatchType ) : SurveyMatchType {
139
30
return matchType ?? 'icontains'
140
31
}
@@ -375,7 +266,6 @@ export class PostHogSurveys {
375
266
return this . instance . featureFlags . isFeatureEnabled ( value )
376
267
} )
377
268
}
378
- getNextSurveyStep = getNextSurveyStep
379
269
380
270
// this method is lazily loaded onto the window to avoid loading preact and other dependencies if surveys is not enabled
381
271
private _canActivateRepeatedly ( survey : Survey ) {
0 commit comments