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(activity-logs): keep every filter value in activity log exports (#112431)

Eleftheria Trivyzakic985ee291e1d
diff --git a/posthog/api/advanced_activity_logs/viewset.py b/posthog/api/advanced_activity_logs/viewset.py
index 1c99c68100c0..55fccb19381c 100644
--- a/posthog/api/advanced_activity_logs/viewset.py
+++ b/posthog/api/advanced_activity_logs/viewset.py
@@ -721,17 +721,21 @@ class AdvancedActivityLogsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSe
if not filters_serializer.is_valid():
return Response({"error": "Filters are invalid"}, status=400)
- query_params = {}
+ query_params: dict[str, str | list[str]] = {}
- # Transform body params to query params to include the filters in the export path
+ # Transform body params to query params to include the filters in the export path.
+ # Lists become repeated keys (?users=a&users=b): the filters serializer reads them via
+ # getlist, so a comma-joined value would be validated as one item. Only unset or empty
+ # values are skipped: an explicit False is a filter too (is_system=false).
for key, value in filters_serializer.validated_data.items():
- if value:
- if isinstance(value, list):
- query_params[key] = ",".join(str(v) for v in value)
- elif isinstance(value, dict):
- query_params[key] = json.dumps(value)
- else:
- query_params[key] = str(value)
+ if value is None or value in ("", [], {}):
+ continue
+ if isinstance(value, list):
+ query_params[key] = [str(v) for v in value]
+ elif isinstance(value, dict):
+ query_params[key] = json.dumps(value)
+ else:
+ query_params[key] = str(value)
try:
serializable_filters = self._make_filters_serializable(filters_serializer.validated_data)
@@ -747,7 +751,7 @@ class AdvancedActivityLogsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSe
team=self.team,
export_format=format_mapping[export_format],
export_context={
- "path": f"/api/projects/{self.team_id}/advanced_activity_logs/?{urlencode(query_params)}",
+ "path": f"/api/projects/{self.team_id}/advanced_activity_logs/?{urlencode(query_params, doseq=True)}",
"method": "GET",
"filters": serializable_filters,
"filename": filename,
diff --git a/posthog/api/test/test_activity_log.py b/posthog/api/test/test_activity_log.py
index 7d4c5d755d48..8211e3832103 100644
--- a/posthog/api/test/test_activity_log.py
+++ b/posthog/api/test/test_activity_log.py
@@ -25,6 +25,7 @@ from posthog.session.activity import session_public_id
from posthog.test.insight_queries import default_pageview_query
from products.exports.backend.models.exported_asset import ExportedAsset
+from products.exports.backend.tasks.csv_exporter import add_query_params
def _feature_flag_json_payload(key: str) -> dict:
@@ -652,6 +653,56 @@ class TestActivityLogBearerAuthAttribution(APIBaseTest):
assert exported_asset.source_credential_id == str(token.id)
+class TestAdvancedActivityLogExportPath(APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.other_user = User.objects.create_and_join(self.organization, "[email protected]", None)
+ for item_id, user, activity in (
+ ("flag-created", self.user, "created"),
+ ("flag-updated", self.other_user, "updated"),
+ ("flag-deleted", None, "deleted"),
+ ):
+ log_activity(
+ organization_id=self.organization.id,
+ team_id=self.team.id,
+ user=user,
+ was_impersonated=False,
+ item_id=item_id,
+ scope="FeatureFlag",
+ activity=activity,
+ detail=Detail(name=item_id),
+ force_save=True,
+ )
+
+ @parameterized.expand(
+ [
+ ("multiple_users", "users", lambda self: [str(self.user.uuid), str(self.other_user.uuid)]),
+ ("multiple_activities", "activities", lambda _self: ["created", "updated"]),
+ ("explicit_false", "is_system", lambda _self: False),
+ ]
+ )
+ @patch("posthog.api.advanced_activity_logs.viewset.exporter.export_asset.delay")
+ def test_export_path_keeps_every_filter_value(
+ self, _name: str, filter_key: str, value: Any, _mock_exporter_task: Any
+ ) -> None:
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/advanced_activity_logs/export/",
+ {"format": "csv", "filters": {filter_key: value(self)}},
+ format="json",
+ )
+ assert response.status_code == status.HTTP_202_ACCEPTED
+ exported_asset = ExportedAsset.objects.get(id=response.json()["id"])
+ assert exported_asset.export_context is not None
+ export_path = exported_asset.export_context["path"]
+
+ # The exporter rewrites the stored path and replays it against the list endpoint, so a
+ # filter must survive both the stored encoding and that rewrite.
+ replay = self.client.get(add_query_params(export_path, {"limit": "100", "is_csv_export": "1"}))
+
+ assert replay.status_code == status.HTTP_200_OK
+ assert {row["item_id"] for row in replay.json()["results"]} == {"flag-created", "flag-updated"}
+
+
class TestActivityLogSerializerFields(SimpleTestCase):
@parameterized.expand([("advanced", ActivityLogSerializer), ("my_notifications", MyNotificationsSerializer)])
def test_credential_fields_are_not_serialized(self, _name: str, serializer_class: type) -> None: