-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 3 commits
ee56aed
fd1a5c4
8d6c02d
d338e89
f95445a
09acc09
d544230
c17057d
45b1015
4f99666
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @fileoverview A component that displays an interactive survey within a session recording. It handles survey display, user responses, and submission | ||
*/ | ||
import { LemonButton, LemonCheckbox } from '@posthog/lemon-ui' | ||
import { useActions, useValues } from 'kea' | ||
import { InternalMultipleChoiceSurveyLogic } from 'lib/components/InternalSurvey/InternalMultipleChoiceSurveyLogic' | ||
|
||
import { SurveyQuestion, SurveyQuestionType } from '~/types' | ||
|
||
interface InternalSurveyProps { | ||
surveyId: string | ||
} | ||
|
||
export function InternalMultipleChoiceSurvey({ surveyId }: InternalSurveyProps): JSX.Element { | ||
const logic = InternalMultipleChoiceSurveyLogic({ surveyId }) | ||
const { survey, surveyResponse, showThankYouMessage, thankYouMessage } = useValues(logic) | ||
const { handleChoiceChange, handleSurveyResponse } = 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 && ( | ||
<> | ||
{question.question} | ||
{question.type === SurveyQuestionType.MultipleChoice && ( | ||
<ul className="list-inside list-none my-2"> | ||
{question.choices.map((choice) => ( | ||
<li key={choice}> | ||
<LemonCheckbox | ||
onChange={(checked) => handleChoiceChange(choice, checked)} | ||
label={choice} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
<LemonButton | ||
type="primary" | ||
disabledReason={ | ||
surveyResponse.length === 0 ? 'Please select at least one option' : false | ||
} | ||
onClick={handleSurveyResponse} | ||
> | ||
{question.buttonText ?? 'Submit'} | ||
</LemonButton> | ||
</> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @fileoverview A logic that handles the internal multiple choice survey | ||
*/ | ||
import { actions, afterMount, kea, key, listeners, path, props, reducers } from 'kea' | ||
import posthog, { Survey as PostHogSurvey } from 'posthog-js' | ||
|
||
import type { InternalMultipleChoiceSurveyLogicType } from './InternalMultipleChoiceSurveyLogicType' | ||
|
||
export interface InternalSurveyLogicProps { | ||
surveyId: string | ||
} | ||
|
||
export const InternalMultipleChoiceSurveyLogic = kea<InternalMultipleChoiceSurveyLogicType>([ | ||
path(['lib', 'components', 'InternalSurvey', 'InternalMultipleChoiceSurveyLogic']), | ||
props({} as InternalSurveyLogicProps), | ||
key((props) => props.surveyId), | ||
actions({ | ||
setSurveyId: (surveyId: string) => ({ surveyId }), | ||
getSurveys: () => ({}), | ||
setSurvey: (survey: PostHogSurvey) => ({ survey }), | ||
handleSurveys: (surveys: PostHogSurvey[]) => ({ surveys }), | ||
handleSurveyResponse: () => ({}), | ||
handleChoiceChange: (choice: string, isAdded: boolean) => ({ choice, isAdded }), | ||
setShowThankYouMessage: (showThankYouMessage: boolean) => ({ showThankYouMessage }), | ||
setThankYouMessage: (thankYouMessage: string) => ({ thankYouMessage }), | ||
}), | ||
reducers({ | ||
surveyId: [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the use case for varying the survey id past the one specified in props? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I see why you're using this and included what to do instead - hope that makes sense :) |
||
null as string | null, | ||
{ | ||
setSurveyId: (_, { surveyId }) => surveyId, | ||
}, | ||
], | ||
survey: [ | ||
null as PostHogSurvey | null, | ||
{ | ||
setSurvey: (_, { survey }) => survey, | ||
}, | ||
], | ||
thankYouMessage: [ | ||
'Thank you for your feedback!', | ||
{ | ||
setThankYouMessage: (_, { thankYouMessage }) => thankYouMessage, | ||
}, | ||
], | ||
showThankYouMessage: [ | ||
false as boolean, | ||
{ | ||
setShowThankYouMessage: (_, { showThankYouMessage }) => showThankYouMessage, | ||
}, | ||
], | ||
surveyResponse: [ | ||
[] as string[], | ||
{ | ||
handleChoiceChange: (state, { choice, isAdded }) => | ||
isAdded ? [...state, choice] : state.filter((c) => c !== choice), | ||
}, | ||
], | ||
}), | ||
listeners(({ actions, values }) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you'd add props here alongside |
||
/** When surveyId is set, get the list of surveys for the user */ | ||
setSurveyId: () => { | ||
posthog.getSurveys(actions.handleSurveys) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so you can call this in the afterMount instead of calling setSurveyId in it |
||
}, | ||
/** Callback for the surveys response. Filter it to the surveyId and set the survey */ | ||
handleSurveys: ({ surveys }) => { | ||
const survey = surveys.find((s: PostHogSurvey) => s.id === values.surveyId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then this would be props.surveyId |
||
if (survey) { | ||
posthog.capture('survey shown', { | ||
$survey_id: values.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: () => { | ||
posthog.capture('survey sent', { | ||
$survey_id: values.surveyId, | ||
$survey_response: values.surveyResponse, | ||
}) | ||
actions.setShowThankYouMessage(true) | ||
setTimeout(() => actions.setSurvey(null), 5000) | ||
}, | ||
})), | ||
afterMount(({ actions, props }) => { | ||
/** When the logic is mounted, set the surveyId from the props */ | ||
actions.setSurveyId(props.surveyId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah... you can read props in variious places so don't need that... let me re-read this... |
||
}), | ||
]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,7 @@ export const SESSION_REPLAY_MINIMUM_DURATION_OPTIONS: LemonSelectOptions<number | |
] | ||
|
||
export const UNSUBSCRIBE_SURVEY_ID = '018b6e13-590c-0000-decb-c727a2b3f462' | ||
export const SESSION_RECORDING_OPT_OUT_SURVEY_ID = '0194a763-9a13-0000-8088-32b52acf7156' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
export const TAILWIND_BREAKPOINTS = { | ||
sm: 526, | ||
|
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.
nit, we tend to lower camel case these... so
internalMultipleChoiceSurveyLogic