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): leave null session ids out of the watch shelf comparison (#98598)

Marcel Poelker41fa8c5a080c
diff --git a/products/experiments/backend/session_event_deltas.py b/products/experiments/backend/session_event_deltas.py
index d9cb3fcbf934..00003eb876b7 100644
--- a/products/experiments/backend/session_event_deltas.py
+++ b/products/experiments/backend/session_event_deltas.py
@@ -742,9 +742,16 @@ class _QuerySetup:
right=ast.Constant(value=self.window_end),
),
ast.CompareOperation(
- op=ast.CompareOperationOp.NotEq,
+ # An SDK that sends `$session_id: null` leaves the string "null" in the column,
+ # because the materialization keeps the raw JSON token. That string is a non-empty
+ # value, so it needs its own exclusion. Without it, every such event of every
+ # exposed person groups into one session whose last activity is always the most
+ # recent. That session takes a slot of the session ceiling, and the scan reads
+ # people from a session they never had. HogQL's own property read of `$session_id`
+ # treats both values as absent.
+ op=ast.CompareOperationOp.NotIn,
left=ast.Field(chain=["$session_id"]),
- right=ast.Constant(value=""),
+ right=ast.Constant(value=["", "null"]),
),
]
diff --git a/products/experiments/backend/test/test_session_event_deltas_api.py b/products/experiments/backend/test/test_session_event_deltas_api.py
index fd83b5b86544..ea9f37a12069 100644
--- a/products/experiments/backend/test/test_session_event_deltas_api.py
+++ b/products/experiments/backend/test/test_session_event_deltas_api.py
@@ -202,6 +202,14 @@ class TestExperimentSessionEventDeltas(ClickhouseTestMixin, APILicensedTest):
at=EXPOSED_AT + timedelta(minutes=30),
)
+ def _sessionless_backend_event(self, distinct_id: str, event: str, *, at: datetime) -> None:
+ # An explicit JSON null leaves the string "null" in the `$session_id` column, because the
+ # materialization keeps the raw JSON token. That is a different trace from
+ # `_unsessioned_exposure`, which omits the key and leaves the column empty.
+ _create_event(
+ team=self.team, event=event, distinct_id=distinct_id, timestamp=at, properties={"$session_id": None}
+ )
+
def _post_deltas(self, experiment: Experiment, **body: Any) -> Any:
return self.client.post(
f"/api/projects/{self.team.id}/experiments/{experiment.id}/session_event_deltas/",
@@ -362,6 +370,12 @@ class TestExperimentSessionEventDeltas(ClickhouseTestMixin, APILicensedTest):
self._session(
variants=[], events=["after_event"], distinct_id=returns_after, at=EXPOSED_AT + timedelta(hours=1)
)
+ # Backend calls that carry no session. One lands between returns_after's exposure and their
+ # next session, so it must not become the session they are read from. The other is the only
+ # thing api_only ever does after exposure, so they have no session to compare at all.
+ self._sessionless_backend_event(returns_after, "api_call", at=EXPOSED_AT + timedelta(minutes=10))
+ api_only = self._unsessioned_exposure("control", distinct_id="api_only", at=EXPOSED_AT)
+ self._sessionless_backend_event(api_only, "api_call", at=EXPOSED_AT + timedelta(minutes=10))
flush_persons_and_events()
data = self._post_deltas(experiment).json()
@@ -375,7 +389,7 @@ class TestExperimentSessionEventDeltas(ClickhouseTestMixin, APILicensedTest):
# sessions all ended before their exposure is not counted at all.
carded_events = {card["event"] for card in self._cards(data, "behavior")}
assert "after_event" in carded_events
- assert {"stale_event", "before_event"}.isdisjoint(carded_events)
+ assert {"stale_event", "before_event", "api_call"}.isdisjoint(carded_events)
# People counted once; the sessions total still says how much material sits behind the variant.
assert [(variant["persons"], variant["sessions"]) for variant in data["variants"]] == [(1, 4), (2, 2)]