-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
feat: add a survey to opt out SR #27949
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ee56aed
feat: add a survey to opt out SR
veryayskiy fd1a5c4
Merge branch 'master' into feat/sr/opt-out-survey
veryayskiy 8d6c02d
Update the survey id for production
veryayskiy d338e89
Update
veryayskiy f95445a
Fixes in logic
veryayskiy 09acc09
Optimisation and fix open question
veryayskiy d544230
Merge branch 'master' into feat/sr/opt-out-survey
veryayskiy c17057d
Rename
veryayskiy 45b1015
Fix path
veryayskiy 4f99666
Merge branch 'master' into feat/sr/opt-out-survey
veryayskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
frontend/src/lib/components/InternalSurvey/InternalMultipleChoiceSurvey.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @fileoverview A component that displays an interactive survey within a session recording. It handles survey display, user responses, and submission | ||
*/ | ||
import { LemonButton, LemonCheckbox, LemonTextArea } from '@posthog/lemon-ui' | ||
import { useActions, useValues } from 'kea' | ||
|
||
import { SurveyQuestion, SurveyQuestionType } from '~/types' | ||
|
||
import { internalMultipleChoiceSurveyLogic } from './internalMultipleChoiceSurveyLogic' | ||
|
||
interface InternalSurveyProps { | ||
surveyId: string | ||
} | ||
|
||
export function InternalMultipleChoiceSurvey({ surveyId }: InternalSurveyProps): JSX.Element { | ||
const logic = internalMultipleChoiceSurveyLogic({ surveyId }) | ||
const { survey, surveyResponse, showThankYouMessage, thankYouMessage, openChoice } = useValues(logic) | ||
const { handleChoiceChange, handleSurveyResponse, setOpenChoice } = useActions(logic) | ||
|
||
if (!survey) { | ||
return <></> | ||
} | ||
|
||
return ( | ||
<div className="Popover Popover--padded Popover--appear-done Popover--enter-done my-4"> | ||
<div className="Popover__box p-4"> | ||
{survey.questions.map((question: SurveyQuestion) => ( | ||
<div key={question.question} className="text-sm"> | ||
{showThankYouMessage && thankYouMessage} | ||
{!showThankYouMessage && ( | ||
<> | ||
<strong>{question.question}</strong> | ||
{question.type === SurveyQuestionType.MultipleChoice && ( | ||
<ul className="list-inside list-none mt-2"> | ||
{question.choices.map((choice, index) => { | ||
// Add an open choice text area if the last choice is an open choice | ||
if (index === question.choices.length - 1 && question.hasOpenChoice) { | ||
return ( | ||
<div className="mt-2" key={choice}> | ||
{choice} | ||
<LemonTextArea | ||
placeholder="Please share any additional comments or feedback" | ||
onChange={setOpenChoice} | ||
value={openChoice ?? ''} | ||
className="my-2" | ||
/> | ||
</div> | ||
) | ||
} | ||
return ( | ||
<li key={choice}> | ||
<LemonCheckbox | ||
onChange={(checked) => handleChoiceChange(choice, checked)} | ||
label={choice} | ||
className="font-normal" | ||
/> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
)} | ||
<LemonButton | ||
type="primary" | ||
disabledReason={ | ||
surveyResponse.length === 0 && openChoice === null | ||
? 'Please select at least one option' | ||
: false | ||
} | ||
onClick={handleSurveyResponse} | ||
> | ||
{question.buttonText ?? 'Submit'} | ||
</LemonButton> | ||
</> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} |
95 changes: 95 additions & 0 deletions
95
frontend/src/lib/components/InternalSurvey/internalMultipleChoiceSurveyLogic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { actions, afterMount, kea, key, listeners, path, props, reducers } from 'kea' | ||
import posthog from 'posthog-js' | ||
|
||
import { Survey } from '~/types' | ||
|
||
import type { internalMultipleChoiceSurveyLogicType } from './internalMultipleChoiceSurveyLogicType' | ||
|
||
export interface InternalSurveyLogicProps { | ||
surveyId: string | ||
} | ||
|
||
export const internalMultipleChoiceSurveyLogic = kea<internalMultipleChoiceSurveyLogicType>([ | ||
path(['lib', 'components', 'InternalSurvey', 'internalMultipleChoiceSurveyLogicType']), | ||
props({} as InternalSurveyLogicProps), | ||
key((props) => props.surveyId), | ||
actions({ | ||
getSurveys: () => ({}), | ||
setSurvey: (survey: Survey) => ({ survey }), | ||
handleSurveys: (surveys: Survey[]) => ({ surveys }), | ||
handleSurveyResponse: () => ({}), | ||
handleChoiceChange: (choice: string, isAdded: boolean) => ({ choice, isAdded }), | ||
setShowThankYouMessage: (showThankYouMessage: boolean) => ({ showThankYouMessage }), | ||
setThankYouMessage: (thankYouMessage: string) => ({ thankYouMessage }), | ||
setOpenChoice: (openChoice: string) => ({ openChoice }), | ||
}), | ||
reducers({ | ||
survey: [ | ||
null as Survey | null, | ||
{ | ||
setSurvey: (_, { survey }) => survey, | ||
}, | ||
], | ||
thankYouMessage: [ | ||
'Thank you for your feedback!', | ||
{ | ||
setThankYouMessage: (_, { thankYouMessage }) => thankYouMessage, | ||
}, | ||
], | ||
showThankYouMessage: [ | ||
false as boolean, | ||
{ | ||
setShowThankYouMessage: (_, { showThankYouMessage }) => showThankYouMessage, | ||
}, | ||
], | ||
openChoice: [ | ||
null as string | null, | ||
{ | ||
setOpenChoice: (_, { openChoice }) => openChoice, | ||
}, | ||
], | ||
surveyResponse: [ | ||
[] as string[], | ||
{ | ||
handleChoiceChange: (state, { choice, isAdded }) => | ||
isAdded ? [...state, choice] : state.filter((c: string) => c !== choice), | ||
}, | ||
], | ||
}), | ||
listeners(({ actions, values, props }) => ({ | ||
/** When surveyId is set, get the list of surveys for the user */ | ||
setSurveyId: () => {}, | ||
/** Callback for the surveys response. Filter it to the surveyId and set the survey */ | ||
handleSurveys: ({ surveys }) => { | ||
const survey = surveys.find((s: Survey) => s.id === props.surveyId) | ||
if (survey) { | ||
posthog.capture('survey shown', { | ||
$survey_id: props.surveyId, | ||
}) | ||
actions.setSurvey(survey) | ||
|
||
if (survey.appearance?.thankYouMessageHeader) { | ||
actions.setThankYouMessage(survey.appearance?.thankYouMessageHeader) | ||
} | ||
} | ||
}, | ||
/** When the survey response is sent, capture the response and show the thank you message */ | ||
handleSurveyResponse: () => { | ||
const payload = { | ||
$survey_id: props.surveyId, | ||
$survey_response: values.surveyResponse, | ||
} | ||
if (values.openChoice) { | ||
payload.$survey_response.push(values.openChoice) | ||
} | ||
posthog.capture('survey sent', payload) | ||
|
||
actions.setShowThankYouMessage(true) | ||
setTimeout(() => actions.setSurvey(null as unknown as Survey), 5000) | ||
}, | ||
})), | ||
afterMount(({ actions }) => { | ||
/** When the logic is mounted, set the surveyId from the props */ | ||
posthog.getSurveys((surveys) => actions.handleSurveys(surveys as unknown as Survey[])) | ||
}), | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could do this as a flag payload so we can change the survey without a code release but that's very nit-picky