: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(intercom): walk companies via scroll API to avoid 10k page limit (#63033)
Tom Owers5c8e224b14b7
diff --git a/posthog/temporal/data_imports/sources/intercom/intercom.py b/posthog/temporal/data_imports/sources/intercom/intercom.pyindex eea5bd670798..c75e0243c0e0 100644--- a/posthog/temporal/data_imports/sources/intercom/intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/intercom.py@@ -260,23 +260,25 @@ def _conversation_parts_generator(def _iter_companies(session: Session) -> Iterator[dict[str, Any]]:- """Walk `POST /companies/list` page by page (no server-side timestamp- filter — full refresh)."""- body: dict[str, Any] = {"per_page": INTERCOM_ENDPOINTS["companies"].page_size}- next_url: str | None = None+ """Walk every company via `GET /companies/scroll` (full refresh).++ `POST /companies/list` is hard-capped at 10,000 companies — paging past+ that ceiling makes Intercom return `400 bad_request: page limit reached,+ please use scroll API`. The Scroll API has no such ceiling: each response+ carries a `scroll_param` to feed into the next request, and the walk ends+ when `data` comes back empty (the scroll param then expires). Only one+ scroll can be open per workspace at a time — fine here, since this is the+ sole scroll user and a schema never syncs concurrently with itself."""+ scroll_param: str | None = Nonewhile True:- if next_url is None:- payload = _intercom_post(session, "/companies/list", body)- else:- # `pages.next` is a full URL carrying the page cursor in its query- # string. `/companies/list` is POST-only — a GET against it 404s —- # so we re-POST the same body to the next-page URL, mirroring how- # the REST next-URL paginator advances the other list endpoints- # (it preserves the POST method + body and only swaps the URL).- payload = _intercom_post(session, next_url, body)- yield from (payload.get("data") or [])- next_url = (payload.get("pages") or {}).get("next")- if not next_url:+ params = {"scroll_param": scroll_param} if scroll_param else None+ payload = _intercom_get(session, "/companies/scroll", params=params)+ data = payload.get("data") or []+ if not data:+ return+ yield from data+ scroll_param = payload.get("scroll_param")+ if not scroll_param:returndiff --git a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.pyindex 5864680c8503..76f762324126 100644--- a/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py+++ b/posthog/temporal/data_imports/sources/intercom/tests/test_intercom.py@@ -300,13 +300,15 @@ class TestSubstreamGenerators:list(_conversation_parts_generator(mock_session, "updated_at", None))def test_company_segments_skips_404_parent(self):+ # The parent companies walk (scroll) and the per-company segments fetch+ # both go through GET, so the calls interleave on `session.get` in order:+ # scroll page -> segments(co1) -> segments(co2) -> empty scroll page.mock_session = mock.MagicMock()- mock_session.post.side_effect = [- _make_response({"data": [{"id": "co1"}, {"id": "co2"}], "pages": {}}),- ]mock_session.get.side_effect = [+ _make_response({"data": [{"id": "co1"}, {"id": "co2"}], "scroll_param": "s1"}),_make_response(None, status_code=404, text="Not Found"),_make_response({"data": [{"id": "s2"}]}),+ _make_response({"data": [], "scroll_param": "s2"}),]segments = list(_company_segments_generator(mock_session))@@ -316,10 +318,8 @@ class TestSubstreamGenerators:def test_company_segments_reraises_non_404(self):mock_session = mock.MagicMock()- mock_session.post.side_effect = [- _make_response({"data": [{"id": "co1"}], "pages": {}}),- ]mock_session.get.side_effect = [+ _make_response({"data": [{"id": "co1"}], "scroll_param": "s1"}),_make_response(None, status_code=500, text="Server Error"),]@@ -328,12 +328,11 @@ class TestSubstreamGenerators:def test_company_segments_injects_company_id(self):mock_session = mock.MagicMock()- mock_session.post.side_effect = [- _make_response({"data": [{"id": "co1"}, {"id": "co2"}], "pages": {}}),- ]mock_session.get.side_effect = [+ _make_response({"data": [{"id": "co1"}, {"id": "co2"}], "scroll_param": "s1"}),_make_response({"data": [{"id": "s1"}]}),_make_response({"data": [{"id": "s2"}, {"id": "s3"}]}),+ _make_response({"data": [], "scroll_param": "s2"}),]segments = list(_company_segments_generator(mock_session))@@ -342,28 +341,38 @@ class TestSubstreamGenerators:assert segments[0]["company_id"] == "co1"assert segments[1]["company_id"] == "co2"- def test_iter_companies_follows_next_url_with_post(self):- # `/companies/list` is POST-only; its `pages.next` URL carries the page- # cursor in the query string. Following it with GET 404s in production,- # so every page — including subsequent ones — must be a POST carrying- # the same body.- next_url = f"{INTERCOM_API_BASE}/companies/list?per_page=60&page=2"+ def test_iter_companies_walks_scroll(self):+ # `POST /companies/list` is capped at 10,000 companies (60 * 167 page+ # crosses the ceiling and Intercom 400s). The Scroll API has no ceiling:+ # the first GET carries no param, subsequent GETs feed `scroll_param`+ # back, and the walk ends when `data` comes back empty.mock_session = mock.MagicMock()- mock_session.post.side_effect = [- _make_response({"data": [{"id": "co1"}], "pages": {"next": next_url}}),- _make_response({"data": [{"id": "co2"}], "pages": {}}),+ mock_session.get.side_effect = [+ _make_response({"data": [{"id": "co1"}], "scroll_param": "s1"}),+ _make_response({"data": [{"id": "co2"}], "scroll_param": "s2"}),+ _make_response({"data": [], "scroll_param": "s3"}),]companies = list(_iter_companies(mock_session))assert [c["id"] for c in companies] == ["co1", "co2"]- # Second page is POSTed to the next-page URL with the same body — not- # GET (the production 404 this guards against).- assert mock_session.post.call_args_list[1].args[0] == next_url- assert mock_session.post.call_args_list[1].kwargs["json"] == {- "per_page": INTERCOM_ENDPOINTS["companies"].page_size- }- assert mock_session.get.call_count == 0+ calls = mock_session.get.call_args_list+ assert calls[0].kwargs["params"] is None+ assert calls[1].kwargs["params"] == {"scroll_param": "s1"}+ assert calls[2].kwargs["params"] == {"scroll_param": "s2"}+ # Every call hits the un-capped scroll endpoint, never the 10k-capped+ # `/companies/list` (POST) path that produced the 400.+ assert all(call.args[0].endswith("/companies/scroll") for call in calls)+ assert mock_session.post.call_count == 0++ def test_iter_companies_stops_on_empty_first_page(self):+ # A workspace with no companies returns empty data on the first scroll+ # request — the walk must terminate without a second call.+ mock_session = mock.MagicMock()+ mock_session.get.side_effect = [_make_response({"data": [], "scroll_param": "s1"})]++ assert list(_iter_companies(mock_session)) == []+ assert mock_session.get.call_count == 1class TestSubstreamSessionRetries: