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(tags): order tags alphabetically in tag list endpoint (#61513)

ruby childs6024d432f2dd
diff --git a/posthog/api/tagged_item.py b/posthog/api/tagged_item.py
index 9d1dbe7832de..5fb5b5cd0b29 100644
--- a/posthog/api/tagged_item.py
+++ b/posthog/api/tagged_item.py
@@ -288,7 +288,9 @@ class TaggedItemViewSet(TeamAndOrgViewSetMixin, GenericViewSet):
queryset = Tag.objects.none()
def list(self, request, *args, **kwargs) -> response.Response:
- return response.Response(Tag.objects.filter(team=self.team).values_list("name", flat=True).distinct())
+ return response.Response(
+ Tag.objects.filter(team=self.team).values_list("name", flat=True).distinct().order_by("name")
+ )
@dataclasses.dataclass(frozen=True)
diff --git a/posthog/api/test/test_tagged_item.py b/posthog/api/test/test_tagged_item.py
index 3284efb11f5b..b04a386d7d9c 100644
--- a/posthog/api/test/test_tagged_item.py
+++ b/posthog/api/test/test_tagged_item.py
@@ -85,16 +85,17 @@ class TestTaggedItemSerializerMixin(APIBaseTest):
def test_can_list_tags(self) -> None:
dashboard = Dashboard.objects.create(team_id=self.team.id, name="private dashboard")
- tag = Tag.objects.create(name="dashboard tag", team_id=self.team.id)
+ tag = Tag.objects.create(name="zebra tag", team_id=self.team.id)
dashboard.tagged_items.create(tag_id=tag.id)
insight = Insight.objects.create(team_id=self.team.id, name="empty insight")
- tag2 = Tag.objects.create(name="insight tag", team_id=self.team.id)
+ tag2 = Tag.objects.create(name="apple tag", team_id=self.team.id)
insight.tagged_items.create(tag_id=tag2.id)
response = self.client.get(f"/api/projects/{self.team.id}/tags")
assert response.status_code == status.HTTP_200_OK
- assert sorted(response.json()) == ["dashboard tag", "insight tag"]
+ # Tags are returned in alphabetical order regardless of insertion order
+ assert response.json() == ["apple tag", "zebra tag"]
class TestBulkUpdateTags(APIBaseTest):