Skip to content

Commit f47267d

Browse files
darynaishchenkojatinyadav-cc
authored andcommitted
🐛 Source Google Ads: Remove metrics from ad group for manager account (airbytehq#34212)
1 parent c008fb5 commit f47267d

File tree

7 files changed

+138
-105
lines changed

7 files changed

+138
-105
lines changed

airbyte-integrations/connectors/source-google-ads/acceptance-test-config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ acceptance_tests:
123123
tests:
124124
- config_path: "secrets/config.json"
125125
configured_catalog_path: "integration_tests/configured_catalog.json"
126+
- config_path: "secrets/config_manager_account.json"
126127
incremental:
127128
tests:
128129
- config_path: "secrets/incremental_config.json"

airbyte-integrations/connectors/source-google-ads/integration_tests/expected_records_click.jsonl

+4-4
Large diffs are not rendered by default.

airbyte-integrations/connectors/source-google-ads/metadata.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data:
1111
connectorSubtype: api
1212
connectorType: source
1313
definitionId: 253487c0-2246-43ba-a21f-5116b20a2c50
14-
dockerImageTag: 3.2.1
14+
dockerImageTag: 3.3.0
1515
dockerRepository: airbyte/source-google-ads
1616
documentationUrl: https://docs.airbyte.com/integrations/sources/google-ads
1717
githubIssueLabel: source-google-ads

airbyte-integrations/connectors/source-google-ads/source_google_ads/streams.py

+15
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,21 @@ class AdGroup(IncrementalGoogleAdsStream):
353353

354354
primary_key = ["ad_group.id", "segments.date"]
355355

356+
def get_query(self, stream_slice: Mapping[str, Any] = None) -> str:
357+
fields = GoogleAds.get_fields_from_schema(self.get_json_schema())
358+
# validation that the customer is not a manager
359+
# due to unsupported metrics.cost_micros field and removing it in case custom is a manager
360+
if [customer for customer in self.customers if customer.id == stream_slice["customer_id"]][0].is_manager_account:
361+
fields = [field for field in fields if field != "metrics.cost_micros"]
362+
table_name = get_resource_name(self.name)
363+
start_date, end_date = stream_slice.get("start_date"), stream_slice.get("end_date")
364+
cursor_condition = [f"{self.cursor_field} >= '{start_date}' AND {self.cursor_field} <= '{end_date}'"]
365+
366+
query = GoogleAds.convert_schema_into_query(
367+
fields=fields, table_name=table_name, conditions=cursor_condition, order_field=self.cursor_field
368+
)
369+
return query
370+
356371

357372
class AdGroupLabel(GoogleAdsStream):
358373
"""

airbyte-integrations/connectors/source-google-ads/unit_tests/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ def mock_oauth_call(requests_mock):
5656
@pytest.fixture
5757
def customers(config):
5858
return [CustomerModel(id=_id, time_zone="local", is_manager_account=False) for _id in config["customer_id"].split(",")]
59+
60+
61+
@pytest.fixture
62+
def customers_manager(config):
63+
return [CustomerModel(id=_id, time_zone="local", is_manager_account=True) for _id in config["customer_id"].split(",")]

airbyte-integrations/connectors/source-google-ads/unit_tests/test_streams.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from google.api_core.exceptions import DataLoss, InternalServerError, ResourceExhausted, TooManyRequests, Unauthenticated
1515
from grpc import RpcError
1616
from source_google_ads.google_ads import GoogleAds
17-
from source_google_ads.streams import ClickView, Customer, CustomerLabel
17+
from source_google_ads.streams import AdGroup, ClickView, Customer, CustomerLabel
1818

1919
# EXPIRED_PAGE_TOKEN exception will be raised when page token has expired.
2020
exception = GoogleAdsException(
@@ -287,3 +287,14 @@ def test_read_records_unauthenticated(mocker, customers, config):
287287
assert exc_info.value.message == (
288288
"Authentication failed for the customer 'customer_id'. " "Please try to Re-authenticate your credentials on set up Google Ads page."
289289
)
290+
291+
292+
def test_ad_group_stream_query_removes_metrics_field_for_manager(customers_manager, customers, config):
293+
credentials = config["credentials"]
294+
api = GoogleAds(credentials=credentials)
295+
stream_config = dict(api=api, customers=customers_manager, start_date="2020-01-01", conversion_window_days=10)
296+
stream = AdGroup(**stream_config)
297+
assert "metrics" not in stream.get_query(stream_slice={"customer_id": "123"})
298+
stream_config = dict(api=api, customers=customers, start_date="2020-01-01", conversion_window_days=10)
299+
stream = AdGroup(**stream_config)
300+
assert "metrics" in stream.get_query(stream_slice={"customer_id": "123"})

0 commit comments

Comments
 (0)