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(paths): strip trailing slash from start/end filter URLs (#59193)

Ben Leab036272ad4cd
diff --git a/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py b/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py
index 395dcdcc14e8..9e284b29481b 100644
--- a/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py
@@ -114,6 +114,15 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):
return event in (self.query.pathsFilter.includeEventTypes or [])
+ @staticmethod
+ def _strip_trailing_slash(url: Optional[str]) -> Optional[str]:
+ # Mirrors the `(.)/$` regex applied to event URLs in `construct_event_hogql`,
+ # so that startPoint/endPoint values match the normalized values stored in
+ # `compact_path` / `start_filtered_path`. The bare "/" URL is preserved.
+ if url and len(url) > 1 and url.endswith("/"):
+ return url[:-1]
+ return url
+
def construct_event_hogql(self) -> ast.Expr:
event_hogql: ast.Expr = parse_expr("event")
@@ -523,6 +532,8 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):
def get_target_clause(self) -> list[ast.Expr]:
if self.query.pathsFilter.startPoint and self.query.pathsFilter.endPoint:
+ start_point = self._strip_trailing_slash(self.query.pathsFilter.startPoint)
+ end_point = self._strip_trailing_slash(self.query.pathsFilter.endPoint)
clauses: list[ast.Expr] = [
ast.Alias(
alias=f"start_target_index",
@@ -530,7 +541,7 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):
name="indexOf",
args=[
ast.Field(chain=["compact_path"]),
- ast.Constant(value=self.query.pathsFilter.startPoint),
+ ast.Constant(value=start_point),
],
),
),
@@ -545,7 +556,7 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):
name="indexOf",
args=[
ast.Field(chain=["start_filtered_path"]),
- ast.Constant(value=self.query.pathsFilter.endPoint),
+ ast.Constant(value=end_point),
],
),
),
@@ -577,10 +588,7 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):
)
def paths_per_person_query(self) -> ast.SelectQuery:
- target_point = self.query.pathsFilter.endPoint or self.query.pathsFilter.startPoint
- target_point = (
- target_point[:-1] if target_point and len(target_point) > 1 and target_point.endswith("/") else target_point
- )
+ target_point = self._strip_trailing_slash(self.query.pathsFilter.endPoint or self.query.pathsFilter.startPoint)
path_tuples_expr = ast.Call(
name="arrayZip",
diff --git a/products/product_analytics/backend/hogql_queries/paths/test/test_paths_query_runner.py b/products/product_analytics/backend/hogql_queries/paths/test/test_paths_query_runner.py
index a47dad315ff4..f14611d2c3e2 100644
--- a/products/product_analytics/backend/hogql_queries/paths/test/test_paths_query_runner.py
+++ b/products/product_analytics/backend/hogql_queries/paths/test/test_paths_query_runner.py
@@ -12,6 +12,7 @@ from posthog.test.base import (
from django.utils.timezone import now
from dateutil.relativedelta import relativedelta
+from parameterized import parameterized
from posthog.schema import CachedPathsQueryResponse
@@ -934,6 +935,59 @@ class TestPaths(ClickhouseTestMixin, APIBaseTest):
response[2].dict().items() >= {"source": "2_/pricing", "target": "3_/about", "value": 1}.items()
)
+ @parameterized.expand(
+ [
+ ("both", "/pricing/", "/checkout/"),
+ ("start_only", "/pricing/", "/checkout"),
+ ("end_only", "/pricing", "/checkout/"),
+ ]
+ )
+ def test_paths_start_and_end_trailing_slashes(self, _name: str, start_point: str, end_point: str) -> None:
+ # `construct_event_hogql` strips trailing slashes from `$current_url`
+ # before paths runs its `indexOf` checks. The runner must apply the same
+ # normalization to `startPoint`/`endPoint` — otherwise a filter like
+ # `/pricing/` silently returns zero results even though `/pricing` would.
+ # Each endpoint goes through its own strip, so a trailing slash on only
+ # one side is a distinct code path worth asserting.
+ _create_person(team_id=self.team.pk, distinct_ids=["person_1"])
+ _create_person(team_id=self.team.pk, distinct_ids=["person_2"])
+
+ for distinct_id in ("person_1", "person_2"):
+ for url in ("/pricing/", "/about", "/checkout/"):
+ _create_event(
+ properties={"$current_url": url},
+ distinct_id=distinct_id,
+ event="$pageview",
+ team=self.team,
+ )
+
+ baseline = PathsQueryRunner(
+ query={
+ "kind": "PathsQuery",
+ "pathsFilter": {
+ "startPoint": "/pricing",
+ "endPoint": "/checkout",
+ },
+ },
+ team=self.team,
+ ).run()
+ assert isinstance(baseline, CachedPathsQueryResponse)
+ self.assertGreater(len(baseline.results), 0)
+
+ with_trailing_slashes = PathsQueryRunner(
+ query={
+ "kind": "PathsQuery",
+ "pathsFilter": {
+ "startPoint": start_point,
+ "endPoint": end_point,
+ },
+ },
+ team=self.team,
+ ).run()
+ assert isinstance(with_trailing_slashes, CachedPathsQueryResponse)
+
+ self.assertEqual(with_trailing_slashes.results, baseline.results)
+
def test_paths_in_window(self):
_create_person(team_id=self.team.pk, distinct_ids=["person_1"])