find_location.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # Copyright 2018 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # https://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """This example illustrates getting GeoTargetConstants by given location names.
  16. """
  17. import sys
  18. from google.ads.googleads.client import GoogleAdsClient
  19. from google.ads.googleads.errors import GoogleAdsException
  20. # Locale is using ISO 639-1 format. If an invalid locale is given,
  21. # 'en' is used by default.
  22. LOCALE = "zh_TW"
  23. # A list of country codes can be referenced here:
  24. # https://developers.google.com/google-ads/api/reference/data/geotargets
  25. COUNTRY_CODE = "TW"
  26. # [START get_geo_target_constants_by_names]
  27. def main(client):
  28. gtc_service = client.get_service("GeoTargetConstantService")
  29. gtc_request = client.get_type("SuggestGeoTargetConstantsRequest")
  30. gtc_request.locale = LOCALE
  31. gtc_request.country_code = COUNTRY_CODE
  32. # The location names to get suggested geo target constants.
  33. gtc_request.location_names.names.extend(
  34. ["Taiwan"]
  35. )
  36. # gtc_request.location_names.names.extend(
  37. # ["Paris", "Quebec", "Spain", "Deutschland"]
  38. # )
  39. results = gtc_service.suggest_geo_target_constants(gtc_request)
  40. for suggestion in results.geo_target_constant_suggestions:
  41. geo_target_constant = suggestion.geo_target_constant
  42. print(
  43. f"{geo_target_constant.resource_name} "
  44. f"({geo_target_constant.name}, "
  45. f"{geo_target_constant.country_code}, "
  46. f"{geo_target_constant.target_type}, "
  47. f"{geo_target_constant.status.name}) "
  48. f"is found in locale ({suggestion.locale}) "
  49. f"with reach ({suggestion.reach}) "
  50. f"from search term ({suggestion.search_term})."
  51. )
  52. # [END get_geo_target_constants_by_names]
  53. if __name__ == "__main__":
  54. # GoogleAdsClient will read the google-ads.yaml configuration file in the
  55. # home directory if none is specified.
  56. # googleads_client = GoogleAdsClient.load_from_storage(version="v8")
  57. googleads_client = GoogleAdsClient.load_from_storage(version="v7")
  58. try:
  59. main(googleads_client)
  60. except GoogleAdsException as ex:
  61. print(
  62. f'Request with ID "{ex.request_id}" failed with status '
  63. f'"{ex.error.code().name}" and includes the following errors:'
  64. )
  65. for error in ex.failure.errors:
  66. print(f'\tError with message "{error.message}".')
  67. if error.location:
  68. for field_path_element in error.location.field_path_elements:
  69. print(f"\t\tOn field: {field_path_element.field_name}")
  70. sys.exit(1)