:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(web-analytics): skip the error tracking query until error tracking is set up (#122794)
Paul D'Ambraf2718c91e3b5
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex a4beb1cd1a40..998ea1338d06 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -17068,3 +17068,19 @@ snapshots:hash: v1.k794b7964.bb428bce34a71ce86eb2bcbd242891c30b2587e4c2832312ed187858dc0e557e.2--iSbo0szpR2wW_Mm-t-0xlj7Km8lZLqTQdnyykPuIweb-analytics-tiles--world-map--light:hash: v1.k794b7964.a629f8ad749c539453f0c4337a0b6455a43da6b4c341316ced9b0d86ca6f73f7.4CHgm0-R2GWY6RT7zyDspwHgOFpsPLrIbCNDQ01J2nY+ web-analytics-tiles-error-tracking--not-set-up--dark:+ hash: v1.k794b7964.53f0a34611e55eb4e923e0d894043c7fc852b2aaef6816be6ab99c77ccf79b2c.UbdogbF1ZBYNre485tN8W05N0-VtQZPfCOnnTNngq8E+ web-analytics-tiles-error-tracking--not-set-up--light:+ hash: v1.k794b7964.6193bb1b633db917bee11faa0eebac938e19755de5d91312aa3a13c0ea87163c.jb1HbGLHwmEpriROVA00XGU4J5n_WttzZAGh-E0U1-o+ web-analytics-tiles-error-tracking--not-set-up-narrow--dark:+ hash: v1.k794b7964.c5c64d045020762dcc06023afcf592c663290b7168299b0705c244a73054f5b2.ahwlWMaexNNNO2nv6PirypMVmsugnChbMkC7S7tG-Eg+ web-analytics-tiles-error-tracking--not-set-up-narrow--light:+ hash: v1.k794b7964.33c3f9c3100efbc3a753c0d9597d988b52e73301095e885cce416e10080e7847.xQ8Byuqi5DwvPK1cPci52Xo5bMXTOdiDRmbacac2Xqg+ web-analytics-tiles-error-tracking--set-up-without-issues--dark:+ hash: v1.k794b7964.829735547464ad4b79e99d7db52b1df1f52477f6a0563c2f04634cf4a17c03bc.kX_OdA2gNq3VvbskBQVLrBEaPkobBqwd0gRdTp1mqBs+ web-analytics-tiles-error-tracking--set-up-without-issues--light:+ hash: v1.k794b7964.d044deb7a3511aa4e083e3caf69f062ced9a5fe9bd0ae10a2b867e09ae0011f8.Biupi7mp3e0pq_U7SIxkbkzrUEGp6vcvXsgPVFUB3Zo+ web-analytics-tiles-error-tracking--with-issues--dark:+ hash: v1.k794b7964.a2c7bef258943e6c214711ab0c75a62d5c15d9e3f18577ff253a5b9e52b95372.pYsHHvyjE6Bc_wJfelJBpUeH6Lp0avzXmuxHbHuICVY+ web-analytics-tiles-error-tracking--with-issues--light:+ hash: v1.k794b7964.8554a239ce82ed32f3ebf2fc8e003ee69f55a1453194f015e05b769752431344._admxEVKJsw0JzvS4lvRF9RByxBjNWXGELL56UB1764diff --git a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.stories.tsx b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.stories.tsxnew file mode 100644index 000000000000..3ea9bfd23d87--- /dev/null+++ b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.stories.tsx@@ -0,0 +1,108 @@+import type { Meta, StoryObj } from '@storybook/react'++import {+ errorTrackingQueryResponse,+ errorTrackingTypeIssue,+} from '@posthog/products-error-tracking/frontend/__mocks__/error_tracking_query'+import { errorTrackingQuery } from '@posthog/products-error-tracking/frontend/queries'++import { ErrorTrackingTile, TileId } from 'scenes/web-analytics/common'++import { mswDecorator } from '~/mocks/browser'+import { ErrorTrackingQueryResponse } from '~/queries/schema/schema-general'+import { FilterLogicalOperator } from '~/types'++import { WebAnalyticsErrorTrackingTile } from './WebAnalyticsErrorTracking'++const tile: ErrorTrackingTile = {+ kind: 'error_tracking',+ tileId: TileId.ERROR_TRACKING,+ layout: {},+ query: errorTrackingQuery({+ orderBy: 'users',+ dateRange: { date_from: '-7d', date_to: null },+ filterTestAccounts: false,+ filterGroup: { type: FilterLogicalOperator.And, values: [] },+ columns: ['error', 'users', 'occurrences', 'last_seen'],+ limit: 4,+ }),+}++const ISSUES: { name: string; description: string; users: number; occurrences: number }[] = [+ {+ name: 'TypeError',+ description: "Cannot read properties of undefined (reading 'onLCP')",+ users: 42,+ occurrences: 310,+ },+ {+ name: 'Non-OK response',+ description: 'The billing request returned an unsuccessful response.',+ users: 17,+ occurrences: 96,+ },+ { name: 'ChunkLoadError', description: 'Loading chunk 482 failed.', users: 5, occurrences: 12 },+]++const ISSUES_RESPONSE: ErrorTrackingQueryResponse = {+ ...errorTrackingQueryResponse,+ results: ISSUES.map(({ name, description, users, occurrences }, index) => ({+ ...errorTrackingQueryResponse.results[0],+ ...errorTrackingTypeIssue,+ id: `01890a1b-2c3d-4e4f-8a9b-0c1d2e3f4a${index}0`,+ name,+ description,+ last_seen: '2023-01-31T12:00:00Z',+ aggregations: { ...errorTrackingQueryResponse.results[0].aggregations!, users, occurrences },+ })),+}++const meta: Meta = {+ title: 'Web Analytics/Tiles/Error tracking',+ parameters: {+ layout: 'padded',+ viewMode: 'story',+ mockDate: '2023-02-01',+ testOptions: { waitForLoadersToDisappear: true },+ },+ render: (_, { parameters }) => (+ <div className={parameters.tileWidthClass ?? 'w-[36rem]'}>+ <WebAnalyticsErrorTrackingTile tile={tile} />+ </div>+ ),+}+export default meta++type Story = StoryObj++export const NotSetUp: Story = {+ decorators: [+ mswDecorator({+ get: { '/api/environments/:team_id/error_tracking/issues/exists/': [200, { exists: false }] },+ post: { '/api/environments/:team_id/query/:kind/': () => new Promise<never>(() => {}) },+ }),+ ],+}++export const NotSetUpNarrow: Story = {+ ...NotSetUp,+ parameters: { tileWidthClass: 'w-80' },+}++export const WithIssues: Story = {+ decorators: [+ mswDecorator({+ get: { '/api/environments/:team_id/error_tracking/issues/exists/': [200, { exists: true }] },+ post: { '/api/environments/:team_id/query/:kind/': [200, ISSUES_RESPONSE] },+ }),+ ],+}++export const SetUpWithoutIssues: Story = {+ decorators: [+ mswDecorator({+ get: { '/api/environments/:team_id/error_tracking/issues/exists/': [200, { exists: true }] },+ post: { '/api/environments/:team_id/query/:kind/': [200, { ...errorTrackingQueryResponse, results: [] }] },+ }),+ ],+}diff --git a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.test.tsx b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.test.tsxindex 629f072ab0ec..b3f07f58de70 100644--- a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.test.tsx+++ b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.test.tsx@@ -6,13 +6,10 @@ import { cleanup, render, screen, waitFor } from '@testing-library/react'import { errorTrackingQuery } from '@posthog/products-error-tracking/frontend/queries'-import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'-import { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'import { teamLogic } from 'scenes/teamLogic'import { ErrorTrackingTile, TileId } from 'scenes/web-analytics/common'import { useMocks } from '~/mocks/jest'-import { ProductKey } from '~/queries/schema/schema-general'import { initKeaTests } from '~/test/init'import { FilterLogicalOperator } from '~/types'@@ -35,59 +32,62 @@ describe('WebAnalyticsErrorTrackingTile', () => {beforeEach(() => {initKeaTests()- useMocks({- post: {- '/api/environments/:team_id/query/': { results: [] },- },- })})afterEach(() => {cleanup()})- const mountWithSetupStatus = (autocaptureOptIn: boolean, setupStatus: ProductSetupStatus | null): void => {- teamLogic.mount()- teamLogic.actions.loadCurrentTeamSuccess({- ...MOCK_DEFAULT_TEAM,- autocapture_exceptions_opt_in: autocaptureOptIn,- })- const setup = productSetupStatusLogic({ productKey: ProductKey.ERROR_TRACKING })- setup.mount()- if (setupStatus) {- setup.actions.setDetectedStatus(setupStatus)- }- }-it.each([{name: 'good news when exceptions already reach the project',autocaptureOptIn: false,- setupStatus: 'has-data' as const,+ issuesExistResponse: [200, { exists: true }] as [number, object],expected: 'No errors found!',+ queryRuns: true,},{name: 'good news when autocapture is on but nothing has errored yet',autocaptureOptIn: true,- setupStatus: 'needs-setup' as const,+ issuesExistResponse: [200, { exists: false }] as [number, object],expected: 'No errors found!',+ queryRuns: true,},{- name: 'a nudge when nothing reports errors, so a missing SDK does not read as no errors',+ name: 'an upsell without running the query when nothing reports errors',autocaptureOptIn: false,- setupStatus: 'needs-setup' as const,+ issuesExistResponse: [200, { exists: false }] as [number, object],expected: 'Error tracking is not set up',+ queryRuns: false,},{- name: 'the generic copy while setup detection has not answered',+ name: 'the generic copy when setup detection fails',autocaptureOptIn: false,- setupStatus: null,+ issuesExistResponse: [500, {}] as [number, object],expected: 'There are no matching events for this query',+ queryRuns: true,},- ])('reads an empty table as $name', async ({ autocaptureOptIn, setupStatus, expected }) => {- mountWithSetupStatus(autocaptureOptIn, setupStatus)+ ])('shows $name', async ({ autocaptureOptIn, issuesExistResponse, expected, queryRuns }) => {+ let queryRequests = 0+ useMocks({+ get: {+ '/api/environments/:team_id/error_tracking/issues/exists/': issuesExistResponse,+ },+ post: {+ '/api/environments/:team_id/query/:kind/': () => {+ queryRequests += 1+ return [200, { results: [] }]+ },+ },+ })+ teamLogic.mount()+ teamLogic.actions.loadCurrentTeamSuccess({+ ...MOCK_DEFAULT_TEAM,+ autocapture_exceptions_opt_in: autocaptureOptIn,+ })render(<WebAnalyticsErrorTrackingTile tile={tile} />)await waitFor(() => expect(screen.getByText(expected)).toBeInTheDocument())+ expect(queryRequests > 0).toBe(queryRuns)})})diff --git a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.tsx b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.tsxindex 361963cfc027..3c00d08467a2 100644--- a/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.tsx+++ b/frontend/src/scenes/web-analytics/tiles/WebAnalyticsErrorTracking.tsx@@ -1,16 +1,19 @@import clsx from 'clsx'-import { useActions, useValues } from 'kea'-import { useCallback, useMemo } from 'react'+import { useActions, useMountedLogic, useValues } from 'kea'+import { useCallback } from 'react'import * as panicPng from '@posthog/brand/hoggies/png/panic'import * as starPng from '@posthog/brand/hoggies/png/star'+import { errorTrackingSetupLogic } from '@posthog/products-error-tracking/frontend/emptyState/errorTrackingSetupLogic'import { pngHoggie } from 'lib/brand/hoggies'import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'+import { ProductIntroduction } from 'lib/components/ProductIntroduction/ProductIntroduction'import { TZLabel } from 'lib/components/TZLabel'import { FEATURE_FLAGS } from 'lib/constants'import { IconOpenInNew } from 'lib/lemon-ui/icons'import { LemonButton } from 'lib/lemon-ui/LemonButton'+import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'import { LemonTableLink } from 'lib/lemon-ui/LemonTable/LemonTableLink'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { humanFriendlyLargeNumber } from 'lib/utils/numbers'@@ -54,7 +57,7 @@ const CountColumn = ({ record, columnName }: { record: unknown; columnName: strireturn <>{humanFriendlyLargeNumber(count)}</>}-const baseContext: QueryContext = {+const issuesContext: QueryContext = {extraDataTableQueryFeatures: [QueryFeature.hideLoadNextButton],showOpenEditorButton: false,showQueryEditor: false,@@ -79,21 +82,24 @@ const baseContext: QueryContext = {},}+const emptyIssuesContext: QueryContext = {+ ...issuesContext,+ emptyStateIcon: <HedgehogStar className="w-32 mb-2" />,+ emptyStateHeading: 'No errors found!',+ emptyStateDetail: 'Keep up the great work!',+}+export const WebAnalyticsErrorTrackingTile = ({ tile }: { tile: ErrorTrackingTile }): JSX.Element => {const { layout, query } = tileconst to = urls.errorTracking()const { addProductIntentForCrossSell } = useActions(teamLogic)- const { currentTeam } = useValues(teamLogic)const { featureFlags } = useValues(featureFlagLogic)+ useMountedLogic(errorTrackingSetupLogic)const { status: errorTrackingStatus } = useValues(productSetupStatusLogic({ productKey: ProductKey.ERROR_TRACKING }))const useTileHeaderV2 = featureFlags[FEATURE_FLAGS.WEB_ANALYTICS_TILE_HEADER_V2] === 'test'- // An empty table only means good news when exceptions do reach this project: either- // some have already been grouped into issues, or autocapture is on and will send them.- const errorsAreReported = errorTrackingStatus === 'has-data' || !!currentTeam?.autocapture_exceptions_opt_in-const crossSellToErrorTracking = useCallback((): void => {addProductIntentForCrossSell({from: ProductKey.WEB_ANALYTICS,@@ -102,50 +108,51 @@ export const WebAnalyticsErrorTrackingTile = ({ tile }: { tile: ErrorTrackingTil})}, [addProductIntentForCrossSell])- const context = useMemo((): QueryContext => {- if (errorsAreReported) {- return {- ...baseContext,- emptyStateIcon: <HedgehogStar className="w-32 mb-2" />,- emptyStateHeading: 'No errors found!',- emptyStateDetail: 'Keep up the great work!',- }- }- if (errorTrackingStatus === 'needs-setup') {- return {- ...baseContext,- emptyStateIcon: <HedgehogPanic className="w-32 mb-2" />,- emptyStateHeading: 'Error tracking is not set up',- emptyStateDetail: (- <>- See the exceptions your visitors hit, grouped by issue and linked to the session they happened- in.- <span className="mt-2 flex justify-center">- <LemonButton- to={to}- onClick={crossSellToErrorTracking}- size="small"- type="primary"- data-attr="web-analytics-error-tracking-setup"- >- Set up error tracking- </LemonButton>- </span>- </>- ),- }- }- // Detection is still loading, or it failed. Neither answer is safe to act on, so the- // generic copy stands: it promises nothing about whether errors are being reported.- return baseContext- }, [errorsAreReported, errorTrackingStatus, to, crossSellToErrorTracking])-const viewAllButton = (<LemonButton to={to} icon={<IconOpenInNew />} onClick={crossSellToErrorTracking} size="small" type="secondary">View all</LemonButton>)+ // Without error tracking set up, an empty table says nothing, so skip the query and pitch the product.+ // While detection is loading the query also waits, so a project without it never pays for the request.+ // If detection fails (`unknown`) the query runs with generic copy: that makes no claim about setup.+ const needsSetup = errorTrackingStatus === 'needs-setup'+ const body = needsSetup ? (+ <ProductIntroduction+ thingName="error tracking"+ titleOverride="Error tracking is not set up"+ description="See the exceptions your visitors hit, grouped by issue and linked to the session they happened in."+ actionElementOverride={+ <LemonButton+ to={to}+ onClick={crossSellToErrorTracking}+ type="primary"+ data-attr="web-analytics-error-tracking-setup"+ >+ Set up error tracking+ </LemonButton>+ }+ customHog={HedgehogPanic}+ hogLayout="vertical"+ hogClassName="w-24 sm:w-24 lg:w-24 mb-2"+ className="my-0 p-4"+ />+ ) : errorTrackingStatus === 'loading' ? (+ <LemonSkeleton className="h-40 m-2" />+ ) : (+ <Query+ attachTo={webAnalyticsLogic}+ query={query}+ embedded={true}+ context={+ errorTrackingStatus === 'has-data' || errorTrackingStatus === 'waiting-for-data'+ ? emptyIssuesContext+ : issuesContext+ }+ />+ )+return (<divclassName={clsx(@@ -160,17 +167,15 @@ export const WebAnalyticsErrorTrackingTile = ({ tile }: { tile: ErrorTrackingTil<div className="border rounded bg-surface-primary flex-1 flex flex-col py-2 px-1"><div className="flex flex-row items-center self-stretch gap-2 min-h-10 px-3 py-2"><h2 className="flex-1 m-0 text-base font-semibold">Error tracking</h2>- {viewAllButton}+ {!needsSetup && viewAllButton}</div>- <Query attachTo={webAnalyticsLogic} query={query} embedded={true} context={context} />+ {body}</div>) : (<><h2 className="m-0 mb-3">Error tracking</h2>- <div className="border rounded bg-surface-primary flex-1 flex flex-col py-2 px-1">- <Query attachTo={webAnalyticsLogic} query={query} embedded={true} context={context} />- </div>- <div className="flex flex-row-reverse my-2">{viewAllButton}</div>+ <div className="border rounded bg-surface-primary flex-1 flex flex-col py-2 px-1">{body}</div>+ {!needsSetup && <div className="flex flex-row-reverse my-2">{viewAllButton}</div>}</>)}</div>