Skip to content

Commit

Permalink
feat: web vitals metrics allowlist (#24850)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and timgl committed Sep 10, 2024
1 parent 3fb562c commit 5cd21ea
Show file tree
Hide file tree
Showing 34 changed files with 314 additions and 10 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion frontend/src/scenes/settings/SettingsScene.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryFn } from '@storybook/react'
import { router } from 'kea-router'
import { MOCK_DEFAULT_USER } from 'lib/api.mock'
import { MOCK_DEFAULT_TEAM, MOCK_DEFAULT_USER } from 'lib/api.mock'
import { useEffect } from 'react'
import { App } from 'scenes/App'
import { urls } from 'scenes/urls'
Expand All @@ -27,6 +27,13 @@ const meta: Meta = {
},
'/api/projects/:id/integrations': { results: [] },
},
patch: {
'/api/projects/:id': async (req, res, ctx) => {
// bounce the setting back as is
const newTeamSettings = { ...MOCK_DEFAULT_TEAM, ...(await req.json()) }
return res(ctx.json(newTeamSettings))
},
},
}),
],
}
Expand Down Expand Up @@ -77,6 +84,16 @@ SettingsOrganization.parameters = {
testOptions: { waitForSelector: '.Settings__sections button' },
}

export const SettingsWebVitals: StoryFn = () => {
useEffect(() => {
router.actions.push(urls.settings('project-autocapture', 'web-vitals-autocapture'))
}, [])
return <App />
}
SettingsOrganization.parameters = {
testOptions: { waitForSelector: '.Settings__sections button' },
}

