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(exports): show ai report chart titles in images (#90179)

Vasco de Krijger70b07bb512ac
diff --git a/frontend/src/exporter/Exporter.tsx b/frontend/src/exporter/Exporter.tsx
index 746e81763f4c..ded69d12bbb5 100644
--- a/frontend/src/exporter/Exporter.tsx
+++ b/frontend/src/exporter/Exporter.tsx
@@ -83,6 +83,7 @@ export function Exporter(props: ExportedData): JSX.Element {
inline_query_results: inlineQueryResults,
query,
query_results: queryResults,
+ query_title: queryTitle,
themes,
accessToken,
exportToken,
@@ -226,6 +227,7 @@ export function Exporter(props: ExportedData): JSX.Element {
<LazyQueryScene
query={query}
queryResults={queryResults}
+ title={queryTitle}
themes={themes!}
exportOptions={exportOptions}
/>
diff --git a/frontend/src/exporter/scenes/ExporterQueryScene.test.tsx b/frontend/src/exporter/scenes/ExporterQueryScene.test.tsx
index d9ea6341f81a..ceb51148249b 100644
--- a/frontend/src/exporter/scenes/ExporterQueryScene.test.tsx
+++ b/frontend/src/exporter/scenes/ExporterQueryScene.test.tsx
@@ -2,7 +2,7 @@ import { MOCK_DATA_COLOR_THEMES } from 'lib/api.mock'
import '@testing-library/jest-dom'
-import { cleanup, waitFor } from '@testing-library/react'
+import { cleanup, screen, waitFor } from '@testing-library/react'
import { setupJsdom, setupSyncRaf } from '@posthog/quill-charts/testing'
@@ -27,12 +27,13 @@ afterEach(() => {
})
describe('ExporterQueryScene', () => {
- function renderAdhoc(fixture: any, legend = false): { container: HTMLElement } {
+ function renderAdhoc(fixture: any, legend = false, title?: string): { container: HTMLElement } {
return renderWithInsights({
component: (
<ExporterQueryScene
query={fixture.query}
queryResults={{ results: fixture.result }}
+ title={title}
themes={MOCK_DATA_COLOR_THEMES}
exportOptions={{ legend }}
/>
@@ -53,6 +54,12 @@ describe('ExporterQueryScene', () => {
})
})
+ it('renders the supplied title inside the exported chart card', () => {
+ renderAdhoc(__trendsBarBreakdown, false, 'Weekly signups')
+
+ expect(screen.getByText('Weekly signups')).toHaveClass('ExportedInsight__header__title')
+ })
+
it('lets the chart draw the quill in-chart legend, like a saved-insight export', async () => {
const { container } = renderAdhoc(__trendsBarBreakdown, true)
diff --git a/frontend/src/exporter/scenes/ExporterQueryScene.tsx b/frontend/src/exporter/scenes/ExporterQueryScene.tsx
index e8618f89452c..bf714e522452 100644
--- a/frontend/src/exporter/scenes/ExporterQueryScene.tsx
+++ b/frontend/src/exporter/scenes/ExporterQueryScene.tsx
@@ -26,11 +26,13 @@ import { ExportedData } from '../types'
export default function ExporterQueryScene({
query,
queryResults,
+ title,
themes,
exportOptions,
}: {
query: NonNullable<ExportedData['query']>
queryResults: ExportedData['query_results']
+ title: ExportedData['query_title']
themes: NonNullable<ExportedData['themes']>
exportOptions: SharingConfigurationSettings
}): JSX.Element {
@@ -82,6 +84,11 @@ export default function ExporterQueryScene({
trendsDisplay === ChartDisplayType.Metric && 'ExportedInsight--metric'
)}
>
+ {title && (
+ <div className="ExportedInsight__header">
+ <div className="ExportedInsight__header__title">{title}</div>
+ </div>
+ )}
<div className="ExportedInsight__content">
<Query
query={query}
diff --git a/frontend/src/exporter/types.ts b/frontend/src/exporter/types.ts
index f8cb7f21e3f6..3da97b15769d 100644
--- a/frontend/src/exporter/types.ts
+++ b/frontend/src/exporter/types.ts
@@ -67,6 +67,8 @@ export interface ExportedData extends SharingConfigurationSettings {
*/
query?: QuerySchema
query_results?: AnyResponseType
+ /** Optional title shown inside an ad-hoc query image export. */
+ query_title?: string
autoplay?: boolean
/** Player adds border by default - we want to remove it **/
noBorder?: boolean
diff --git a/posthog/api/sharing.py b/posthog/api/sharing.py
index af57dadc12d1..63a5ab349d24 100644
--- a/posthog/api/sharing.py
+++ b/posthog/api/sharing.py
@@ -1335,6 +1335,7 @@ class SharingViewerPageViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSe
{
"query": source_query,
"query_results": serialized_response,
+ "query_title": resource.export_context.get("title"),
"themes": get_themes_for_team(resource.team),
}
)
diff --git a/posthog/api/test/test_sharing.py b/posthog/api/test/test_sharing.py
index 30468ede245b..85fd079131c9 100644
--- a/posthog/api/test/test_sharing.py
+++ b/posthog/api/test/test_sharing.py
@@ -1588,7 +1588,7 @@ class TestSharedAdhocQueryExport(APIBaseTest):
team=self.team,
export_format=ExportedAsset.ExportFormat.PNG,
created_by=self.user,
- export_context={"source": self._SOURCE_QUERY},
+ export_context={"source": self._SOURCE_QUERY, "title": "Weekly signups"},
)
@patch("posthog.api.sharing.process_query_dict")
@@ -1604,6 +1604,7 @@ class TestSharedAdhocQueryExport(APIBaseTest):
exported_data = json.loads(mock_render_template.call_args[1]["context"]["exported_data"])
assert exported_data["query"] == self._SOURCE_QUERY
assert exported_data["query_results"] == {"results": [{"count": 42}], "cache_key": "abc"}
+ assert exported_data["query_title"] == "Weekly signups"
# The read must be attributed to the export owner so warehouse access control resolves.
assert mock_process_query.call_args[1]["user"] == self.user
diff --git a/posthog/templates/email/ai_subscription_report.html b/posthog/templates/email/ai_subscription_report.html
index 7a72415f79e1..5575f88e381b 100644
--- a/posthog/templates/email/ai_subscription_report.html
+++ b/posthog/templates/email/ai_subscription_report.html
@@ -10,9 +10,6 @@
{% if charts %}
<div class="report-charts">
{% for chart in charts %}
- {% if chart.title %}
- <p style="margin: 0 0 4px 0; font-weight: bold;">{{ chart.title }}</p>
- {% endif %}
<img class="insight-image mb" src="{{ chart.image_url }}" alt="{{ chart.title|default:'Chart' }}" />
{% endfor %}
</div>
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/charts.py b/products/exports/backend/temporal/subscriptions/ai_subscription/charts.py
index 16ec31292d07..4136de1e9e1c 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/charts.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/charts.py
@@ -168,6 +168,7 @@ def build_export_context(chart: ValidatedChart) -> dict:
chart_settings["showLegend"] = True
return {
"limit_context": "posthog_ai",
+ "title": chart.title,
"source": {
"kind": "DataVisualizationNode",
"source": {"kind": "HogQLQuery", "query": chart.hogql},
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/delivery.py b/products/exports/backend/temporal/subscriptions/ai_subscription/delivery.py
index 5db6f8f37897..4cb33294535d 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/delivery.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/delivery.py
@@ -77,6 +77,7 @@ _ALLOWED_EMAIL_ATTRS = {"a": {"href", "title"}}
# Slack's hard limit is 3000 chars per section block; keep margin for safety.
SLACK_MRKDWN_SECTION_LIMIT = 2900
+SLACK_IMAGE_TITLE_LIMIT = 2000
def _split_text_into_chunks(text: str, limit: int = SLACK_MRKDWN_SECTION_LIMIT) -> list[str]:
@@ -212,7 +213,6 @@ async def build_ai_subscription_report(subscription: Subscription) -> AiReportRe
CHART_IMAGE_URL_TTL = timedelta(days=180)
-SLACK_IMAGE_TITLE_LIMIT = 2000
def build_chart_image_urls(charts: Any, *, team_id: int) -> list[dict]:
@@ -336,7 +336,11 @@ def _build_ai_slack_message(
]
for chart in charts or []:
caption = chart.get("title") or "Chart"
- image_block: dict = {"type": "image", "image_url": chart["image_url"], "alt_text": caption[:2000]}
+ image_block: dict = {
+ "type": "image",
+ "image_url": chart["image_url"],
+ "alt_text": caption[:SLACK_IMAGE_TITLE_LIMIT],
+ }
if chart.get("title"):
image_block["title"] = {"type": "plain_text", "text": caption[:SLACK_IMAGE_TITLE_LIMIT]}
blocks.append(image_block)
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_charts.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_charts.py
index b1acdbc9666c..e4f14dd33ba3 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_charts.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_charts.py
@@ -122,6 +122,7 @@ def _chart(spec=_LINE, hogql="SELECT 1", step_index=0) -> ValidatedChart:
def test_the_export_context_pins_the_render_to_the_step_row_limits():
assert build_export_context(_chart())["limit_context"] == "posthog_ai"
+ assert build_export_context(_chart())["title"] == "signups"
def test_the_export_context_wraps_the_executed_sql_for_the_renderer():
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_delivery.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_delivery.py
index 7b83392ec62e..f28747236fdc 100644
--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_delivery.py
+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_delivery.py
@@ -5,6 +5,8 @@ import pytest
from posthog.test.base import APIBaseTest
from unittest.mock import AsyncMock, MagicMock, patch
+from django.template.loader import render_to_string
+
from parameterized import parameterized
from slack_sdk.errors import SlackApiError
@@ -79,6 +81,14 @@ class TestRenderAIEmailHtml:
assert "<strong>bold</strong>" in html
assert "<em>italic</em>" in html
+ def test_chart_title_only_appears_in_the_image_alt_text(self) -> None:
+ html = render_to_string(
+ "email/ai_subscription_report.html",
+ {"title": "Report", "rendered_html": "", "charts": [_CHART]},
+ )
+
+ assert html.count(_CHART["title"]) == 1
+
class TestExternalUrlExfilGuard:
"""A prompt-injected synthesis could embed a link to attacker.example; Slack auto-unfurls