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(experiments): fix admin experiment migration tool for shared metrics. (#59261)

Rodrigo Iloro597289367dbd
diff --git a/posthog/admin/admins/experiment_admin.py b/posthog/admin/admins/experiment_admin.py
index 618fb3ac76c6..5a004931fe70 100644
--- a/posthog/admin/admins/experiment_admin.py
+++ b/posthog/admin/admins/experiment_admin.py
@@ -195,16 +195,22 @@ class ExperimentAdmin(admin.ModelAdmin):
return redirect("admin:experiments_experiment_changelist")
all_metrics = (obj.metrics or []) + (obj.metrics_secondary or [])
- extra_context["show_migration"] = has_legacy_metric(all_metrics)
extra_context["show_fix_properties"] = has_malformed_properties(all_metrics)
# Get all related ExperimentSavedMetric objects
shared_metrics = obj.saved_metrics.all()
shared_metrics_status = []
+ has_legacy_shared_metric = False
+ has_unmigrated_legacy_shared_metric = False
for metric in shared_metrics:
kind = metric.query.get("kind") if metric.query else None
is_legacy = kind in ("ExperimentFunnelsQuery", "ExperimentTrendsQuery")
- migrated = bool(metric.metadata and "migrated_to" in metric.metadata)
+ if is_legacy:
+ has_legacy_shared_metric = True
+ migrated_to_id = metric.metadata.get("migrated_to") if metric.metadata else None
+ migrated = migrated_to_id is not None
+ if is_legacy and not migrated:
+ has_unmigrated_legacy_shared_metric = True
shared_metrics_status.append(
{
"id": metric.id,
@@ -212,9 +218,15 @@ class ExperimentAdmin(admin.ModelAdmin):
"is_legacy": is_legacy,
"migrated": migrated,
"migrate_url": reverse("admin:experiments_experimentsavedmetric_change", args=[metric.id]),
+ "migrated_to_id": migrated_to_id,
+ "migrated_to_url": reverse("admin:experiments_experimentsavedmetric_change", args=[migrated_to_id])
+ if migrated_to_id
+ else None,
}
)
extra_context["shared_metrics_status"] = shared_metrics_status
+ extra_context["show_migration"] = has_legacy_metric(all_metrics) or has_legacy_shared_metric
+ extra_context["can_migrate"] = not has_unmigrated_legacy_shared_metric
return super().change_view(request, object_id, form_url, extra_context=extra_context)
diff --git a/posthog/templates/admin/posthog/experiment/change_form.html b/posthog/templates/admin/posthog/experiment/change_form.html
index 23d5330aae96..e31708a8a591 100644
--- a/posthog/templates/admin/posthog/experiment/change_form.html
+++ b/posthog/templates/admin/posthog/experiment/change_form.html
@@ -11,30 +11,58 @@
</a>
</div>
{% endif %}
- {% if show_migration %}
- {% if shared_metrics_status %}
- <div class="submit-row" style="margin-top: 20px; padding: 10px; border-radius: 5px; border: 1px solid #f7a501; color: #f7a501; display: flex; align-items: center; gap: 10px;">
- <ul style="margin: 10px 0;">
+ {% if shared_metrics_status %}
+ <div class="module" style="margin-top: 20px;">
+ <h2>Shared metrics</h2>
+ <table style="width: 100%;">
+ <thead>
+ <tr>
+ <th scope="col">Name</th>
+ <th scope="col">Engine</th>
+ <th scope="col">Migrated to</th>
+ </tr>
+ </thead>
+ <tbody>
{% for metric in shared_metrics_status %}
- <li>
- <a href="{{ metric.migrate_url }}" target="_blank">{{ metric.name }}</a>
- {% if metric.is_legacy %}
- {% if metric.migrated %}
- <span style="color: green;">&#10003;</span>
+ <tr class="{% cycle 'row1' 'row2' %}">
+ <td><a href="{{ metric.migrate_url }}" target="_blank">{{ metric.name }}</a></td>
+ <td>
+ {% if metric.is_legacy %}
+ <span style="padding: 2px 6px; border-radius: 3px; background-color: #f7a501; color: white; font-size: 11px;">Legacy</span>
+ {% else %}
+ <span style="color: #6c757d;">Current</span>
+ {% endif %}
+ </td>
+ <td>
+ {% if metric.is_legacy %}
+ {% if metric.migrated %}
+ <a href="{{ metric.migrated_to_url }}" target="_blank">#{{ metric.migrated_to_id }}</a>
+ {% else %}
+ <span style="color: #dc3545;">Not migrated</span>
+ {% endif %}
{% else %}
- <span style="color: red;">&#10007;</span>
+ &mdash;
{% endif %}
- {% endif %}
- </li>
+ </td>
+ </tr>
{% endfor %}
- </ul>
- </div>
- {% endif %}
+ </tbody>
+ </table>
+ </div>
+ {% endif %}
+ {% if show_migration %}
<div class="submit-row" style="margin-top: 20px; padding: 20px; border-radius: 5px; border: 1px solid #f7a501; color: #f7a501; display: flex; align-items: center; gap: 10px;">
- <div class="info">This experiment uses the legacy engine and can be migrated.</div>
- <a class="button" style="padding:10px 15px;" href="{% url 'admin:experiment_migrate' object_id=original.pk %}">
- Migrate Experiment
- </a>
+ {% if can_migrate %}
+ <div class="info">This experiment uses the legacy engine and can be migrated.</div>
+ <a class="button" style="padding:10px 15px;" href="{% url 'admin:experiment_migrate' object_id=original.pk %}">
+ Migrate Experiment
+ </a>
+ {% else %}
+ <div class="info">This experiment cannot be migrated yet. Migrate all legacy shared metrics first.</div>
+ <span class="button" style="padding:10px 15px; opacity: 0.5; cursor: not-allowed;" aria-disabled="true">
+ Migrate Experiment
+ </span>
+ {% endif %}
</div>
{% endif %}
{% endblock %}