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.

feat: add source and dashboard fields to insight view history (#106552)

Vasco de Krijgera6351479310e
diff --git a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
index 1640185e488c..2671db19adb7 100644
--- a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
+++ b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
@@ -422,7 +422,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
@@ -3007,7 +3009,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
@@ -4941,7 +4945,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
@@ -6013,7 +6019,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
@@ -10697,7 +10705,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
@@ -11754,7 +11764,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999
diff --git a/products/product_analytics/backend/migrations/0011_insight_viewed_context_fields.py b/products/product_analytics/backend/migrations/0011_insight_viewed_context_fields.py
new file mode 100644
index 000000000000..438a1e95e7af
--- /dev/null
+++ b/products/product_analytics/backend/migrations/0011_insight_viewed_context_fields.py
@@ -0,0 +1,32 @@
+# Generated by Django 5.2.17 on 2026-09-25 14:01
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("dashboards", "0021_drop_dashboardtemplate_github_url_column"),
+ ("product_analytics", "0010_alter_insight_created_by_and_more"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="insightviewed",
+ name="dashboard",
+ field=models.ForeignKey(
+ blank=True,
+ db_constraint=False,
+ db_index=False,
+ null=True,
+ on_delete=django.db.models.deletion.DO_NOTHING,
+ related_name="+",
+ to="dashboards.dashboard",
+ ),
+ ),
+ migrations.AddField(
+ model_name="insightviewed",
+ name="source",
+ field=models.CharField(blank=True, db_default="", default="", max_length=64),
+ ),
+ ]
diff --git a/products/product_analytics/backend/migrations/max_migration.txt b/products/product_analytics/backend/migrations/max_migration.txt
index c6c709297ebe..3ea33c7209a3 100644
--- a/products/product_analytics/backend/migrations/max_migration.txt
+++ b/products/product_analytics/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0010_alter_insight_created_by_and_more
+0011_insight_viewed_context_fields
diff --git a/products/product_analytics/backend/models/insight.py b/products/product_analytics/backend/models/insight.py
index cbc95c82dc71..2d5a4e3006cb 100644
--- a/products/product_analytics/backend/models/insight.py
+++ b/products/product_analytics/backend/models/insight.py
@@ -455,6 +455,18 @@ class InsightViewed(models.Model):
insight: models.ForeignKey = models.ForeignKey(Insight, on_delete=models.CASCADE)
last_viewed_at: models.DateTimeField = models.DateTimeField()
+ # Empty source identifies legacy/unattributed history, not a standalone view.
+ source = models.CharField(max_length=64, default="", db_default="", blank=True)
+ dashboard = models.ForeignKey(
+ "dashboards.Dashboard",
+ on_delete=models.DO_NOTHING,
+ null=True,
+ blank=True,
+ db_constraint=False,
+ db_index=False,
+ related_name="+",
+ )
+
class Meta:
constraints = [models.UniqueConstraint(fields=["team", "user", "insight"], name="posthog_unique_insightviewed")]
indexes = [
diff --git a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
index 25c32980db2a..d7d03820e076 100644
--- a/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
+++ b/products/product_analytics/backend/tests/api/__snapshots__/test_insight.ambr
@@ -364,7 +364,9 @@
"posthog_insightviewed"."team_id",
"posthog_insightviewed"."user_id",
"posthog_insightviewed"."insight_id",
- "posthog_insightviewed"."last_viewed_at"
+ "posthog_insightviewed"."last_viewed_at",
+ "posthog_insightviewed"."source",
+ "posthog_insightviewed"."dashboard_id"
FROM "posthog_insightviewed"
WHERE ("posthog_insightviewed"."insight_id" = 99999
AND "posthog_insightviewed"."team_id" = 99999