Posts tagged with python

I'm trying to use Google Ads API using service account.
The following works:

from google.ads.googleads.client import GoogleAdsClient from google.oauth2.service_account import Credentials as ServiceAccountCreds oauth2_client = ServiceAccountCreds.from_service_account_file(     filename=key_file, subject=developer_account,     scopes=["https://www.googleapis.com/auth/adwords"] ) google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=oauth2_client,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_serice.search_stream(customer_id=self.client_customer_id, query =  query) 

I want to to do following with Identity Workload Federation(w/o key):

from google.auth import aws as google_auth_aws from google.ads.googleads.client import GoogleAdsClient credentials = google_auth_aws.Credentials.from_info(json_config_info)  # from workload identity federation credentials = credentials.with_scopes(["https://www.googleapis.com/auth/adwords"]) credentials = credentials.with_subject(self.developer_account) # Build the service object. self.google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=credentials,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_service.search_stream(customer_id =self.client_customer_id, query =  self.query) 

Which throws error: ERROR:'Credentials' object has no attribute 'with_subject'.

I need to use Python in Google Colab to dismiss recommendations from many managed Google Ads accounts using the Google Ads API. I have been using the docs and the GitHub examples to build my code and it has worked to an extent. I have been able to retrieve recommendations with their resource names, but I have not been able to dismiss the recommendations. It seems there is an error getting the DismissRecommendationOperation.

Here is the code (with sensitive info redacted) and error message:

client = GoogleAdsClient.load_from_storage("google-ads.yaml.txt", version="v10") customer_id = "XXXXXXXXXX" recommendation_id = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" rec_service = client.get_service("RecommendationService") operation = client.get_type("DismissRecommendationOperation") operation.resource_name = recommendation_service.recommendation_path(     customer_id, recommendation_id ) response = rec_service.dismiss_recommendation(     customer_id=customer_id, operations=[operation] ) print(     "Dismissed recommendation with resource name: "     f"'{response.results[0].resource_name}'." ) 
--------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/google/ads/googleads/client.py in get_type(self, name, version)     440             type_classes = self._get_api_services_by_version(version) --> 441             message_class = getattr(type_classes, name)     442         except AttributeError: 2 frames AttributeError: unknown type 'DismissRecommendationOperation'. During handling of the above exception, another exception occurred: ValueError                                Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/google/ads/googleads/client.py in get_type(self, name, version)     442         except AttributeError:     443             raise ValueError( --> 444                 f"Specified type '{name}' does not exist in "     445                 f"Google Ads API {version}"     446             ) ValueError: Specified type 'DismissRecommendationOperation' does not exist in Google Ads API v10 

I have been using the docs here: https://developers.google.com/google-ads/api/docs/recommendations and the GitHub example here: https://github.com/googleads/google-ads-python/blob/23b6342914b49fa50e1c82b67f6508d2ac721787/examples/recommendations/dismiss_recommendation.py

Thank you so much for your help!

I have integrated google ads api in our companies CRM. It is only one api call using "KeywordPlanIdeaService". It works well in test-mode.

Next step is to "publish". But if i run the script to get the refresh_token and follow the URL, i get the "Authentification error" with deleloper hint "redirect_uri: urn:ietf:wg:oauth:2.0:oob"

What could be wrong?

I am trying to retrieve a report from google ads api of a metric over time in hourly increments. Is this possible?

You can select a date range and set the dimensions to hourly, for example:

SELECT   campaign.name,   metrics.impressions, FROM product_group_view WHERE segments.date DURING LAST_30_DAYS   AND metrics.impressions > 0 

Will get the impressions for 30days, but the data is aggregated across the 30days. Can I retrieve it as a time series?

In facebook, they have me?fields=id,name,insights.fields(impressions).breakdowns(aggregate_by_hourly_audience_timezones).time_increment(1)

This breaks the impressions data down into hourly increments for each day in the date range (not aggregated - 24 values regardless of date range).

I've searched throught the documentation for a few days now and can't seem to find what I'm looking for.

Edit: So searching through the last 3 days would produce something like the following output (structure of the data not as relevant):

// based on Dorian's example SELECT   campaign.name,   metrics.clicks,   segments.date,   segments.hour FROM campaign WHERE segments.date DURING LAST_3_DAYS // OUTPUT {"data": [      "2021-03-08": {0: {clicks: 20}, 1: {clicks: 43}, ...} (out to 23 - 1/ hour) ),      "2021-03-09": {0: {clicks: 18}, 1: {clicks: 33}, ...}       "2021-03-10": {0: {clicks: 18}, 1: {clicks: 33}, ...}  ]}

I am looking for collect data from Google ADS API into GCP by using Python scripts and it requires to fill these items for authentication in the google-ads.yaml file:

developer_token: client_id: client_secret: refresh_token: login_customer_id: 

I was able to fill these items by asking people in my company or generating it with google python scripts in GitHub but I need to understand the role of each, the docs seems to be disperse with a long learning path.