: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
feat(user_interviews): enable pageviews + replay on the public interview page (#60158)
Paul D'Ambrac51a4c4e036c
diff --git a/frontend/src/exporter/index.tsx b/frontend/src/exporter/index.tsxindex abd191a72c0f..2fcf6458d735 100644--- a/frontend/src/exporter/index.tsx+++ b/frontend/src/exporter/index.tsx@@ -2,21 +2,64 @@ import '~/styles'import './Exporter.scss'import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'+import { BeforeSendFn, CapturedNetworkRequest } from 'posthog-js'import { createRoot } from 'react-dom/client'import { Exporter } from '~/exporter/Exporter'-import { ExportedData } from '~/exporter/types'+import { ExportType, ExportedData } from '~/exporter/types'import { initKea } from '~/initKea'import { loadPostHogJS } from '~/loadPostHogJS'import { ErrorBoundary } from '../layout/ErrorBoundary'-// Disable tracking for all exports and embeds.-// This is explicitly set as to not track our customers' customers data.-// Without it, embeds of self-hosted iframes will log metrics to app.posthog.com.-window.JS_POSTHOG_API_KEY = undefined+const exportedData: ExportedData = window.POSTHOG_EXPORTED_DATA++// Disable tracking for shared dashboards / insights / embeds — those iframes can be embedded on+// our customers' sites, and tracking there would log their visitors to app.posthog.com.+// The `interview` scene is the exception: it's our own hosted public page, opened by an invitee+// on a link we sent. We want pageviews + replay to measure the invite-to-call funnel.+// Require the interview payload to be present — the public `force_type=interview` query param+// can flip `type` on any shared resource, so checking `type` alone would let anyone enable+// tracking on a customer dashboard share by appending the parameter.+const isInterview = exportedData?.type === ExportType.Interview && !!exportedData?.interview+if (!isInterview) {+ window.JS_POSTHOG_API_KEY = undefined+}++// The interview URL embeds the SharingConfiguration access token (/interview/<token>/) and the+// public start_call API path (/api/user_interviews/share/<token>/start_call/) embeds it too.+// Two hooks redact it everywhere a viewer of analytics or replay could otherwise read it:+// - `before_send` strips URL-shaped event properties ($current_url, $pathname, $referrer, ...)+// - `maskCapturedNetworkRequestFn` is also the hook posthog-js uses for ALL replay URL+// surfaces — captured network requests, the rrweb `EventType.Meta` header, `$url_changed`+// custom events on SPA route transitions, and masked $current_url in captured console events.+// Together they cover every surface where the token could land for a viewer to reuse.+const INTERVIEW_TOKEN_RE = /\/interview\/[^/?#]+|\/api\/user_interviews\/share\/[^/?#]+/g+const URL_PROPERTIES = ['$current_url', '$pathname', '$referrer', '$initial_current_url', '$initial_pathname']+const redactInterviewToken = (value: string): string =>+ value.replace(INTERVIEW_TOKEN_RE, (match) => match.replace(/\/[^/?#]+$/, '/<redacted>'))+const interviewBeforeSend: BeforeSendFn = (event) => {+ if (event?.properties) {+ for (const key of URL_PROPERTIES) {+ const value = event.properties[key]+ if (typeof value === 'string') {+ event.properties[key] = redactInterviewToken(value)+ }+ }+ }+ return event+}+const interviewMaskNetworkRequest = (req: CapturedNetworkRequest): CapturedNetworkRequest => {+ if (req.name) {+ req.name = redactInterviewToken(req.name)+ }+ return req+}-loadPostHogJS()+loadPostHogJS({+ beforeSend: isInterview ? interviewBeforeSend : undefined,+ sessionRecording: isInterview ? { maskCapturedNetworkRequestFn: interviewMaskNetworkRequest } : undefined,+})initKea({ replaceInitialPathInWindow: false })// On Chrome + Windows, the country flag emojis don't render correctly. This is a polyfill for that.@@ -26,8 +69,6 @@ initKea({ replaceInitialPathInWindow: false })// Make sure to update the font family in the CSS if you change this.polyfillCountryFlagEmojis('Emoji Flags Polyfill')-const exportedData: ExportedData = window.POSTHOG_EXPORTED_DATA-function renderApp(): void {const root = document.getElementById('root')if (root) {diff --git a/frontend/src/loadPostHogJS.tsx b/frontend/src/loadPostHogJS.tsxindex a2c44f96dc67..ca4f17a658a5 100644--- a/frontend/src/loadPostHogJS.tsx+++ b/frontend/src/loadPostHogJS.tsx@@ -1,4 +1,4 @@-import posthog, { PostHogInterface } from 'posthog-js'+import posthog, { BeforeSendFn, PostHogInterface, SessionRecordingOptions } from 'posthog-js'import { sampleOnProperty } from 'posthog-js/lib/src/extensions/sampling'import { FEATURE_FLAGS } from 'lib/constants'@@ -21,7 +21,21 @@ const shouldTrackFramerate = (loadedInstance: PostHogInterface): boolean => {)}-export function loadPostHogJS(): void {+export interface LoadPostHogJSOptions {+ /**+ * Hook posthog-js's `before_send` so the caller can mutate or drop events before they leave+ * the browser. Used by the exporter app to redact the SharingConfiguration access token from+ * URL-shaped properties on the interview share page — see `frontend/src/exporter/index.tsx`.+ */+ beforeSend?: BeforeSendFn | BeforeSendFn[]+ /**+ * Extra `session_recording` config merged on top of the defaults — useful for overriding URL+ * / network-payload masking when the page renders sensitive bearer tokens in its own URL.+ */+ sessionRecording?: Partial<SessionRecordingOptions>+}++export function loadPostHogJS(options: LoadPostHogJSOptions = {}): void {if (window.JS_POSTHOG_API_KEY) {posthog.init(window.JS_POSTHOG_API_KEY, {opt_out_useragent_filter: window.location.hostname === 'localhost', // we ARE a bot when running in localhost, so we need to enable this opt-out@@ -41,6 +55,7 @@ export function loadPostHogJS(): void {error_tracking: {__capturePostHogExceptions: true,},+ before_send: options.beforeSend,loaded: (loadedInstance) => {if (loadedInstance.sessionRecording) {loadedInstance.sessionRecording._forceAllowLocalhostNetworkCapture = true@@ -143,6 +158,7 @@ export function loadPostHogJS(): void {},session_recording: {blockSelector: '.ph-replay-block',+ ...options.sessionRecording,},person_profiles: 'always',__add_tracing_headers: ['eu.posthog.com', 'us.posthog.com'],