function TimeSensitiveSettings(props: {
has_password?: boolean
saml_available?: boolean
Expand Down
55 changes: 54 additions & 1 deletion frontend/src/scenes/settings/project/AutocaptureSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
import { LemonSwitch, LemonTag, LemonTextArea, Link } from '@posthog/lemon-ui'
import { LemonDivider, LemonSwitch, LemonTag, LemonTextArea, Link } from '@posthog/lemon-ui'
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { PropertyKeyInfo } from 'lib/components/PropertyKeyInfo'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { SupportedWebVitalsMetrics } from 'posthog-js'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'

import { autocaptureExceptionsLogic } from './autocaptureExceptionsLogic'

function WebVitalsAllowedMetricSwitch({ metric }: { metric: SupportedWebVitalsMetrics }): JSX.Element {
const { userLoading } = useValues(userLogic)
const { currentTeam } = useValues(teamLogic)
const { updateCurrentTeam } = useActions(teamLogic)

return (
<LemonSwitch
label={`Capture ${metric}`}
bordered
checked={
currentTeam?.autocapture_web_vitals_allowed_metrics
? currentTeam?.autocapture_web_vitals_allowed_metrics?.includes(metric)
: true
}
disabledReason={
userLoading
? 'Loading user'
: currentTeam?.autocapture_web_vitals_opt_in
? null
: 'Enable web vitals autocapture to set allowed metrics'
}
onChange={(checked) => {
if (!currentTeam) {
// shouldn't ever get here without a team, but we certainly can't edit it if it's not there
return
}

const without = (
currentTeam?.autocapture_web_vitals_allowed_metrics || ['FCP', 'CLS', 'INP', 'LCP']
)?.filter((allowedMetric) => allowedMetric !== metric)
if (checked) {
updateCurrentTeam({
autocapture_web_vitals_allowed_metrics: [...without, metric],
})
} else {
updateCurrentTeam({
autocapture_web_vitals_allowed_metrics: [...without],
})
}
}}
/>
)
}

export function AutocaptureSettings(): JSX.Element {
const { userLoading } = useValues(userLogic)
const { currentTeam } = useValues(teamLogic)
Expand Down Expand Up @@ -131,6 +176,14 @@ export function WebVitalsAutocaptureSettings(): JSX.Element {
}
bordered
/>
<LemonDivider />
<p>You can choose which metrics to capture. By default, we capture all metrics.</p>
<div className="inline-grid grid-cols-2 gap-2 xs:grid xs:w-full">
<WebVitalsAllowedMetricSwitch metric="CLS" />
<WebVitalsAllowedMetricSwitch metric="FCP" />
<WebVitalsAllowedMetricSwitch metric="LCP" />
<WebVitalsAllowedMetricSwitch metric="INP" />
</div>
</>
)
}
5 changes: 5 additions & 0 deletions frontend/src/scenes/teamActivityDescriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ const teamActionsMapping: Record<
autocapture_web_vitals_opt_in(change: ActivityChange | undefined): ChangeMapping | null {
return { description: [<>{change?.after ? 'enabled' : 'disabled'} web vitals autocapture</>] }
},
autocapture_web_vitals_allowed_metrics(change: ActivityChange | undefined): ChangeMapping | null {
const after = change?.after
const metricsList = Array.isArray(after) ? after.join(', ') : 'CLS, FCP, INP, and LCP'
return { description: [<>set allowed web vitals autocapture metrics to {metricsList}</>] }
},
autocapture_opt_out(change: ActivityChange | undefined): ChangeMapping | null {
return { description: [<>{change?.after ? 'opted in to' : 'opted out of'} autocapture</>] }
},
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from 'lib/constants'
import { Dayjs, dayjs } from 'lib/dayjs'
import { PopoverProps } from 'lib/lemon-ui/Popover/Popover'
import type { PostHog } from 'posthog-js'
import type { PostHog, SupportedWebVitalsMetrics } from 'posthog-js'
import { Layout } from 'react-grid-layout'
import { LogLevel } from 'rrweb'
import { BehavioralFilterKey, BehavioralFilterType } from 'scenes/cohorts/CohortFilters/types'
Expand Down Expand Up @@ -489,6 +489,7 @@ export interface TeamType extends TeamBasicType {
session_replay_config: { record_canvas?: boolean; ai_config?: SessionRecordingAIConfig } | undefined | null
autocapture_exceptions_opt_in: boolean
autocapture_web_vitals_opt_in?: boolean
autocapture_web_vitals_allowed_metrics?: SupportedWebVitalsMetrics[]
surveys_opt_in?: boolean
heatmaps_opt_in?: boolean
autocapture_exceptions_errors_to_ignore: string[]
Expand Down
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0016_rolemembership_organization_member
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0466_alter_externaldatasource_source_type
posthog: 0467_add_web_vitals_allowed_metrics
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
4 changes: 4 additions & 0 deletions posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ def get_decide(request: HttpRequest):

capture_network_timing = True if team.capture_performance_opt_in else False
capture_web_vitals = True if team.autocapture_web_vitals_opt_in else False
autocapture_web_vitals_allowed_metrics = None
if capture_web_vitals:
autocapture_web_vitals_allowed_metrics = team.autocapture_web_vitals_allowed_metrics
response["capturePerformance"] = (
{
"network_timing": capture_network_timing,
"web_vitals": capture_web_vitals,
"web_vitals_allowed_metrics": autocapture_web_vitals_allowed_metrics,
}
if capture_network_timing or capture_web_vitals
else False
Expand Down
2 changes: 2 additions & 0 deletions posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Meta:
"autocapture_opt_out",
"autocapture_exceptions_opt_in",
"autocapture_web_vitals_opt_in",
"autocapture_web_vitals_allowed_metrics",
"autocapture_exceptions_errors_to_ignore",
"capture_performance_opt_in",
"capture_console_log_opt_in",
Expand Down Expand Up @@ -143,6 +144,7 @@ class Meta:
"autocapture_opt_out",
"autocapture_exceptions_opt_in",
"autocapture_web_vitals_opt_in",
"autocapture_web_vitals_allowed_metrics",
"autocapture_exceptions_errors_to_ignore",
"capture_console_log_opt_in",
"capture_performance_opt_in",
Expand Down
3 changes: 3 additions & 0 deletions posthog/api/test/__snapshots__/test_action.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -137,6 +138,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -457,6 +459,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
3 changes: 3 additions & 0 deletions posthog/api/test/__snapshots__/test_annotation.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -105,6 +106,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -358,6 +360,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
8 changes: 8 additions & 0 deletions posthog/api/test/__snapshots__/test_decide.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -282,6 +283,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -403,6 +405,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -499,6 +502,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -650,6 +654,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -746,6 +751,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -904,6 +910,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1016,6 +1023,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
2 changes: 2 additions & 0 deletions posthog/api/test/__snapshots__/test_early_access_feature.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -158,6 +159,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
1 change: 1 addition & 0 deletions posthog/api/test/__snapshots__/test_element.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
8 changes: 8 additions & 0 deletions posthog/api/test/__snapshots__/test_feature_flag.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -663,6 +664,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1028,6 +1030,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1168,6 +1171,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1465,6 +1469,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1581,6 +1586,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1651,6 +1657,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down Expand Up @@ -1714,6 +1721,7 @@
"posthog_team"."ingested_event",
"posthog_team"."autocapture_opt_out",
"posthog_team"."autocapture_web_vitals_opt_in",
"posthog_team"."autocapture_web_vitals_allowed_metrics",
"posthog_team"."autocapture_exceptions_opt_in",
"posthog_team"."autocapture_exceptions_errors_to_ignore",
"posthog_team"."session_recording_opt_in",
Expand Down
Loading

0 comments on commit 5cd21ea

Please sign in to comment.