fix(web): scrub invite tokens before analytics

This commit is contained in:
jjbrunton committed 2026-09-06 12:49:32 +01:00
1 parent 49a9cd270f
commit dd1a47ecfa
2 files changed
+48 -2

No files matched your search

+26 -2
View File
@@ -1,9 +1,12 @@
'use client'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import type { CaptureResult } from 'posthog-js'
import { useEffect } from 'react'
const INVITE_PATH_PATTERN = /\/join\/[^/?#\s]+/g
function redactInviteUrl(defaultUrl: string): string {
const url = new URL(defaultUrl)
@@ -14,6 +17,26 @@ function redactInviteUrl(defaultUrl: string): string {
return defaultUrl
}
export function redactInvitePaths<T>(value: T): T {
if (typeof value === 'string') {
return value.replace(INVITE_PATH_PATTERN, '/join/[invite]') as T
}
if (Array.isArray(value)) {
return value.map(redactInvitePaths) as T
}
if (value && typeof value === 'object' && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, redactInvitePaths(entry)])) as T
}
return value
}
export function redactInvitePathsBeforeSend(event: CaptureResult | null): CaptureResult | null {
return event ? redactInvitePaths(event) : null
}
export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
@@ -23,6 +46,7 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
capture_pageview: false,
capture_pageleave: false,
get_current_url: redactInviteUrl,
before_send: redactInvitePathsBeforeSend,
})
}
}, [])