chore: Add org slug and project ref as tags to the Sentry data (#47442)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Enhanced telemetry to include the active project reference and
selected organization on events.
* Cleared and re-applied project and organization tags when navigating
to prevent stale tagging.
* Only applies user identification when user account details are
available.
* **Tests**
* Updated telemetry tests to cover the additional tagging behavior by
extending Sentry mocks accordingly.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Ivan Vasilov authored and GitHub committed 2026-07-02 11:47:13 +02:00
1 parent 450dc56cbe
commit fae2c5b96e
2 files changed
+15 -5

No files matched your search

+3
View File
@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({
useUser: vi.fn(),
useOrganizationsQuery: vi.fn(),
setUser: vi.fn(),
setTag: vi.fn(),
}))
vi.mock('common', async (importOriginal) => {
@@ -36,6 +37,7 @@ vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
vi.mock('@sentry/nextjs', () => ({
setUser: (...args: unknown[]) => mocks.setUser(...args),
setTag: (...args: unknown[]) => mocks.setTag(...args),
}))
const USER_ID = 'user-abc-123'
@@ -50,6 +52,7 @@ describe('Telemetry — posthog identify mirroring', () => {
mocks.useUser.mockReset()
mocks.useOrganizationsQuery.mockReset()
mocks.setUser.mockReset()
mocks.setTag.mockReset()
})
it('fires identify with both org_count and signup_timestamp when user and orgs are loaded', async () => {
+12 -5
View File
@@ -1,5 +1,5 @@
import * as Sentry from '@sentry/nextjs'
import { PageTelemetry, posthogClient, useUser } from 'common'
import { PageTelemetry, posthogClient, useParams, useUser } from 'common'
import { useEffect, useRef } from 'react'
import { useConsentToast } from 'ui-patterns/consent'
@@ -19,6 +19,9 @@ export function Telemetry() {
const user = useUser()
// Project ref from the URL params, mirroring the backend's `request.params.ref`
const { ref: projectRef } = useParams()
// Mirror the user's org-list length into a PostHog person property so feature
// flags and analytics can segment by current org membership. signup_timestamp
// is set on the same identify so flag audiences requiring both properties see
@@ -50,12 +53,16 @@ export function Telemetry() {
useEffect(() => {
// don't set the sentry user id if the user hasn't logged in (so that Sentry errors show null user id instead of anonymous id)
if (!user?.id) {
return
if (user?.id) {
Sentry.setUser({ id: user.id })
}
Sentry.setUser({ id: user.id })
}, [user?.id])
// Tag Sentry events with the current project ref and customer org slug so backend/
// frontend errors can be filtered by project / org. Passing a null value clears
// the tag, so stale values don't leak across navigation.
Sentry.setTag('project_ref', projectRef ?? null)
Sentry.setTag('org_slug', organization?.slug ?? null)
}, [user?.id, projectRef, organization?.slug])
return (
<PageTelemetry