: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(error-tracking): add source maps wizard prompt to weekly digest (#65219)
Aleksander Błaszkiewicz3c4c99c49a69
diff --git a/posthog/tasks/email.py b/posthog/tasks/email.pyindex e63d93904674..e71129f131fe 100644--- a/posthog/tasks/email.py+++ b/posthog/tasks/email.py@@ -1907,6 +1907,7 @@ def send_error_tracking_weekly_digest_for_org(org_id: str) -> None:"new_issues": error_tracking_api.get_new_issues_for_team(team),"daily_counts": error_tracking_api.get_daily_exception_counts(team),"crash_free": error_tracking_api.get_crash_free_sessions(team),+ "source_maps_recommendation": error_tracking_api.get_source_maps_recommendation_for_team(team),"error_tracking_url": f"{settings.SITE_URL}/project/{team_id}/error_tracking?utm_source=error_tracking_weekly_digest","ingestion_failures_url": error_tracking_api.build_ingestion_failures_url(team_id),}diff --git a/posthog/tasks/test/preview_error_tracking_digest.py b/posthog/tasks/test/preview_error_tracking_digest.pyindex e65a4d4f397b..7b3a3853f039 100644--- a/posthog/tasks/test/preview_error_tracking_digest.py+++ b/posthog/tasks/test/preview_error_tracking_digest.py@@ -3,15 +3,30 @@ Quick preview of the error tracking weekly digest email template.Usage:python manage.py shell -c "exec(open('posthog/tasks/test/preview_error_tracking_digest.py').read())"++ # Preview the EU source maps wizard command (adds --region eu)+ DIGEST_PREVIEW_REGION=eu python manage.py shell -c "exec(open('posthog/tasks/test/preview_error_tracking_digest.py').read())""""+import osimport sysimport subprocess+from django.conf import settingsfrom django.template.loader import get_templatefrom posthog.email import inline_css+from products.error_tracking.backend.facade.api import SOURCE_MAPS_DOCS_URL+++def _wizard_command() -> str:+ # Mirrors sourceMapsFixWizardLogic / _source_maps_wizard_command: --region eu on EU.+ region = (os.environ.get("DIGEST_PREVIEW_REGION") or settings.CLOUD_DEPLOYMENT or "us").lower()+ region_flag = " --region eu" if region == "eu" else ""+ return f"npx -y @posthog/wizard@latest upload-source-maps{region_flag}"++_issue_sparkline_up = [{"height_percent": 10},{"height_percent": 25},@@ -51,6 +66,12 @@ project_a = {"long_text": "Down 23% from previous week",},"ingestion_failure_count": 347,+ "source_maps_recommendation": {+ "unresolved_percent": 72,+ "lookback_hours": 24,+ "wizard_command": _wizard_command(),+ "docs_url": f"{SOURCE_MAPS_DOCS_URL}?utm_source=error_tracking_weekly_digest",+ },"daily_counts": [{"day": "Tue", "count": 1200, "height_percent": 60},{"day": "Wed", "count": 800, "height_percent": 40},@@ -153,6 +174,8 @@ project_b = {"long_text": "Up 45% from previous week",},"ingestion_failure_count": 0,+ # No source maps problem here — section is omitted for this project.+ "source_maps_recommendation": None,"daily_counts": [{"day": "Tue", "count": 80, "height_percent": 40},{"day": "Wed", "count": 120, "height_percent": 60},diff --git a/posthog/templates/email/error_tracking_weekly_digest.html b/posthog/templates/email/error_tracking_weekly_digest.htmlindex 124786a78404..12574ca0e867 100644--- a/posthog/templates/email/error_tracking_weekly_digest.html+++ b/posthog/templates/email/error_tracking_weekly_digest.html@@ -78,6 +78,21 @@{% endif %}</div>+ <!-- Missing source maps: prompt the source maps wizard -->+ {% if section.source_maps_recommendation %}+ <div style="background-color: #fef3cd; border: 1px solid #ffc107; padding: 20px; border-radius: 8px; margin-bottom: 8px;">+ <p style="margin: 0 0 8px 0; font-weight: 700; color: #856404; font-size: 1em;">+ ⚠ {{ section.source_maps_recommendation.unresolved_percent }}% of your stack traces aren't readable+ </p>+ <p style="margin: 0 0 16px 0; color: #664d03; font-size: 0.9em;">+ Upload source maps so stack traces show your real source. Run our wizard from your project root, or <a href="{{ section.source_maps_recommendation.docs_url }}" target="_blank" style="color: #856404; text-decoration: underline; font-weight: 500;">read the docs</a>.+ </p>+ <div style="background-color: #ffffff; border: 1px solid #f0d98c; border-radius: 6px; padding: 10px 14px;">+ <code style="font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace; color: #5c4a03; font-size: 0.7em;">{{ section.source_maps_recommendation.wizard_command }}</code>+ </div>+ </div>+ {% endif %}+<!-- Top issues -->{% if section.top_issues %}<div style="background-color: #ffffff; border-radius: 8px; padding: 20px; margin-bottom: 8px;">@@ -95,7 +110,7 @@{% for issue in section.top_issues %}<tr style="border-bottom: 1px solid #f3f4f6;"><td align="left" style="padding: 10px 8px 10px 0; vertical-align: top; text-align: left;">- <a href="{{ issue.url }}" target="_blank" style="text-decoration: none; color: #111827; font-weight: 600; font-size: 0.9em;">{{ issue.name|truncatechars:40 }}</a>+ <a href="{{ issue.url }}" target="_blank" style="text-decoration: underline; color: #111827; font-weight: 600; font-size: 0.9em;">{{ issue.name|truncatechars:40 }}</a>{% if issue.description %}<div style="color: #6b7280; font-size: 0.8em; margin-top: 2px;">{{ issue.description|truncatechars:45 }}</div>{% endif %}@@ -141,7 +156,7 @@{% for issue in section.new_issues %}<tr style="border-bottom: 1px solid #f3f4f6;"><td align="left" style="padding: 10px 8px 10px 0; vertical-align: top; text-align: left;">- <a href="{{ issue.url }}" target="_blank" style="text-decoration: none; color: #111827; font-weight: 600; font-size: 0.9em;">{{ issue.name|truncatechars:40 }}</a>+ <a href="{{ issue.url }}" target="_blank" style="text-decoration: underline; color: #111827; font-weight: 600; font-size: 0.9em;">{{ issue.name|truncatechars:40 }}</a>{% if issue.description %}<div style="color: #6b7280; font-size: 0.8em; margin-top: 2px;">{{ issue.description|truncatechars:45 }}</div>{% endif %}diff --git a/products/error_tracking/backend/facade/api.py b/products/error_tracking/backend/facade/api.pyindex 20fed966b25a..497e1a64aa62 100644--- a/products/error_tracking/backend/facade/api.py+++ b/products/error_tracking/backend/facade/api.py@@ -20,6 +20,8 @@ ExternalReferenceValidationError = logic.ErrorTrackingExternalReferenceValidatioReleaseHashInUseError = logic.ErrorTrackingReleaseHashInUseErrorInvalidBytecodeError = logic.ErrorTrackingInvalidBytecodeError+SOURCE_MAPS_DOCS_URL = weekly_digest.SOURCE_MAPS_DOCS_URL+def _to_issue_assignee(assignment) -> contracts.ErrorTrackingIssueAssignee | None:if assignment is None:@@ -572,5 +574,9 @@ def auto_select_project_for_user(user: Any, org_id: int, team_exception_counts:)+def get_source_maps_recommendation_for_team(team: Any) -> dict[str, Any] | None:+ return weekly_digest.get_source_maps_recommendation_for_team(team)++def build_ingestion_failures_url(team_id: int) -> str:return weekly_digest.build_ingestion_failures_url(team_id)diff --git a/products/error_tracking/backend/test/test_weekly_digest.py b/products/error_tracking/backend/test/test_weekly_digest.pyindex dad2ce3abf28..51ec9580985c 100644--- a/products/error_tracking/backend/test/test_weekly_digest.py+++ b/products/error_tracking/backend/test/test_weekly_digest.py@@ -3,6 +3,7 @@ from uuid import uuid4from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events+from django.test import override_settingsfrom django.utils import timezonefrom parameterized import parameterized@@ -12,7 +13,11 @@ from posthog.models.organization import Organizationfrom posthog.models.utils import uuid7from posthog.tasks.email_utils import compute_week_over_week_change-from products.error_tracking.backend.models import ErrorTrackingIssue, ErrorTrackingIssueFingerprintV2+from products.error_tracking.backend.models import (+ ErrorTrackingIssue,+ ErrorTrackingIssueFingerprintV2,+ ErrorTrackingRecommendation,+)from products.error_tracking.backend.weekly_digest import (auto_select_project_for_user,get_crash_free_sessions,@@ -21,6 +26,7 @@ from products.error_tracking.backend.weekly_digest import (get_exception_summary_for_team,get_new_issues_for_team,get_org_ids_with_exceptions,+ get_source_maps_recommendation_for_team,get_top_issues_for_team,)@@ -477,3 +483,68 @@ class TestComputeWeekOverWeekChange:def test_returns_none_when_no_change(self):assert compute_week_over_week_change(100, 100, higher_is_better=True) is None+++# total_frames >= 20 and unresolved_pct > 0.30 => an active (not completed) recommendation+_ACTIVE_META = {+ "total_frames": 100,+ "unresolved_frames": 72,+ "unresolved_pct": 0.72,+ "threshold_pct": 0.30,+ "min_sample_frames": 20,+ "lookback_hours": 24,+}+++class TestSourceMapsRecommendationForDigest(APIBaseTest):+ def _create_recommendation(+ self, *, meta: dict, computed: bool = True, dismissed: bool = False+ ) -> ErrorTrackingRecommendation:+ now = timezone.now()+ return ErrorTrackingRecommendation.objects.create(+ team=self.team,+ type="source_maps",+ meta=meta,+ computed_at=now if computed else None,+ dismissed_at=now if dismissed else None,+ )++ def test_returns_none_when_no_recommendation(self):+ assert get_source_maps_recommendation_for_team(self.team) is None++ def test_returns_none_when_not_yet_computed(self):+ self._create_recommendation(meta=_ACTIVE_META, computed=False)+ assert get_source_maps_recommendation_for_team(self.team) is None++ def test_returns_none_when_dismissed(self):+ self._create_recommendation(meta=_ACTIVE_META, dismissed=True)+ assert get_source_maps_recommendation_for_team(self.team) is None++ def test_returns_none_when_completed_below_threshold(self):+ self._create_recommendation(meta={**_ACTIVE_META, "unresolved_frames": 5, "unresolved_pct": 0.05})+ assert get_source_maps_recommendation_for_team(self.team) is None++ def test_returns_none_when_completed_too_few_frames(self):+ self._create_recommendation(meta={**_ACTIVE_META, "total_frames": 5})+ assert get_source_maps_recommendation_for_team(self.team) is None++ def test_returns_data_when_active(self):+ self._create_recommendation(meta=_ACTIVE_META)+ result = get_source_maps_recommendation_for_team(self.team)+ assert result is not None+ assert result["unresolved_percent"] == 72+ assert result["lookback_hours"] == 24+ assert result["wizard_command"] == "npx -y @posthog/wizard@latest upload-source-maps"+ assert result["docs_url"].startswith("https://posthog.com/docs/error-tracking/upload-source-maps")++ @override_settings(CLOUD_DEPLOYMENT="EU")+ def test_wizard_command_appends_region_eu_on_eu_cloud(self):+ self._create_recommendation(meta=_ACTIVE_META)+ result = get_source_maps_recommendation_for_team(self.team)+ assert result is not None+ assert result["wizard_command"] == "npx -y @posthog/wizard@latest upload-source-maps --region eu"++ def test_only_returns_recommendation_for_the_given_team(self):+ other_team = Team.objects.create(organization=self.organization, name="Other")+ self._create_recommendation(meta=_ACTIVE_META)+ assert get_source_maps_recommendation_for_team(other_team) is Nonediff --git a/products/error_tracking/backend/weekly_digest.py b/products/error_tracking/backend/weekly_digest.pyindex 41e149a4aace..392f0846e58d 100644--- a/products/error_tracking/backend/weekly_digest.py+++ b/products/error_tracking/backend/weekly_digest.py@@ -14,6 +14,9 @@ from posthog.schema_enums import ProductKeylogger = structlog.get_logger(__name__)+# Keep in sync with SOURCE_MAPS_DOCS_URL in sourceMapsFixWizardLogic.ts+SOURCE_MAPS_DOCS_URL = "https://posthog.com/docs/error-tracking/upload-source-maps"+def get_org_ids_with_exceptions() -> list[str]:"""Return distinct organization IDs that have teams with exceptions in the last 7 days"""@@ -368,6 +371,47 @@ def _daily_counts_to_sparkline(daily_counts: list[int]) -> list[dict]:return [{"height_percent": int((v / max_val) * 100) if max_val > 0 else 0} for v in daily_counts]+def _source_maps_wizard_command() -> str:+ """Source maps upload wizard command, mirroring sourceMapsFixWizardLogic on the frontend.++ Appends ``--region eu`` on EU Cloud so the wizard uploads to the right region.+ """+ region_flag = " --region eu" if (settings.CLOUD_DEPLOYMENT or "").upper() == "EU" else ""+ return f"npx -y @posthog/wizard@latest upload-source-maps{region_flag}"+++def get_source_maps_recommendation_for_team(team: Team) -> dict | None:+ """Return wizard prompt data when the team has an active 'missing source maps' recommendation.++ Mirrors the in-app banner: only surface it once the recommendation has been computed, while+ there's still a problem (not completed), and the user hasn't dismissed it. Returns ``None``+ otherwise so the digest can omit the section entirely.+ """+ from products.error_tracking.backend.logic.recommendations import RECOMMENDATIONS_BY_TYPE+ from products.error_tracking.backend.models import ErrorTrackingRecommendation++ rec = RECOMMENDATIONS_BY_TYPE.get("source_maps")+ if rec is None:+ return None++ recommendation = ErrorTrackingRecommendation.objects.filter(+ team=team, type="source_maps", dismissed_at__isnull=True+ ).first()+ if recommendation is None or recommendation.computed_at is None:+ return None++ meta = recommendation.meta or {}+ if rec.is_completed(meta):+ return None++ return {+ "unresolved_percent": round((meta.get("unresolved_pct") or 0.0) * 100),+ "lookback_hours": meta.get("lookback_hours"),+ "wizard_command": _source_maps_wizard_command(),+ "docs_url": f"{SOURCE_MAPS_DOCS_URL}?utm_source=error_tracking_weekly_digest",+ }++def build_ingestion_failures_url(team_id: int) -> str:return (f"{settings.SITE_URL}/project/{team_id}/activity/explore#q="