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

feat: add a survey to opt out SR #27949

Merged
merged 10 commits into from
Jan 28, 2025
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>([
Copy link
Member

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

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: [
Copy link
Member

Choose a reason for hiding this comment

The 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?
since we're keyed on survey id in props i think this shouldn't change?

Copy link
Member

Choose a reason for hiding this comment

The 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 }) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'd add props here alongside actions, values

/** When surveyId is set, get the list of surveys for the user */
setSurveyId: () => {
posthog.getSurveys(actions.handleSurveys)
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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...

}),
])
1 change: 1 addition & 0 deletions frontend/src/lib/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

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


export const TAILWIND_BREAKPOINTS = {
sm: 526,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import { useActions, useValues } from 'kea'
import { AuthorizedUrlList } from 'lib/components/AuthorizedUrlList/AuthorizedUrlList'
import { AuthorizedUrlListType } from 'lib/components/AuthorizedUrlList/authorizedUrlListLogic'
import { EventSelect } from 'lib/components/EventSelect/EventSelect'
import { InternalMultipleChoiceSurvey } from 'lib/components/InternalSurvey/InternalMultipleChoiceSurvey'
import { PropertySelect } from 'lib/components/PropertySelect/PropertySelect'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { SESSION_RECORDING_OPT_OUT_SURVEY_ID } from 'lib/constants'
import { IconSelectEvents } from 'lib/lemon-ui/icons'
import { LemonLabel } from 'lib/lemon-ui/LemonLabel/LemonLabel'
import { isObject, objectsEqual } from 'lib/utils'
import { ReactNode } from 'react'
import { ReactNode, useState } from 'react'
import { teamLogic } from 'scenes/teamLogic'

import { SessionRecordingAIConfig } from '~/types'
Expand Down Expand Up @@ -504,6 +506,20 @@ export function ReplayAISettings(): JSX.Element | null {
export function ReplayGeneral(): JSX.Element {
const { updateCurrentTeam } = useActions(teamLogic)
const { currentTeam } = useValues(teamLogic)
const [showSurvey, setShowSurvey] = useState<boolean>(false)

/**
* Handle the opt in change
* @param checked
*/
const handleOptInChange = (checked: boolean): void => {
updateCurrentTeam({
session_recording_opt_in: checked,
})

//If the user opts out, we show the survey
setShowSurvey(!checked)
}

return (
<div className="flex flex-col gap-4">
Expand All @@ -521,16 +537,13 @@ export function ReplayGeneral(): JSX.Element {
<LemonSwitch
data-attr="opt-in-session-recording-switch"
onChange={(checked) => {
updateCurrentTeam({
// when switching replay on or off,
// we set defaults for some of the other settings
session_recording_opt_in: checked,
})
handleOptInChange(checked)
}}
label="Record user sessions"
bordered
checked={!!currentTeam?.session_recording_opt_in}
/>
{showSurvey && <InternalMultipleChoiceSurvey surveyId={SESSION_RECORDING_OPT_OUT_SURVEY_ID} />}
</div>
<LogCaptureSettings />
<CanvasCaptureSettings />
Expand Down
Loading