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(warehouse-sources): fan out klaviyo coupon codes over coupons (#77318)

Tom Owers32bf1b03e50c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/canonical_descriptions.py
index d173042242bd..4807c1e99e86 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/canonical_descriptions.py
@@ -356,9 +356,10 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
},
"coupon_codes": {
"description": "A unique coupon code assigned to a profile.",
- "docs_url": "https://developers.klaviyo.com/en/reference/get_coupon_codes",
+ "docs_url": "https://developers.klaviyo.com/en/reference/get_coupon_coupon_codes",
"columns": {
"id": "Unique identifier for the coupon code.",
+ "coupon_id": "Identifier of the coupon this code was issued from.",
"unique_code": "The code assigned to the profile.",
"expires_at": "The datetime when the code expires.",
"status": "Current status of the code (e.g. unassigned, assigned_to_profile, used).",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/settings.py
index 98e049e5db42..78166fc55f95 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/settings.py
@@ -569,11 +569,18 @@ KLAVIYO_ENDPOINTS: dict[str, KlaviyoEndpointConfig] = {
page_size=100,
incremental_fields=[],
),
+ # Klaviyo requires a filter on coupon.id or profile.id on the flat /coupon-codes collection, so
+ # it can't be listed directly; fan out over /coupons and pull each coupon's codes instead.
"coupon_codes": KlaviyoEndpointConfig(
name="coupon_codes",
- path="/coupon-codes",
+ path="/coupons/{coupon_id}/coupon-codes",
page_size=100,
incremental_fields=[],
+ fan_out=KlaviyoFanOutConfig(
+ parent_path="/coupons",
+ parent_page_size=100,
+ parent_id_column="coupon_id",
+ ),
),
"tags": KlaviyoEndpointConfig(
name="tags",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py
index 80802fb0b6a2..4e5776e6cbbf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/klaviyo/test_klaviyo.py
@@ -574,6 +574,36 @@ class TestGeneralizedFanOut:
}
]
+ def test_coupon_codes_fan_out_over_coupons_instead_of_the_unfiltered_collection(self, monkeypatch: Any) -> None:
+ # Klaviyo's flat /coupon-codes list requires a coupon.id or profile.id filter and 400s
+ # without one; fanning out per coupon avoids ever calling that unfiltered endpoint.
+ pages = {
+ "https://a.klaviyo.com/api/coupons?page[size]=100": {
+ "data": [{"id": "C1"}],
+ "links": {"next": None},
+ },
+ "https://a.klaviyo.com/api/coupons/C1/coupon-codes?page[size]=100": {
+ "data": [
+ {
+ "type": "coupon-code",
+ "id": "C1-CODE1",
+ "attributes": {"unique_code": "CODE1", "status": "UNASSIGNED"},
+ }
+ ],
+ "links": {"next": None},
+ },
+ }
+ rows = _collect_rows("coupon_codes", monkeypatch, pages)
+ assert rows == [
+ {
+ "type": "coupon-code",
+ "id": "C1-CODE1",
+ "unique_code": "CODE1",
+ "status": "UNASSIGNED",
+ "coupon_id": "C1",
+ }
+ ]
+
def test_flow_messages_walk_flows_then_actions_and_carry_both_ancestors(self, monkeypatch: Any) -> None:
# Two-level fan-out: the intermediate path must be formatted with the grandparent id, and
# each row must carry both ancestors or the flow -> action -> message chain can't be rebuilt.