kw_planner.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import argparse
  2. import sys
  3. from google.ads.googleads.client import GoogleAdsClient
  4. from google.ads.googleads.errors import GoogleAdsException
  5. # Location IDs are listed here:
  6. # https://developers.google.com/google-ads/api/reference/data/geotargets
  7. # and they can also be retrieved using the GeoTargetConstantService as shown
  8. # here: https://developers.google.com/google-ads/api/docs/targeting/location-targeting
  9. _DEFAULT_LOCATION_IDS = ["2158"] # location ID for New York, NY
  10. # A language criterion ID. For example, specify 1000 for English. For more
  11. # information on determining this value, see the below link:
  12. # https://developers.google.com/google-ads/api/reference/data/codes-formats#expandable-7
  13. _DEFAULT_LANGUAGE_ID = "1018" # language ID for English
  14. ## 1018 chinese
  15. # [START generate_keyword_ideas]
  16. def main(
  17. client, customer_id, location_ids, language_id, keyword_texts, page_url
  18. ):
  19. keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService")
  20. keyword_competition_level_enum = client.get_type(
  21. "KeywordPlanCompetitionLevelEnum"
  22. ).KeywordPlanCompetitionLevel
  23. keyword_plan_network = client.get_type(
  24. "KeywordPlanNetworkEnum"
  25. ).KeywordPlanNetwork.GOOGLE_SEARCH_AND_PARTNERS
  26. location_rns = _map_locations_ids_to_resource_names(client, location_ids)
  27. language_rn = client.get_service(
  28. "LanguageConstantService"
  29. ).language_constant_path(language_id)
  30. # Either keywords or a page_url are required to generate keyword ideas
  31. # so this raises an error if neither are provided.
  32. if not (keyword_texts or page_url):
  33. raise ValueError(
  34. "At least one of keywords or page URL is required, "
  35. "but neither was specified."
  36. )
  37. # Only one of the fields "url_seed", "keyword_seed", or
  38. # "keyword_and_url_seed" can be set on the request, depending on whether
  39. # keywords, a page_url or both were passed to this function.
  40. request = client.get_type("GenerateKeywordIdeasRequest")
  41. request.customer_id = customer_id
  42. request.language = language_rn
  43. request.geo_target_constants = location_rns
  44. request.include_adult_keywords = False
  45. request.keyword_plan_network = keyword_plan_network
  46. # To generate keyword ideas with only a page_url and no keywords we need
  47. # to initialize a UrlSeed object with the page_url as the "url" field.
  48. if not keyword_texts and page_url:
  49. request.url_seed.url = url_seed
  50. # To generate keyword ideas with only a list of keywords and no page_url
  51. # we need to initialize a KeywordSeed object and set the "keywords" field
  52. # to be a list of StringValue objects.
  53. if keyword_texts and not page_url:
  54. request.keyword_seed.keywords.extend(keyword_texts)
  55. # To generate keyword ideas using both a list of keywords and a page_url we
  56. # need to initialize a KeywordAndUrlSeed object, setting both the "url" and
  57. # "keywords" fields.
  58. if keyword_texts and page_url:
  59. request.keyword_and_url_seed.url = page_url
  60. request.keyword_and_url_seed.keywords.extend(keyword_texts)
  61. keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(
  62. request=request
  63. )
  64. list_keywords = []
  65. for idea in keyword_ideas:
  66. competition_value = idea.keyword_idea_metrics.competition.name
  67. list_keywords.append(idea)
  68. return list_keywords
  69. def map_keywords_to_string_values(client, keyword_texts):
  70. keyword_protos = []
  71. for keyword in keyword_texts:
  72. string_val = client.get_type("StringValue")
  73. string_val.value = keyword
  74. keyword_protos.append(string_val)
  75. return keyword_protos
  76. def _map_locations_ids_to_resource_names(client, location_ids):
  77. """Converts a list of location IDs to resource names.
  78. Args:
  79. client: an initialized GoogleAdsClient instance.
  80. location_ids: a list of location ID strings.
  81. Returns:
  82. a list of resource name strings using the given location IDs.
  83. """
  84. build_resource_name = client.get_service(
  85. "GeoTargetConstantService"
  86. ).geo_target_constant_path
  87. return [build_resource_name(location_id) for location_id in location_ids]