I am trying to fetch campaigns' reports from Google Ads API. But, getting an error when trying to fetch data with MCC:
Code:
import sys,os
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
def get_data(client, customer_id):
ga_service = client.get_service("GoogleAdsService", version="v6")
query = """
SELECT
campaign.name,
campaign.status,
segments.device,
metrics.impressions,
metrics.clicks,
metrics.ctr,
metrics.average_cpc,
metrics.cost_micros
FROM campaign
WHERE segments.date DURING LAST_30_DAYS
"""
# Issues a search request using streaming.
response = ga_service.search_stream(customer_id, query=query)
try:
for batch in response:
for row in batch.results:
print(
row
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
if __name__ == "__main__":
# get client object with oauth2
credentials = {'developer_token': "xxxxxxxxxxxxxxx",
'refresh_token': "xxxxxxxxxxxxxxxx",
'client_id': "xxxxxxxxxxxxxxx",
'client_secret': "xxxxxxxxxxxxxxxx"
}
google_ads_client = GoogleAdsClient.load_from_dict(credentials)
get_data(google_ads_client, 'xxxxxxxxx')
Wehn Running the code with MCC client ID:
get_data(google_ads_client, 'MANAGER(MCC)_CLIENT_ID')
I'm Getting Error_1:
Request made: ClientCustomerId: xxxxxxxxx, Host: googleads.googleapis.com:443, Method: /google.ads.googleads.v6.services.GoogleAdsService/SearchStream, RequestId: xxxxxxxxxx, IsFault: True, FaultMessage: Metrics cannot be requested for a manager account. To retrieve metrics, issue separate requests against each client account under the manager account.
I assumed, the solution would be to set a different ClientCustomerId of the account itself, not MCC. So I did, and run the code again with the client id of the direct account, and, got another error:
Wehn Running the code with Account client ID:
get_data(google_ads_client, 'ACCOUNT_CLIENT_ID')
I'm Getting Error_2:
Request made: ClientCustomerId: xxxxxxx, Host: googleads.googleapis.com:443, Method: /google.ads.googleads.v6.services.GoogleAdsService/SearchStream, RequestId: xxxxxxxxxx, IsFault: True, FaultMessage: User doesn't have permission to access customer. Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header. See https://developers.google.com/google-ads/api/docs/concepts/call-structure#cid
That error essentially says to insert the manager's client id, which I already did and got error_1 (!).
What am I missing here?