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.

feat(warehouse): add HubSpot leads table (default-disabled) (#73909)

Luke Belton4d6fba8b48b7
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index 86fee7d7a9d4..9e1e295a1dfa 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -683,8 +683,10 @@ class OauthIntegration:
client_secret=settings.HUBSPOT_APP_CLIENT_SECRET,
scope="tickets crm.objects.contacts.write sales-email-read crm.objects.companies.read crm.objects.deals.read crm.objects.contacts.read crm.objects.quotes.read crm.objects.companies.write",
additional_authorize_params={
- # NOTE: these scopes are only available on certain hubspot plans and as such are optional
- "optional_scope": "analytics.behavioral_events.send behavioral_events.event_definitions.read_write"
+ # NOTE: these scopes are only available on certain hubspot plans and as such are optional.
+ # crm.objects.leads.read is Sales Hub Pro+/Enterprise only — requesting it as a
+ # mandatory scope would fail the whole authorization for portals that lack it.
+ "optional_scope": "analytics.behavioral_events.send behavioral_events.event_definitions.read_write crm.objects.leads.read"
},
id_path="hub_id",
name_path="hub_domain",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/hubspot.py b/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/hubspot.py
index 72737ccadb7a..9c59228a7dd2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/hubspot.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/hubspot.py
@@ -14,6 +14,7 @@ class HubspotCustomPropertiesConfig(config.Config):
quotes_properties: str | None = None
emails_properties: str | None = None
meetings_properties: str | None = None
+ leads_properties: str | None = None
@config.config
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/canonical_descriptions.py
index bee10d056ca0..550502f78518 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/canonical_descriptions.py
@@ -131,4 +131,18 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"hs_meeting_source": "How the meeting was created (e.g. CRM_UI, MEETINGS_PUBLIC).",
},
},
+ "leads": {
+ "description": "A potential customer tracked in HubSpot CRM, associated with a contact or company.",
+ "docs_url": "https://developers.hubspot.com/docs/api/crm/leads",
+ "columns": {
+ "hs_object_id": "HubSpot's unique internal identifier for the lead.",
+ "hs_lead_name": "The lead's name.",
+ "hs_lead_type": "The type of lead (e.g. new business).",
+ "hs_lead_label": "The current status label of the lead (e.g. warm, cold).",
+ "hs_pipeline": "The pipeline the lead is in.",
+ "hs_pipeline_stage": "The pipeline stage the lead is in (e.g. new, attempting, connected, qualified).",
+ "hs_createdate": "Date the lead was created in HubSpot.",
+ "hs_lastmodifieddate": "Date any property on the lead was last modified.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/settings.py
index 201e87a777dd..b2b8481efd9a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/settings.py
@@ -40,6 +40,7 @@ TICKET = "ticket"
QUOTE = "quote"
EMAILS = "emails"
MEETINGS = "meetings"
+LEAD = "lead"
CRM_CONTACTS_ENDPOINT = "/crm/v3/objects/contacts?associations=deals,tickets,quotes"
CRM_COMPANIES_ENDPOINT = "/crm/v3/objects/companies?associations=contacts,deals,tickets,quotes"
@@ -70,6 +71,7 @@ OBJECT_TYPE_SINGULAR = {
"quotes": QUOTE,
"emails": EMAILS,
"meetings": MEETINGS,
+ "leads": LEAD,
}
OBJECT_TYPE_PLURAL = {v: k for k, v in OBJECT_TYPE_SINGULAR.items()}
@@ -83,6 +85,7 @@ ENDPOINTS = (
OBJECT_TYPE_PLURAL[QUOTE],
OBJECT_TYPE_PLURAL[EMAILS],
OBJECT_TYPE_PLURAL[MEETINGS],
+ OBJECT_TYPE_PLURAL[LEAD],
)
# CRM search API constants — shared between windowing and pagination logic
@@ -175,6 +178,19 @@ DEFAULT_MEETINGS_PROPS = [
"hs_attachment_ids",
]
+# Lead-specific standard properties are all hs_-prefixed, so custom-property auto-discovery (which
+# only pulls non-hs_ props) won't surface them — they must be listed here explicitly.
+DEFAULT_LEAD_PROPS = [
+ "hs_createdate",
+ "hs_lastmodifieddate",
+ "hs_lead_label",
+ "hs_lead_name",
+ "hs_lead_type",
+ "hs_object_id",
+ "hs_pipeline",
+ "hs_pipeline_stage",
+]
+
DEFAULT_PROPS = {
OBJECT_TYPE_PLURAL[CONTACT]: DEFAULT_CONTACT_PROPS,
OBJECT_TYPE_PLURAL[COMPANY]: DEFAULT_COMPANY_PROPS,
@@ -183,6 +199,7 @@ DEFAULT_PROPS = {
OBJECT_TYPE_PLURAL[QUOTE]: DEFAULT_QUOTE_PROPS,
OBJECT_TYPE_PLURAL[EMAILS]: DEFAULT_EMAIL_PROPS,
OBJECT_TYPE_PLURAL[MEETINGS]: DEFAULT_MEETINGS_PROPS,
+ OBJECT_TYPE_PLURAL[LEAD]: DEFAULT_LEAD_PROPS,
}
@@ -205,6 +222,9 @@ class HubspotEndpointConfig:
# Name of the HubSpot property used both as the search filter and as the incremental cursor
# (e.g. hs_lastmodifieddate). None means the endpoint does not support incremental sync.
cursor_filter_property_field: Optional[str] = None
+ # Whether this endpoint is selected for sync by default. False keeps a table off unless the
+ # user opts in — used for objects that need an OAuth scope existing connections lack (leads).
+ should_sync_default: bool = True
HUBSPOT_ENDPOINTS: dict[str, HubspotEndpointConfig] = {
@@ -264,4 +284,16 @@ HUBSPOT_ENDPOINTS: dict[str, HubspotEndpointConfig] = {
cursor_filter_property_field="hs_lastmodifieddate",
incremental_fields=[_incremental_field("hs_lastmodifieddate")],
),
+ # Leads need the `crm.objects.leads.read` scope, which existing connections weren't granted,
+ # and the object must be enabled in the portal (Sales Hub Pro+). Default-disabled so existing
+ # customers aren't opted in — they reconnect (picking up the scope) before enabling it.
+ "leads": HubspotEndpointConfig(
+ name="leads",
+ path="/crm/v3/objects/leads",
+ associations=[],
+ partition_key="hs_createdate",
+ cursor_filter_property_field="hs_lastmodifieddate",
+ incremental_fields=[_incremental_field("hs_lastmodifieddate")],
+ should_sync_default=False,
+ ),
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
index 46d38a2cdc71..9c412d653640 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
@@ -172,6 +172,7 @@ class HubspotSource(ResumableSource[HubspotSourceConfig | HubspotSourceOldConfig
supports_incremental=supports_incremental,
supports_append=supports_incremental,
incremental_fields=endpoint_config.incremental_fields,
+ should_sync_default=endpoint_config.should_sync_default,
)
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
index 9acffb3de811..f885f9f84fee 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
@@ -51,7 +51,7 @@ class TestGetSchemas:
schemas = src.get_schemas(MagicMock(), team_id=1)
by_name = {s.name: s for s in schemas}
- # All seven endpoints currently have cursor properties, so all should support incremental.
+ # Every endpoint currently has a cursor property, so all should support incremental.
assert set(by_name.keys()) == set(HUBSPOT_ENDPOINTS.keys())
for name, schema in by_name.items():
endpoint_config = HUBSPOT_ENDPOINTS[name]
@@ -63,6 +63,15 @@ class TestGetSchemas:
assert schema.incremental_fields
assert schema.incremental_fields[0]["field"] == expected_field
+ def test_leads_is_default_disabled_others_default_enabled(self) -> None:
+ # Leads needs the crm.objects.leads.read scope existing connections lack, so it must start
+ # off; flipping it on by default would 403 every existing customer's sync.
+ src = HubspotSource()
+ by_name = {s.name: s for s in src.get_schemas(MagicMock(), team_id=1)}
+
+ assert by_name["leads"].should_sync_default is False
+ assert all(s.should_sync_default for name, s in by_name.items() if name != "leads")
+
def test_filters_by_names(self) -> None:
src = HubspotSource()
schemas = src.get_schemas(MagicMock(), team_id=1, names=["deals", "contacts"])