What type should I be getting to dismiss a recommendation via the Google Ads API in Python?
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!
It looks like there is a bug in the example. You can access the DismissRecommendationOperation through the DismissRecommendation request https://developers.google.com/google-ads/api/reference/rpc/v10/DismissRecommendationRequest
request = client.get_type('DismissRecommendationRequest') operation = request.DismissRecommendationOperationWhen I tried this, I got this error AttributeError: DismissRecommendationOperation
The problem was that I was using proto_plus = False in my config file, which apparently does not work with this wording. This is the code that worked for me:
def dismiss_the_recommendation(client, recommendation): rec_service = client.get_service("RecommendationService") request = client.get_type("DismissRecommendationRequest") operation = request.operations.add() operation.resource_name = recommendation.resource_name response = rec_service.dismiss_recommendation( customer_id=customer_id, operations=[operation] ) return (f"Dismissed recommendation with resource name: {response.results[0].resource_name}.")As a side note, I tried using the suggested solution with proto_plus = True and it still did not work, though I did not try very hard to debug it since I had already gotten this working.
I got the code that worked for me from GitHub here