I have multiple keywords for which I am looking for some related keyword ideas along with their metrics. For that, I am using google-ads-api's GenerateKeywordIdeasRequest. It takes a list of keywords, but right now I am passing a single keyword at a time. After that I convert the output into a dataframe and pick the top 3 keywords ideas based on their monthly search volume.

for example: if passed "bottle", it would return

and from there I would extract the top 3 keyword searches suggested for "bottle".

But with this approach, it takes a lot of time to generate ideas for all input keywords. So I tried passing all input keywords at once, which reduce the processing time dramatically but the issue is, I am not able to get the top 3 suggested keyword for each input keyword separately as all suggested keywords ideas for each input are now mixed. And It is hard to tell which is suggested for which passed input.

Here is the code I am using

import pandas as pd from google.ads.googleads.client import GoogleAdsClient class GoogleAD:     def __init__(self):       pass          def get_keywords_search_volume(client, customer_id, location_ids, language_id, keyword_texts, page_url= None):         """function to get keyword search volume from google ads API """         keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService")         keyword_competition_level_enum = (client.enums.KeywordPlanCompetitionLevelEnum)         keyword_plan_network = (client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH_AND_PARTNERS)         location_rns = GoogleAD.map_locations_ids_to_resource_names(client, location_ids)         language_rn = client.get_service("GoogleAdsService").language_constant_path(language_id)                  # Either keywords or a page_url are required to generate keyword ideas         # so this raises an error if neither are provided.         if not (keyword_texts or page_url):             raise ValueError(                 "At least one of keywords or page URL is required, "                 "but neither was specified."             )                  # Only one of the fields "url_seed", "keyword_seed", or         # "keyword_and_url_seed" can be set on the request, depending on whether         # keywords, a page_url or both were passed to this function.         request = client.get_type("GenerateKeywordIdeasRequest")         request.customer_id = customer_id         request.language = language_rn         request.geo_target_constants = location_rns         request.include_adult_keywords = False         request.keyword_plan_network = keyword_plan_network                  # To generate keyword ideas with only a page_url and no keywords we need         # to initialize a UrlSeed object with the page_url as the "url" field.         if not keyword_texts and page_url:             request.url_seed.url = page_url                  # To generate keyword ideas with only a list of keywords and no page_url         # we need to initialize a KeywordSeed object and set the "keywords" field         # to be a list of StringValue objects.         if keyword_texts and not page_url:             request.keyword_seed.keywords.extend(keyword_texts)                  # To generate keyword ideas using both a list of keywords and a page_url we         # need to initialize a KeywordAndUrlSeed object, setting both the "url" and         # "keywords" fields.         if keyword_texts and page_url:             request.keyword_and_url_seed.url = page_url             request.keyword_and_url_seed.keywords.extend(keyword_texts)                  keyword_ideas = keyword_plan_idea_service.generate_keyword_ideas(request=request)                  for idea in keyword_ideas:             competition_value = idea.keyword_idea_metrics.competition.name         return keyword_ideas          def map_locations_ids_to_resource_names(client, location_ids):             """Converts a list of location IDs to resource names.             Args:                 client: an initialized GoogleAdsClient instance.                 location_ids: a list of location ID strings.             Returns:                 a list of resource name strings using the given location IDs.             """             build_resource_name = client.get_service(                 "GeoTargetConstantService"             ).geo_target_constant_path             return [build_resource_name(location_id) for location_id in location_ids]          def get_keyword_df(key_word):          """function to generate dataframe with keyword search volume"""         try:             client = GoogleAdsClient.load_from_storage('desc.yaml')             list_keywords = GoogleAD.get_keywords_search_volume(client, "686xxxxx647", ["2840"], "1000", key_word, None)             keyword_df = pd.DataFrame()             for idea in list_keywords:                 keyword_dict = {}                 competition_value = idea.keyword_idea_metrics.competition.name                 idea_text = idea.text                 monthly_search_volume = idea.keyword_idea_metrics.avg_monthly_searches                 keyword_dict['idea_text'] = idea_text                 keyword_dict['monthly_search_volume'] = int(monthly_search_volume)                 keyword_dict['competition_value'] = competition_value                 keyword_dict['key_word'] = key_word                 keyword_dict['key_word_modified'] = key_word.replace('%',' percent')                 keyword_series = pd.Series(keyword_dict)                 keyword_df = keyword_df.append(keyword_series, ignore_index = True)         except Exception as e:             print(e)             # print(e.args[2])             keyword_df = pd.DataFrame()         return keyword_df suggestion_df = GoogleAD.get_keyword_df(['bottle']) 

Let me know if there's any more information I can provide.

Tag:google-ads-api, python, api

Add a new comment.