: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
import posthog from 'posthog-js'
export interface RunRef {
taskId?: string
runId?: string
}
/** Shared properties for sandbox-runtime feedback events, so dashboards can split by runtime. */
function feedbackContext(sessionId: string, traceId: string | null, run: RunRef | undefined): Record<string, unknown> {
return {
$ai_session_id: sessionId,
$ai_trace_id: traceId,
ai_product: 'posthog_ai',
agent_runtime: 'sandbox',
task_id: run?.taskId ?? null,
run_id: run?.runId ?? null,
}
}
export function captureTurnRating(
sessionId: string,
traceId: string | null,
rating: 'good' | 'bad',
turnIndex: number,
run?: RunRef
): void {
posthog.capture('$ai_metric', {
$ai_metric_name: 'quality',
$ai_metric_value: rating,
turn_index: turnIndex,
...feedbackContext(sessionId, traceId, run),
})
}
export function captureTurnFeedbackText(
sessionId: string,
traceId: string | null,
feedbackText: string,
turnIndex: number,
run?: RunRef
): void {
posthog.capture('$ai_feedback', {
$ai_feedback_text: feedbackText,
turn_index: turnIndex,
...feedbackContext(sessionId, traceId, run),
})
}
/** Feedback typed as a `/good`, `/bad`, or `/feedback` command. A rating without text is valid, and so is text without a rating. */
export function captureCommandFeedback(
sessionId: string,
traceId: string | null,
rating: 'good' | 'bad' | null,
run: RunRef,
feedbackText?: string
): void {
if (rating) {
posthog.capture('$ai_metric', {
$ai_metric_name: 'feedback',
$ai_metric_value: rating,
feedback_trigger_type: 'slash_command',
turn_index: null,
...feedbackContext(sessionId, traceId, run),
})
}
if (feedbackText) {
posthog.capture('$ai_feedback', {
$ai_feedback_text: feedbackText,
feedback_trigger_type: 'slash_command',
turn_index: null,
...feedbackContext(sessionId, traceId, run),
})
}
}
export type FeedbackPromptRating = 'bad' | 'okay' | 'good' | 'dismissed' | 'implicit_dismiss'
export type FeedbackPromptTrigger = 'message_interval' | 'random_sample' | 'manual' | 'cancel'
/** The periodic prompt's rating (and optional text) — the legacy `captureFeedback` shape plus runtime properties. */
export function capturePromptFeedback(
sessionId: string,
traceId: string | null,
rating: FeedbackPromptRating,
trigger: FeedbackPromptTrigger,
turnIndex: number | null,
run?: RunRef,
feedbackText?: string
): void {
posthog.capture('$ai_metric', {
$ai_metric_name: 'feedback',
$ai_metric_value: rating,
feedback_trigger_type: trigger,
turn_index: turnIndex,
...feedbackContext(sessionId, traceId, run),
})
if (feedbackText) {
posthog.capture('$ai_feedback', {
$ai_feedback_text: feedbackText,
turn_index: turnIndex,
...feedbackContext(sessionId, traceId, run),
})
}
}