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(rokt-ads): add campaign, creative and audience metadata tables (#128403)

Tom Owers36cee54f9d66
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
index 0a976bea12f4..3198eb341a5a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
@@ -10404,9 +10404,9 @@ Today (6): `Accounts`, `AudiencePerformance`, `CampaignDemographics`, `CampaignP
Diffed against: <https://docs.rokt.com/developer-reference/apis/reporting-apis/reporting-api/>
-- [ ] `Campaign Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId})` — resolve campaign IDs from performance reports to campaign type, country, and current status (high)
-- [ ] `Creative Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId}/creatives/{creativeId})` — resolve creative IDs to advertisement text, response options, and current status (high)
-- [ ] `Audience Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId}/audiences/{audienceId})` — interpret audience performance using age limits, gender, and device targeting (medium)
+- [x] `Campaign Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId})` — resolve campaign IDs from performance reports to campaign type, country, and current status (high). Added as `CampaignMetadata`.
+- [x] `Creative Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId}/creatives/{creativeId})` — resolve creative IDs to advertisement text, response options, and current status (high). Added as `CreativeMetadata`.
+- [x] `Audience Metadata (GET /metadata/accounts/{accountId}/campaigns/{campaignId}/audiences/{audienceId})` — interpret audience performance using age limits, gender, and device targeting (medium). Added as `AudienceMetadata`; it needs the `audience_id` Query API dimension to discover IDs.
Note: Both official reporting references were fetched with curl and their operations and migration mappings were parsed. Existing tables cover campaign and transaction Query API reports. The legacy reference explicitly keeps these three metadata endpoints available because Query API replacements do not exist. Metadata requires requests for each parent; audience IDs need separate discovery because the current audience report stores names.
diff --git a/products/warehouse_sources/sources/rokt_ads/canonical_descriptions.py b/products/warehouse_sources/sources/rokt_ads/canonical_descriptions.py
index e802dc249931..859654cfbbb8 100644
--- a/products/warehouse_sources/sources/rokt_ads/canonical_descriptions.py
+++ b/products/warehouse_sources/sources/rokt_ads/canonical_descriptions.py
@@ -1,6 +1,7 @@
from products.warehouse_sources.sources.sdk import CanonicalDescriptions
QUERY_API_DOCS = "https://docs.rokt.com/developers/api-reference/reporting/query-api/"
+METADATA_API_DOCS = "https://docs.rokt.com/developer-reference/apis/reporting-apis/reporting-api/"
# Shared advertiser metric columns. Every campaigns-resource table reports the same metric set, so
# the descriptions are written once and spread into each table below.
@@ -110,4 +111,45 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"referral_rate": "Referrals per impression.",
},
},
+ "CampaignMetadata": {
+ "description": "Current settings of each campaign the account reported on, one row per campaign.",
+ "docs_url": METADATA_API_DOCS,
+ "columns": {
+ "accountId": "Identifier of the Rokt account that owns the campaign.",
+ "campaignId": "Identifier of the campaign.",
+ "campaignName": "Name of the campaign.",
+ "campaignType": "Type of the campaign.",
+ "campaignObjective": "Objective the campaign optimizes for.",
+ "countryCode": "Country the campaign runs in.",
+ "status": "Current status of the campaign.",
+ },
+ },
+ "CreativeMetadata": {
+ "description": "Current content of each creative the account reported on, one row per campaign and creative.",
+ "docs_url": METADATA_API_DOCS,
+ "columns": {
+ "accountId": "Identifier of the Rokt account that owns the creative.",
+ "campaignId": "Identifier of the campaign the creative belongs to.",
+ "creativeId": "Identifier of the creative.",
+ "name": "Name of the creative.",
+ "title": "Title text of the creative.",
+ "subtitle": "Subtitle text of the creative.",
+ "text": "Body text of the creative.",
+ "responses": "Response options the creative shows to the user.",
+ "status": "Current status of the creative.",
+ },
+ },
+ "AudienceMetadata": {
+ "description": "Targeting settings of each audience the account reported on, one row per campaign and audience.",
+ "docs_url": METADATA_API_DOCS,
+ "columns": {
+ "accountId": "Identifier of the Rokt account that owns the audience.",
+ "campaignId": "Identifier of the campaign the audience belongs to.",
+ "audienceId": "Identifier of the audience.",
+ "name": "Name of the audience.",
+ "ageRange": "Minimum and maximum age the audience targets.",
+ "device": "Device types the audience targets: desktop, tablet and mobile.",
+ "gender": "Gender the audience targets.",
+ },
+ },
}
diff --git a/products/warehouse_sources/sources/rokt_ads/rokt_ads.py b/products/warehouse_sources/sources/rokt_ads/rokt_ads.py
index 3625b05dec37..91948a46b6aa 100644
--- a/products/warehouse_sources/sources/rokt_ads/rokt_ads.py
+++ b/products/warehouse_sources/sources/rokt_ads/rokt_ads.py
@@ -2,6 +2,7 @@ import base64
from collections.abc import Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
+from urllib.parse import quote
from requests import Response, Session
from requests.exceptions import HTTPError
@@ -9,8 +10,10 @@ from requests.exceptions import HTTPError
from products.warehouse_sources.sources.rokt_ads.settings import (
ACCOUNTS_ENDPOINT,
BASE_URL,
+ DISCOVERY_METRICS,
ENDPOINTS,
INITIAL_BACKFILL_DAYS,
+ METADATA_ENDPOINTS,
REPORT_INTERVAL,
TOKEN_URL,
WINDOW_DAYS,
@@ -144,6 +147,19 @@ class RoktAdsClient:
self._raise_for_status(response)
return response.json()
+ def get_metadata(self, path: str) -> Optional[dict[str, Any]]:
+ """Read one metadata object, or None when Rokt no longer has it."""
+ try:
+ payload = self.get(path)
+ except RoktAdsError as err:
+ # An ID from an old report can point at a campaign, creative or audience Rokt has since
+ # removed. Skip it, because the shared 404 rule would otherwise fail the whole sync.
+ cause = err.__cause__
+ if isinstance(cause, HTTPError) and cause.response is not None and cause.response.status_code == 404:
+ return None
+ raise
+ return payload if isinstance(payload, dict) else None
+
def list_accounts(self) -> list[dict[str, Any]]:
payload = self.get("/v1/query/accounts")
return payload if isinstance(payload, list) else []
@@ -195,6 +211,85 @@ def resolve_start_date(db_incremental_field_last_value: Optional[Any], today: da
return cursor
+def _require_dimensions(endpoint_name: str, dimensions: list[str], capabilities: ReportCapabilities) -> None:
+ missing_dimensions = sorted(set(dimensions) - capabilities.dimensions)
+ if missing_dimensions:
+ raise RoktAdsError(
+ f"Rokt account cannot report on {', '.join(missing_dimensions)}, which {endpoint_name} needs "
+ f"to identify a row. Deselect this table or ask Rokt to enable those dimensions."
+ )
+
+
+def _granted_metrics(endpoint_name: str, metrics: list[str], capabilities: ReportCapabilities) -> list[str]:
+ granted = [metric for metric in metrics if metric in capabilities.metrics]
+ if not granted:
+ raise RoktAdsError(f"Rokt account grants none of the metrics {endpoint_name} reports on.")
+ return granted
+
+
+def build_discovery_body(endpoint_name: str, window: DateWindow, capabilities: ReportCapabilities) -> dict[str, Any]:
+ """Ask for one row per distinct ID tuple in the window. No `interval`, so days do not multiply rows."""
+ endpoint = METADATA_ENDPOINTS[endpoint_name]
+ _require_dimensions(endpoint_name, endpoint["discovery_dimensions"], capabilities)
+ metrics = _granted_metrics(endpoint_name, DISCOVERY_METRICS, capabilities)
+ return {
+ "startDate": window.start.isoformat(),
+ "endDate": window.end.isoformat(),
+ "metrics": metrics[:1],
+ "dimensions": endpoint["discovery_dimensions"],
+ }
+
+
+def _id_tuple(row: dict[str, Any], dimensions: list[str]) -> Optional[tuple[str, ...]]:
+ values = [row.get(dimension) for dimension in dimensions]
+ if any(value is None or str(value).strip() == "" for value in values):
+ return None
+ return tuple(str(value) for value in values)
+
+
+def rokt_ads_metadata_rows(
+ client: RoktAdsClient,
+ account_id: str,
+ endpoint_name: str,
+ resumable_source_manager: ResumableSourceManager[RoktAdsResumeConfig],
+ today: Optional[date] = None,
+) -> Iterator[dict[str, Any]]:
+ """Discover IDs window by window over the backfill range, then read each new ID's metadata once.
+
+ A full refresh: the set of IDs comes from performance reports, so there is no server-side
+ timestamp to filter metadata on. One row is yielded per metadata request, and a safe point
+ follows each request that yields nothing.
+ """
+ endpoint = METADATA_ENDPOINTS[endpoint_name]
+ capabilities = client.report_capabilities(account_id, "campaigns")
+
+ current_day = today or datetime.now(tz=UTC).date()
+ start_date = current_day - timedelta(days=INITIAL_BACKFILL_DAYS)
+ end_date = current_day + timedelta(days=1)
+
+ seen: set[tuple[str, ...]] = set()
+ for window in _date_windows(start_date, end_date, WINDOW_DAYS):
+ body = build_discovery_body(endpoint_name, window, capabilities)
+ report_rows = client.run_report(account_id, "campaigns", body)
+ resumable_source_manager.safe_point()
+
+ for report_row in report_rows:
+ ids = _id_tuple(report_row, endpoint["discovery_dimensions"])
+ if ids is None or ids in seen:
+ continue
+ seen.add(ids)
+
+ path = endpoint["path"].format(
+ *(quote(value, safe="") for value in ids), account_id=quote(account_id, safe="")
+ )
+ metadata = client.get_metadata(path)
+ if metadata is None:
+ resumable_source_manager.safe_point()
+ continue
+ # The path IDs win, so the primary key is always set even if Rokt omits a field.
+ yield {**metadata, **dict(zip(endpoint["id_fields"], ids))}
+
+
def build_report_body(
endpoint_name: str,
window: DateWindow,
@@ -209,17 +304,8 @@ def build_report_body(
primary key that no longer identifies them.
"""
endpoint = ENDPOINTS[endpoint_name]
-
- missing_dimensions = sorted(set(endpoint["dimensions"]) - capabilities.dimensions)
- if missing_dimensions:
- raise RoktAdsError(
- f"Rokt account cannot report on {', '.join(missing_dimensions)}, which {endpoint_name} needs "
- f"to identify a row. Deselect this table or ask Rokt to enable those dimensions."
- )
-
- metrics = [metric for metric in endpoint["metrics"] if metric in capabilities.metrics]
- if not metrics:
- raise RoktAdsError(f"Rokt account grants none of the metrics {endpoint_name} reports on.")
+ _require_dimensions(endpoint_name, endpoint["dimensions"], capabilities)
+ metrics = _granted_metrics(endpoint_name, endpoint["metrics"], capabilities)
# No `orderBys`: the Query API only sorts on a requested dimension or metric slug, and
# `datetime` is a response-only interval marker rather than a slug, so ordering by it makes
diff --git a/products/warehouse_sources/sources/rokt_ads/settings.py b/products/warehouse_sources/sources/rokt_ads/settings.py
index 8e306ba2e408..9c850c760156 100644
--- a/products/warehouse_sources/sources/rokt_ads/settings.py
+++ b/products/warehouse_sources/sources/rokt_ads/settings.py
@@ -25,6 +25,16 @@ ReportKind = Literal["campaigns", "transactions"]
ACCOUNTS_ENDPOINT = "Accounts"
+class MetadataEndpoint(TypedDict):
+ # Query API dimensions whose distinct values are the path IDs of one metadata request.
+ discovery_dimensions: list[str]
+ # Path under BASE_URL, filled with the account ID and one discovered value per dimension.
+ path: str
+ # Response keys that hold the path IDs, in discovery_dimensions order.
+ id_fields: list[str]
+ primary_key: list[str]
+
+
class ReportEndpoint(TypedDict):
kind: ReportKind
dimensions: list[str]
@@ -109,11 +119,39 @@ ENDPOINTS: dict[str, ReportEndpoint] = {
},
}
-SCHEMA_NAMES = [ACCOUNTS_ENDPOINT, *ENDPOINTS]
+# Rokt has no list endpoint for campaigns, creatives or audiences, so each metadata table discovers
+# its IDs from the Query API, then reads the legacy metadata endpoint once per ID. Rokt keeps these
+# endpoints because the Query API has no replacement for them.
+METADATA_ENDPOINTS: dict[str, MetadataEndpoint] = {
+ "CampaignMetadata": {
+ "discovery_dimensions": ["campaign_id"],
+ "path": "/metadata/accounts/{account_id}/campaigns/{0}",
+ "id_fields": ["campaignId"],
+ "primary_key": ["campaignId"],
+ },
+ "CreativeMetadata": {
+ "discovery_dimensions": ["campaign_id", "creative_id"],
+ "path": "/metadata/accounts/{account_id}/campaigns/{0}/creatives/{1}",
+ "id_fields": ["campaignId", "creativeId"],
+ "primary_key": ["campaignId", "creativeId"],
+ },
+ "AudienceMetadata": {
+ "discovery_dimensions": ["campaign_id", "audience_id"],
+ "path": "/metadata/accounts/{account_id}/campaigns/{0}/audiences/{1}",
+ "id_fields": ["campaignId", "audienceId"],
+ "primary_key": ["campaignId", "audienceId"],
+ },
+}
+
+# Discovery only needs a row per ID, so it asks for the first of these metrics the account grants.
+DISCOVERY_METRICS = ["impressions", "referrals", "gross_cost"]
+
+SCHEMA_NAMES = [ACCOUNTS_ENDPOINT, *ENDPOINTS, *METADATA_ENDPOINTS]
PRIMARY_KEYS: dict[str, list[str]] = {
ACCOUNTS_ENDPOINT: ["accountId"],
**{name: endpoint["primary_key"] for name, endpoint in ENDPOINTS.items()},
+ **{name: endpoint["primary_key"] for name, endpoint in METADATA_ENDPOINTS.items()},
}
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
@@ -135,4 +173,7 @@ DESCRIPTIONS = {
"AudiencePerformance": "Daily advertiser metrics per targeted audience.",
"CampaignDemographics": "Daily advertiser metrics split by age range, gender and device.",
"TransactionPerformance": "Daily partner transaction and impression metrics.",
+ "CampaignMetadata": "Type, objective, country and status of each campaign.",
+ "CreativeMetadata": "Ad text, response options and status of each creative.",
+ "AudienceMetadata": "Age range, gender and device targeting of each audience.",
}
diff --git a/products/warehouse_sources/sources/rokt_ads/source.py b/products/warehouse_sources/sources/rokt_ads/source.py
index 35853bf151ec..2421b4453000 100644
--- a/products/warehouse_sources/sources/rokt_ads/source.py
+++ b/products/warehouse_sources/sources/rokt_ads/source.py
@@ -4,14 +4,16 @@ from products.warehouse_sources.sources.rokt_ads._config import RoktAdsSourceCon
from products.warehouse_sources.sources.rokt_ads.rokt_ads import (
RoktAdsClient,
RoktAdsResumeConfig,
+ rokt_ads_metadata_rows,
rokt_ads_source,
validate_credentials as validate_rokt_credentials,
)
from products.warehouse_sources.sources.rokt_ads.settings import (
- ACCOUNTS_ENDPOINT,
DESCRIPTIONS,
+ ENDPOINTS,
INCREMENTAL_FIELDS,
INCREMENTAL_LOOKBACK_SECONDS,
+ METADATA_ENDPOINTS,
PRIMARY_KEYS,
SCHEMA_NAMES,
)
@@ -137,6 +139,13 @@ class RoktAdsSource(ResumableSource[RoktAdsSourceConfig, RoktAdsResumeConfig]):
endpoint_name = inputs.schema_name
def items():
+ if endpoint_name in METADATA_ENDPOINTS:
+ return rokt_ads_metadata_rows(
+ client=client,
+ account_id=config.account_id,
+ endpoint_name=endpoint_name,
+ resumable_source_manager=resumable_source_manager,
+ )
return rokt_ads_source(
client=client,
account_id=config.account_id,
@@ -149,7 +158,7 @@ class RoktAdsSource(ResumableSource[RoktAdsSourceConfig, RoktAdsResumeConfig]):
currency_code=config.currency_code or None,
)
- is_report = endpoint_name != ACCOUNTS_ENDPOINT
+ is_report = endpoint_name in ENDPOINTS
return SourceResponse(
name=endpoint_name,
items=items,
diff --git a/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads.py b/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads.py
index 286323fbb1bd..d55558105ef4 100644
--- a/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads.py
+++ b/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads.py
@@ -14,13 +14,22 @@ from products.warehouse_sources.sources.rokt_ads.rokt_ads import (
RoktAdsError,
RoktAdsResumeConfig,
_date_windows,
+ build_discovery_body,
build_report_body,
resolve_start_date,
+ rokt_ads_metadata_rows,
rokt_ads_source,
)
-from products.warehouse_sources.sources.rokt_ads.settings import CAMPAIGN_METRICS, ENDPOINTS, INITIAL_BACKFILL_DAYS
+from products.warehouse_sources.sources.rokt_ads.settings import (
+ CAMPAIGN_METRICS,
+ ENDPOINTS,
+ INITIAL_BACKFILL_DAYS,
+ METADATA_ENDPOINTS,
+)
-ALL_DIMENSIONS = {dimension for endpoint in ENDPOINTS.values() for dimension in endpoint["dimensions"]}
+ALL_DIMENSIONS = {dimension for endpoint in ENDPOINTS.values() for dimension in endpoint["dimensions"]} | {
+ dimension for endpoint in METADATA_ENDPOINTS.values() for dimension in endpoint["discovery_dimensions"]
+}
ALL_METRICS = set(CAMPAIGN_METRICS)
ALL_CAPABILITIES = ReportCapabilities(dimensions=ALL_DIMENSIONS, metrics=ALL_METRICS)
MARCH_WINDOW = DateWindow(start=date(2026, 3, 1), end=date(2026, 4, 1))
@@ -155,6 +164,100 @@ class TestRoktAdsSource:
assert client.run_report.call_args_list[0].args[2]["startDate"] == "2026-08-10"
+class TestBuildDiscoveryBody:
+ def test_asks_for_one_row_per_id_and_not_one_per_day(self):
+ body = build_discovery_body(
+ "CreativeMetadata",
+ MARCH_WINDOW,
+ ReportCapabilities(dimensions=ALL_DIMENSIONS, metrics={"referrals", "gross_cost"}),
+ )
+ assert "interval" not in body
+ assert body["dimensions"] == ["campaign_id", "creative_id"]
+ assert body["metrics"] == ["referrals"]
+
+
+class TestRoktAdsMetadataRows:
+ TODAY = date(2026, 8, 13)
+
+ def _client(self, report_rows_per_window: list[list[dict[str, Any]]], metadata: Any = None) -> MagicMock:
+ client = MagicMock()
+ client.report_capabilities.return_value = ALL_CAPABILITIES
+ windows = list(_date_windows(date(2024, 8, 13), date(2026, 8, 14), 31))
+ responses = report_rows_per_window + [[] for _ in range(len(windows) - len(report_rows_per_window))]
+ client.run_report.side_effect = responses
+ client.get_metadata.side_effect = metadata or (lambda path: {"path": path, "status": "Active"})
+ return client
+
+ @parameterized.expand(
+ [
+ (
+ "CampaignMetadata",
+ [[{"campaign_id": "c1"}, {"campaign_id": "c2"}], [{"campaign_id": "c1"}, {"campaign_id": "c3"}]],
+ [
+ ("/metadata/accounts/acc_1/campaigns/c1", {"campaignId": "c1"}),
+ ("/metadata/accounts/acc_1/campaigns/c2", {"campaignId": "c2"}),
+ ("/metadata/accounts/acc_1/campaigns/c3", {"campaignId": "c3"}),
+ ],
+ ),
+ (
+ "CreativeMetadata",
+ # The same creative ID under two campaigns is two rows.
+ [[{"campaign_id": "c1", "creative_id": "k1"}], [{"campaign_id": "c2", "creative_id": "k1"}]],
+ [
+ ("/metadata/accounts/acc_1/campaigns/c1/creatives/k1", {"campaignId": "c1", "creativeId": "k1"}),
+ ("/metadata/accounts/acc_1/campaigns/c2/creatives/k1", {"campaignId": "c2", "creativeId": "k1"}),
+ ],
+ ),
+ (
+ "AudienceMetadata",
+ [[{"campaign_id": "c1", "audience_id": "a1"}, {"campaign_id": "c1", "audience_id": None}]],
+ [("/metadata/accounts/acc_1/campaigns/c1/audiences/a1", {"campaignId": "c1", "audienceId": "a1"})],
+ ),
+ ]
+ )
+ def test_reads_each_discovered_id_once(self, endpoint_name, report_rows_per_window, expected):
+ client = self._client(report_rows_per_window)
+
+ rows = list(rokt_ads_metadata_rows(client, "acc_1", endpoint_name, _manager(), today=self.TODAY))
+
+ assert [call.args[0] for call in client.get_metadata.call_args_list] == [path for path, _ in expected]
+ assert rows == [{"path": path, "status": "Active", **ids} for path, ids in expected]
+
+ def test_discovery_walks_the_whole_backfill_range_in_order(self):
+ client = self._client([])
+
+ list(rokt_ads_metadata_rows(client, "acc_1", "CampaignMetadata", _manager(), today=self.TODAY))
+
+ bodies = [call.args[2] for call in client.run_report.call_args_list]
+ assert bodies[0]["startDate"] == "2024-08-13"
+ assert bodies[-1]["endDate"] == "2026-08-14"
+ assert all(call.args[1] == "campaigns" for call in client.run_report.call_args_list)
+
+ def test_path_ids_win_over_a_missing_or_different_response_field(self):
+ client = self._client(
+ [[{"campaign_id": "c1", "creative_id": "k1"}]],
+ metadata=lambda path: {"campaignId": None, "name": "Spring sale"},
+ )
+
+ rows = list(rokt_ads_metadata_rows(client, "acc_1", "CreativeMetadata", _manager(), today=self.TODAY))
+
+ assert rows == [{"campaignId": "c1", "creativeId": "k1", "name": "Spring sale"}]
+
+ def test_skips_an_id_rokt_no_longer_has_and_reaches_a_safe_point(self):
+ client = self._client(
+ [[{"campaign_id": "gone"}, {"campaign_id": "c2"}]],
+ metadata=lambda path: None if path.endswith("/gone") else {"status": "Active"},
+ )
+ manager = _manager()
+
+ rows = list(rokt_ads_metadata_rows(client, "acc_1", "CampaignMetadata", manager, today=self.TODAY))
+
+ assert rows == [{"status": "Active", "campaignId": "c2"}]
+ # One safe point per discovery window, plus one for the skipped ID.
+ assert manager.safe_point.call_count == client.run_report.call_count + 1
+ manager.save_state.assert_not_called()
+
+
class TestRoktAdsClient:
def _client(self, token_session: MagicMock, session: MagicMock) -> RoktAdsClient:
return RoktAdsClient("app-id", "app-secret", session=session, token_session=token_session)
@@ -240,3 +343,24 @@ class TestRoktAdsClient:
# The status line lets get_non_retryable_errors classify this as permanent.
assert "400 Client Error" in message
assert expected_detail in message
+
+ def _metadata_client(self, status: int) -> RoktAdsClient:
+ token_session = MagicMock()
+ token_session.post.return_value = self._token_response()
+ session = MagicMock()
+ response = MagicMock()
+ response.status_code = status
+ response.json.return_value = {"message": "not found"} if status >= 400 else {"campaignId": "c1"}
+ if status >= 400:
+ response.raise_for_status.side_effect = HTTPError(f"{status} error", response=response)
+ session.get.return_value = response
+ return self._client(token_session, session)
+
+ @parameterized.expand([(200, {"campaignId": "c1"}), (404, None)])
+ def test_get_metadata_returns_the_object_or_none_when_it_is_gone(self, status: int, expected: Any):
+ assert self._metadata_client(status).get_metadata("/metadata/accounts/acc_1/campaigns/c1") == expected
+
+ @parameterized.expand([(401,), (403,), (500,)])
+ def test_get_metadata_raises_every_other_error(self, status: int):
+ with pytest.raises(RoktAdsError, match=str(status)):
+ self._metadata_client(status).get_metadata("/metadata/accounts/acc_1/campaigns/c1")
diff --git a/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads_source.py b/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads_source.py
index b6699c997cd8..771f26f6ffa3 100644
--- a/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads_source.py
+++ b/products/warehouse_sources/sources/rokt_ads/tests/test_rokt_ads_source.py
@@ -10,9 +10,16 @@ from products.warehouse_sources.sources.rokt_ads.rokt_ads import (
ReportCapabilities,
RoktAdsError,
RoktAdsResumeConfig,
+ build_discovery_body,
build_report_body,
)
-from products.warehouse_sources.sources.rokt_ads.settings import CAMPAIGN_METRICS, ENDPOINTS, PRIMARY_KEYS, SCHEMA_NAMES
+from products.warehouse_sources.sources.rokt_ads.settings import (
+ CAMPAIGN_METRICS,
+ ENDPOINTS,
+ METADATA_ENDPOINTS,
+ PRIMARY_KEYS,
+ SCHEMA_NAMES,
+)
from products.warehouse_sources.sources.rokt_ads.source import RoktAdsSource
from products.warehouse_sources.sources.sdk import ResumableSourceManager
from products.warehouse_sources.sources.sdk.testing import error_message_matches
@@ -94,6 +101,20 @@ class TestSourceForPipeline:
assert response.partition_format == "month"
assert response.partition_keys == ["datetime"]
+ @parameterized.expand(list(METADATA_ENDPOINTS))
+ def test_metadata_tables_use_the_metadata_transport_without_partitions(self, schema_name: str):
+ with (
+ patch(f"{SOURCE_MODULE}.RoktAdsClient"),
+ patch(f"{SOURCE_MODULE}.rokt_ads_source") as report_transport,
+ patch(f"{SOURCE_MODULE}.rokt_ads_metadata_rows") as metadata_transport,
+ ):
+ response = RoktAdsSource().source_for_pipeline(_config(), MagicMock(), _inputs(schema_name))
+ response.items()
+ assert metadata_transport.call_args.kwargs["endpoint_name"] == schema_name
+ report_transport.assert_not_called()
+ assert response.partition_mode is None
+ assert response.partition_keys is None
+
def test_full_refresh_run_passes_no_cursor(self):
with patch(f"{SOURCE_MODULE}.RoktAdsClient"), patch(f"{SOURCE_MODULE}.rokt_ads_source") as transport:
response = RoktAdsSource().source_for_pipeline(
@@ -151,3 +172,17 @@ class TestNonRetryableErrors:
errors = RoktAdsSource().get_non_retryable_errors()
assert error_message_matches(str(raised.value), errors.keys())
+
+ @parameterized.expand(
+ [
+ # `audience_id` is not on Rokt's public dimension list, so some accounts may not grant it.
+ ("missing_audience_id", ReportCapabilities(dimensions={"campaign_id"}, metrics=ALL_METRICS)),
+ ("no_discovery_metric", ReportCapabilities(dimensions={"campaign_id", "audience_id"}, metrics={"cpa"})),
+ ]
+ )
+ def test_an_account_that_cannot_list_audience_ids_stops_retrying(self, _name, capabilities):
+ with pytest.raises(RoktAdsError) as raised:
+ build_discovery_body("AudienceMetadata", MARCH_WINDOW, capabilities)
+
+ errors = RoktAdsSource().get_non_retryable_errors()
+ assert error_message_matches(str(raised.value), errors.keys())