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(capture): disable geoip on internal events without a client ip (#113972)

Kyle Swank25b818a2f431
diff --git a/posthog/api/capture.py b/posthog/api/capture.py
index e0126a9d45d8..5778d33847e7 100644
--- a/posthog/api/capture.py
+++ b/posthog/api/capture.py
@@ -452,12 +452,18 @@ def prepare_capture_internal_batch(
ev, process_person_profile=process_person_profile, event_source=event_source
)
+ properties = parts.properties
+ if not properties.get("$ip"):
+ # capture-rs gives internal events a loopback IP. GeoIP would turn it into a placeholder
+ # location and write that over the person's real location.
+ properties = {"$geoip_disable": True, **properties}
+
entry: dict[str, Any] = {
"event": identity.event_name,
"uuid": event_uuid,
"distinct_id": identity.distinct_id,
"timestamp": timestamp_str,
- "properties": parts.properties,
+ "properties": properties,
}
if parts.session_id is not None:
entry["session_id"] = parts.session_id
diff --git a/posthog/api/test/test_capture_internal.py b/posthog/api/test/test_capture_internal.py
index 06912a6b9f70..31863c3079c3 100644
--- a/posthog/api/test/test_capture_internal.py
+++ b/posthog/api/test/test_capture_internal.py
@@ -319,6 +319,21 @@ class TestPrepareCaptureInternalBatch(SimpleTestCase):
)
assert "options" not in payload["batch"][0]
+ @parameterized.expand(
+ [
+ ("no_ip", {"$set": {"a": 1}}, {"$geoip_disable": True, "$set": {"a": 1}}),
+ ("client_ip", {"$ip": "203.0.113.7"}, {"$ip": "203.0.113.7"}),
+ ("caller_opts_in", {"$geoip_disable": False}, {"$geoip_disable": False}),
+ ]
+ )
+ def test_geoip_disabled_without_a_client_ip(
+ self, _name: str, properties: dict[str, Any], expected: dict[str, Any]
+ ) -> None:
+ payload, _ = prepare_capture_internal_batch(
+ [_make_event(properties=properties)], token="tok", event_source="test"
+ )
+ assert payload["batch"][0]["properties"] == expected
+
def test_options_present_when_set(self) -> None:
events = [_make_event(options={"cookieless_mode": True})]
payload, _ = prepare_capture_internal_batch(events, token="tok", event_source="test")
@@ -1765,7 +1780,7 @@ class TestCaptureAiInternal(SimpleTestCase):
entry = spy.calls[0]["json"]["batch"][0]
assert (entry["session_id"], entry["window_id"]) == ("s1", "w1")
- assert entry["properties"] == {"$ai_model": "m"}
+ assert entry["properties"] == {"$geoip_disable": True, "$ai_model": "m"}
@patch("posthog.api.capture.internal_requests_session")
def test_server_side_misrouted_drop_surfaces_per_event(self, mock_session_fn: MagicMock) -> None: