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: new filters for nps trends #28759

Merged
merged 8 commits into from
Feb 15, 2025
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
181 changes: 110 additions & 71 deletions frontend/src/scenes/surveys/SurveyView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { IconGraph } from '@posthog/icons'
import { LemonButton, LemonDialog, LemonDivider, Link } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
import { CompareFilter } from 'lib/components/CompareFilter/CompareFilter'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { EditableField } from 'lib/components/EditableField/EditableField'
import { IntervalFilterStandalone } from 'lib/components/IntervalFilter'
import { PageHeader } from 'lib/components/PageHeader'
import { PropertyFilters } from 'lib/components/PropertyFilters/PropertyFilters'
import { TZLabel } from 'lib/components/TZLabel'
Expand Down Expand Up @@ -586,84 +589,120 @@ function SurveyNPSResults({
surveyNPSScore?: string
questionIndex: number
}): JSX.Element {
const { dateRange, interval, compareFilter, defaultInterval } = useValues(surveyLogic)
const { setDateRange, setInterval, setCompareFilter } = useActions(surveyLogic)

return (
<div>
<div className="text-4xl font-bold">{surveyNPSScore}</div>
<div className="mb-2 font-semibold text-secondary">Latest NPS Score</div>
<Query
query={{
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
dateRange: {
date_from: dayjs(survey.created_at).format('YYYY-MM-DD'),
date_to: survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD'),
},
series: [
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Promoters',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['9', '10'],
},
],
},
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Passives',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['7', '8'],
},
],
},
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Detractors',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['0', '1', '2', '3', '4', '5', '6'],
},
],
},
],
properties: [
{
type: PropertyFilterType.Event,
key: '$survey_id',
operator: PropertyOperator.Exact,
value: survey.id,
<div className="space-y-2 bg-surface-primary p-2 rounded">
<div className="flex items-center justify-between gap-2">
<h4 className="text-lg font-semibold">NPS Trend</h4>
<div className="flex items-center gap-2">
<DateFilter
dateFrom={dateRange?.date_from ?? undefined}
dateTo={dateRange?.date_to ?? undefined}
onChange={(fromDate, toDate) =>
setDateRange({
date_from: fromDate,
date_to: toDate,
})
}
/>
<span>grouped by</span>
<IntervalFilterStandalone
interval={interval ?? defaultInterval}
onIntervalChange={setInterval}
options={[
{ value: 'hour', label: 'Hour' },
{ value: 'day', label: 'Day' },
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
]}
/>
<CompareFilter
compareFilter={compareFilter}
updateCompareFilter={(compareFilter) => setCompareFilter(compareFilter)}
/>
</div>
</div>
<Query
query={{
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
interval: interval ?? defaultInterval,
compareFilter: compareFilter,
dateRange: dateRange ?? {
date_from: dayjs(survey.created_at).format('YYYY-MM-DD'),
date_to: survey.end_date
? dayjs(survey.end_date).format('YYYY-MM-DD')
: dayjs().add(1, 'day').format('YYYY-MM-DD'),
},
{
type: PropertyFilterType.Event,
key: '$survey_iteration',
operator: PropertyOperator.Exact,
value: survey.current_iteration,
series: [
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Promoters',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['9', '10'],
},
],
},
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Passives',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['7', '8'],
},
],
},
{
event: SURVEY_EVENT_NAME,
kind: NodeKind.EventsNode,
custom_name: 'Detractors',
properties: [
{
type: PropertyFilterType.Event,
key: getSurveyResponseKey(questionIndex),
operator: PropertyOperator.Exact,
value: ['0', '1', '2', '3', '4', '5', '6'],
},
],
},
],
properties: [
{
type: PropertyFilterType.Event,
key: '$survey_id',
operator: PropertyOperator.Exact,
value: survey.id,
},
{
type: PropertyFilterType.Event,
key: '$survey_iteration',
operator: PropertyOperator.Exact,
value: survey.current_iteration,
},
],
trendsFilter: {
formula: '(A / (A+B+C) * 100) - (C / (A+B+C) * 100)',
display: 'ActionsBar',
},
],
trendsFilter: {
formula: '(A / (A+B+C) * 100) - (C / (A+B+C) * 100)',
display: 'ActionsBar',
},
},
}}
readOnly={true}
/>
}}
/>
</div>
</div>
)
}
75 changes: 75 additions & 0 deletions frontend/src/scenes/surveys/surveyLogic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expectLogic, partial } from 'kea-test-utils'
import { dayjs } from 'lib/dayjs'
import { surveyLogic } from 'scenes/surveys/surveyLogic'

import { useMocks } from '~/mocks/jest'
Expand Down Expand Up @@ -1555,3 +1556,77 @@ describe('survey filters', () => {
})
})
})

describe('surveyLogic', () => {
let logic: ReturnType<typeof surveyLogic.build>

beforeEach(() => {
initKeaTests()
logic = surveyLogic({ id: 'new' })
logic.mount()
})

describe('interval selection', () => {
it('starts with null interval', async () => {
await expectLogic(logic).toMatchValues({
interval: null,
})
})

it('calculates default interval based on survey dates', async () => {
// Test for survey <= 4 weeks old
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(3, 'weeks').format('YYYY-MM-DD'))
}).toMatchValues({
defaultInterval: 'day',
})

// Test for survey <= 12 weeks old
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(10, 'weeks').format('YYYY-MM-DD'))
}).toMatchValues({
defaultInterval: 'week',
})

// Test for survey > 12 weeks old
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(16, 'weeks').format('YYYY-MM-DD'))
}).toMatchValues({
defaultInterval: 'month',
})
})

it('uses start_date over created_at when available', async () => {
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(16, 'weeks').format('YYYY-MM-DD'))
logic.actions.setSurveyValue('start_date', dayjs().subtract(2, 'weeks').format('YYYY-MM-DD'))
}).toMatchValues({
defaultInterval: 'day',
})
})

it('uses end_date when available', async () => {
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(20, 'weeks').format('YYYY-MM-DD'))
logic.actions.setSurveyValue('end_date', dayjs().subtract(3, 'weeks').format('YYYY-MM-DD'))
}).toMatchValues({
defaultInterval: 'month',
})
})

it('allows manual interval override', async () => {
// Set survey dates that would default to 'day'
await expectLogic(logic, () => {
logic.actions.setSurveyValue('created_at', dayjs().subtract(3, 'weeks').format('YYYY-MM-DD'))
})

// Override with manual selection
await expectLogic(logic, () => {
logic.actions.setInterval('month')
}).toMatchValues({
interval: 'month',
defaultInterval: 'day', // Default interval remains unchanged
})
})
})
})
Loading
Loading