Skip to content

: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.

fix(canvas): tag canvas queries with the canvas product (#117911)

Sam Pennington563ed1c10555
diff --git a/posthog/clickhouse/query_tagging.py b/posthog/clickhouse/query_tagging.py
index 15b4e145645b..42c74bc82f9d 100644
--- a/posthog/clickhouse/query_tagging.py
+++ b/posthog/clickhouse/query_tagging.py
@@ -50,6 +50,7 @@ class Product(StrEnum):
API = "api"
AUTORESEARCH = "autoresearch"
BATCH_EXPORT = "batch_export"
+ CANVAS = "canvas"
COHORTS = "cohorts"
CONVERSATIONS = "conversations"
CUSTOMER_ANALYTICS = "customer_analytics"
diff --git a/products/canvas/frontend/host/canvasDataBridge.test.ts b/products/canvas/frontend/host/canvasDataBridge.test.ts
index 6817a1dda30a..069cb68fb8bb 100644
--- a/products/canvas/frontend/host/canvasDataBridge.test.ts
+++ b/products/canvas/frontend/host/canvasDataBridge.test.ts
@@ -38,15 +38,37 @@ describe('CanvasDataBridge query routing', () => {
expect(api.query).not.toHaveBeenCalled()
})
+ const tags = { productKey: 'canvas', scene: 'Canvas' }
+
test.each([
- { query: { kind: 'TrendsQuery', series: [{ kind: 'EventsNode', event: '$pageview' }] } },
- { query: { kind: 'HogQLQuery', query: 'SELECT 1' } },
- { hogql: 'SELECT 1' },
- ])('runs a supported query: %j', async (input) => {
+ {
+ input: { query: { kind: 'TrendsQuery', series: [{ kind: 'EventsNode', event: '$pageview' }] } },
+ sent: { kind: 'TrendsQuery', series: [{ kind: 'EventsNode', event: '$pageview' }], tags },
+ },
+ {
+ input: { query: { kind: 'HogQLQuery', query: 'SELECT 1', tags: { productKey: 'spoofed', name: 'n' } } },
+ sent: { kind: 'HogQLQuery', query: 'SELECT 1', tags: { ...tags, name: 'n' } },
+ },
+ {
+ input: { query: { kind: 'DataTableNode', source: { kind: 'HogQLQuery', query: 'SELECT 1' } } },
+ sent: { kind: 'DataTableNode', source: { kind: 'HogQLQuery', query: 'SELECT 1', tags }, tags },
+ },
+ {
+ input: { query: { kind: 'InsightVizNode', source: { kind: 'TrendsQuery', series: [] } } },
+ sent: { kind: 'InsightVizNode', source: { kind: 'TrendsQuery', series: [], tags } },
+ },
+ {
+ input: { query: { kind: 'InsightActorsQuery', source: { kind: 'TrendsQuery', series: [] } } },
+ sent: { kind: 'InsightActorsQuery', source: { kind: 'TrendsQuery', series: [], tags }, tags },
+ },
+ {
+ input: { query: { kind: 'WebAnalyticsExternalSummaryQuery', dateRange: {} } },
+ sent: { kind: 'WebAnalyticsExternalSummaryQuery', dateRange: {} },
+ },
+ { input: { hogql: 'SELECT 1' }, sent: { kind: 'HogQLQuery', query: 'SELECT 1', tags } },
+ ])('runs a supported query tagged as canvas: %j', async ({ input, sent }) => {
await expect(bridge().handle('query', input)).resolves.toEqual({ results: [], columns: [] })
- expect(api.query).toHaveBeenCalledWith(input.query ?? { kind: 'HogQLQuery', query: input.hogql }, {
- refresh: 'blocking',
- })
+ expect(api.query).toHaveBeenCalledWith(sent, { refresh: 'blocking' })
})
})
diff --git a/products/canvas/frontend/host/canvasDataBridge.ts b/products/canvas/frontend/host/canvasDataBridge.ts
index 87dbb21ddc64..05c33bfe0098 100644
--- a/products/canvas/frontend/host/canvasDataBridge.ts
+++ b/products/canvas/frontend/host/canvasDataBridge.ts
@@ -33,6 +33,7 @@ const DEFAULT_READ_TTL_SECONDS = 5 * 60
const CONNECTOR_READ_TTL_SECONDS = 60
const MAX_STATE_PAGES = 20
const canvasQuerySchema = z.object({ kind: z.nativeEnum(NodeKind) }).passthrough()
+const CANVAS_QUERY_TAGS = { productKey: 'canvas', scene: 'Canvas' }
// pinned: `$lib` value on events a canvas captures, shared with PostHog Desktop
const CANVAS_CAPTURE_LIB = 'posthog-canvas'
@@ -73,7 +74,32 @@ interface CanvasDataResult {
insight?: { name: string | null; kind: string | null; display: string | null }
}
-type QueryNode = { kind?: string; source?: QueryNode | null; variables?: Record<string, unknown> | null }
+type QueryNode = {
+ kind?: string
+ source?: QueryNode | null
+ variables?: Record<string, unknown> | null
+ tags?: Record<string, unknown> | null
+}
+
+// The backend schemas of these kinds have no `tags` field and forbid extra fields, so a tag fails validation.
+const UNTAGGABLE_QUERY_KINDS = new Set<string>([
+ NodeKind.InsightVizNode,
+ NodeKind.DataVisualizationNode,
+ NodeKind.BIVisualizationNode,
+ NodeKind.InsightActorsQueryOptions,
+ NodeKind.FunnelCorrelationQuery,
+ NodeKind.WebAnalyticsExternalSummaryQuery,
+])
+
+// Tag every node in the source chain, because a runner like InsightActorsQuery reads its own tags
+// and does not run its source. The canvas cannot override attribution, so its own tags lose to ours.
+function withCanvasQueryTags<T extends QueryNode>(node: T): T {
+ const tagged =
+ node.kind && UNTAGGABLE_QUERY_KINDS.has(node.kind)
+ ? node
+ : { ...node, tags: { ...node.tags, ...CANVAS_QUERY_TAGS } }
+ return tagged.source?.kind ? { ...tagged, source: withCanvasQueryTags(tagged.source) } : tagged
+}
function refreshSeconds(value: unknown): number | undefined {
if (value === undefined) {
@@ -182,7 +208,9 @@ export class CanvasDataBridge {
throw new Error('ph.query parameters are not supported. Use a typed query with variables instead.')
}
const refresh = refreshSeconds(input.refresh)
- const node = canvasQuerySchema.parse(typed ? input.query : { kind: NodeKind.HogQLQuery, query: hogql })
+ const node = withCanvasQueryTags(
+ canvasQuerySchema.parse(typed ? input.query : { kind: NodeKind.HogQLQuery, query: hogql })
+ )
return this.cache.read(
'query',
{ node, params: input.params },