Skip to content

Commit

Permalink
🐛 Source Google Ads: Remove metrics from ad group for manager account (
Browse files Browse the repository at this point in the history
  • Loading branch information
darynaishchenko authored and jatinyadav-cc committed Feb 26, 2024
1 parent c008fb5 commit f47267d
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 105 deletions.
Expand Up @@ -123,6 +123,7 @@ acceptance_tests:
tests:
- config_path: "secrets/config.json"
configured_catalog_path: "integration_tests/configured_catalog.json"
- config_path: "secrets/config_manager_account.json"
incremental:
tests:
- config_path: "secrets/incremental_config.json"
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -11,7 +11,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: 253487c0-2246-43ba-a21f-5116b20a2c50
dockerImageTag: 3.2.1
dockerImageTag: 3.3.0
dockerRepository: airbyte/source-google-ads
documentationUrl: https://docs.airbyte.com/integrations/sources/google-ads
githubIssueLabel: source-google-ads
Expand Down
Expand Up @@ -353,6 +353,21 @@ class AdGroup(IncrementalGoogleAdsStream):

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

def get_query(self, stream_slice: Mapping[str, Any] = None) -> str:
fields = GoogleAds.get_fields_from_schema(self.get_json_schema())
# validation that the customer is not a manager
# due to unsupported metrics.cost_micros field and removing it in case custom is a manager
if [customer for customer in self.customers if customer.id == stream_slice["customer_id"]][0].is_manager_account:
fields = [field for field in fields if field != "metrics.cost_micros"]
table_name = get_resource_name(self.name)
start_date, end_date = stream_slice.get("start_date"), stream_slice.get("end_date")
cursor_condition = [f"{self.cursor_field} >= '{start_date}' AND {self.cursor_field} <= '{end_date}'"]

query = GoogleAds.convert_schema_into_query(
fields=fields, table_name=table_name, conditions=cursor_condition, order_field=self.cursor_field
)
return query


class AdGroupLabel(GoogleAdsStream):
"""
Expand Down
Expand Up @@ -56,3 +56,8 @@ def mock_oauth_call(requests_mock):
@pytest.fixture
def customers(config):
return [CustomerModel(id=_id, time_zone="local", is_manager_account=False) for _id in config["customer_id"].split(",")]


@pytest.fixture
def customers_manager(config):
return [CustomerModel(id=_id, time_zone="local", is_manager_account=True) for _id in config["customer_id"].split(",")]
Expand Up @@ -14,7 +14,7 @@
from google.api_core.exceptions import DataLoss, InternalServerError, ResourceExhausted, TooManyRequests, Unauthenticated
from grpc import RpcError
from source_google_ads.google_ads import GoogleAds
from source_google_ads.streams import ClickView, Customer, CustomerLabel
from source_google_ads.streams import AdGroup, ClickView, Customer, CustomerLabel

# EXPIRED_PAGE_TOKEN exception will be raised when page token has expired.
exception = GoogleAdsException(
Expand Down Expand Up @@ -287,3 +287,14 @@ def test_read_records_unauthenticated(mocker, customers, config):
assert exc_info.value.message == (
"Authentication failed for the customer 'customer_id'. " "Please try to Re-authenticate your credentials on set up Google Ads page."
)


def test_ad_group_stream_query_removes_metrics_field_for_manager(customers_manager, customers, config):
credentials = config["credentials"]
api = GoogleAds(credentials=credentials)
stream_config = dict(api=api, customers=customers_manager, start_date="2020-01-01", conversion_window_days=10)
stream = AdGroup(**stream_config)
assert "metrics" not in stream.get_query(stream_slice={"customer_id": "123"})
stream_config = dict(api=api, customers=customers, start_date="2020-01-01", conversion_window_days=10)
stream = AdGroup(**stream_config)
assert "metrics" in stream.get_query(stream_slice={"customer_id": "123"})

0 comments on commit f47267d

Please sign in to comment.