Stay organized with collectionsSave and categorize content based on your preferences.
Using theKeywordPlanIdeaService, you
can search for new keywords that are relevant to your Google Search campaign,
or find historical metrics on keywords.
You can provide keyword and URL seeds to generate ideas. Set targeting
parameters such as locations, language, and network settings to narrow down
your search. Historical statistics such as search volume data are returned to
help you determine if you want to use the keywords for your campaign. For users
familiar with the Google Ads UI, this is similar toKeyword Planner.
There are several ways you can create seeds for generating new keywords:
For words or phrases that describe what you're advertising,
useKeywordSeed. This could be a general type
of business you are targeting such as plumbers, or it could be a product or
service you offer such as drain cleaning.
For the URL of a webpage or entire website related to your business, useUrlSeed. The URL seed targets only a specific
URL. If there are no hits, the search automatically expands up to the pages
from the same domain.
For URL seeds, the contents of hyperlinks arenotused to generate keyword
ideas.
UsingKeywordAndUrlSeedcan result in a larger volume of keyword ideas, as
compared to justUrlSeed.
For an entire site, useSiteSeed. Given a
top-level domain name, such aswww.example.com, the site seed
generates up to 250,000 keyword ideas from publicly available information.
The result set in the response supportspagination.
Ideas are returned with historical metrics which can be used to filter a list
down to use with forecasts. For example, you might only want to target high
search volumes to maximize the reach of your campaign, or you might want to
target only higher competition scores to boost your awareness.
defmain(client:GoogleAdsClient,customer_id:str,location_ids:list[str],language_id:str,keyword_texts:list[str],page_url:str,):keyword_plan_idea_service:KeywordPlanIdeaServiceClient=(client.get_service("KeywordPlanIdeaService"))keyword_competition_level_enum:KeywordPlanCompetitionLevelEnum=(client.enums.KeywordPlanCompetitionLevelEnum)keyword_plan_network:KeywordPlanNetworkEnum=(client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH_AND_PARTNERS)location_rns:list[str]=map_locations_ids_to_resource_names(client,location_ids)google_ads_service:GoogleAdsServiceClient=client.get_service("GoogleAdsService")language_rn:str=google_ads_service.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.ifnot(keyword_textsorpage_url):raiseValueError("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:GenerateKeywordIdeasRequest=client.get_type("GenerateKeywordIdeasRequest")request.customer_id=customer_idrequest.language=language_rnrequest.geo_target_constants=location_rnsrequest.include_adult_keywords=Falserequest.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.ifnotkeyword_textsandpage_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.ifkeyword_textsandnotpage_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.ifkeyword_textsandpage_url:request.keyword_and_url_seed.url=page_urlrequest.keyword_and_url_seed.keywords.extend(keyword_texts)keyword_ideas=keyword_plan_idea_service.generate_keyword_ideas(request=request)idea:GenerateKeywordIdeaResultforideainkeyword_ideas:competition_value=idea.keyword_idea_metrics.competition.nameprint(f'Keyword idea text "{idea.text}" has 'f'"{idea.keyword_idea_metrics.avg_monthly_searches}" 'f'average monthly searches and "{competition_value}" '"competition.\n")
defgenerate_keyword_ideas(customer_id,location_ids,language_id,keywords,page_url)# GoogleAdsClient will read a config file from# ENV['HOME']/google_ads_config.rb when called without parametersclient=Google::Ads::GoogleAds::GoogleAdsClient.new# Make sure that keywords and/or page URL were specified. The request must# have exactly one of urlSeed, keywordSeed, or keywordAndUrlSeed set.ifkeywords.reject{|k|k.nil?}.empty?&&page_url.nil?raise"At least one of keywords or page URL is required."endkp_idea_service=client.service.keyword_plan_ideaoptions_hash=ifkeywords.empty?seed=client.resource.url_seeddo|seed|seed.url=page_urlend{url_seed:seed}elsifpage_url.nil?seed=client.resource.keyword_seeddo|seed|keywords.eachdo|keyword|seed.keywords<<keywordendend{keyword_seed:seed}elseseed=client.resource.keyword_and_url_seeddo|seed|seed.url=page_urlkeywords.eachdo|keyword|seed.keywords<<keywordendend{keyword_and_url_seed:seed}endgeo_target_constants=location_ids.mapdo|location_id|client.path.geo_target_constant(location_id)endinclude_adult_keywords=trueresponse=kp_idea_service.generate_keyword_ideas(customer_id:customer_id,language:client.path.language_constant(language_id),geo_target_constants:geo_target_constants,include_adult_keywords:include_adult_keywords,# To restrict to only Google Search, change the parameter below to# :GOOGLE_SEARCHkeyword_plan_network::GOOGLE_SEARCH_AND_PARTNERS,**options_hash)response.eachdo|result|monthly_searches=ifresult.keyword_idea_metrics.nil?0elseresult.keyword_idea_metrics.avg_monthly_searchesendcompetition=ifresult.keyword_idea_metrics.nil?:UNSPECIFIEDelseresult.keyword_idea_metrics.competitionendputs"Keyword idea text#{result.text}has#{monthly_searches}average "+"monthly searches and competition as#{competition}."endend
subgenerate_keyword_ideas{my($api_client,$customer_id,$location_ids,$language_id,$keyword_texts,$page_url)=@_;# Make sure that keywords and/or page URL were specified. The request must have# exactly one of urlSeed, keywordSeed, or keywordAndUrlSeed set.if(notscalar@$keyword_textsandnot$page_url){die"At least one of keywords or page URL is required, "."but neither was specified.";}# Specify the optional arguments of the request as a keywordSeed, urlSeed,# or keywordAndUrlSeed.my$request_option_args={};if(!scalar@$keyword_texts){# Only page URL was specified, so use a UrlSeed.$request_option_args->{urlSeed}=Google::Ads::GoogleAds::V21::Services::KeywordPlanIdeaService::UrlSeed->new({url=>$page_url});}elsif(not$page_url){# Only keywords were specified, so use a KeywordSeed.$request_option_args->{keywordSeed}=Google::Ads::GoogleAds::V21::Services::KeywordPlanIdeaService::KeywordSeed->new({keywords=>$keyword_texts});}else{# Both page URL and keywords were specified, so use a KeywordAndUrlSeed.$request_option_args->{keywordAndUrlSeed}=Google::Ads::GoogleAds::V21::Services::KeywordPlanIdeaService::KeywordAndUrlSeed->new({url=>$page_url,keywords=>$keyword_texts});}# Create a list of geo target constants based on the resource name of specified# location IDs.my$geo_target_constants=[map(Google::Ads::GoogleAds::V21::Utils::ResourceNames::geo_target_constant($_),@$location_ids)];# Generate keyword ideas based on the specified parameters.my$keyword_ideas_response=$api_client->KeywordPlanIdeaService()->generate_keyword_ideas({customerId=>$customer_id,# Set the language resource using the provided language ID.language=>Google::Ads::GoogleAds::V21::Utils::ResourceNames::language_constant($language_id),# Add the resource name of each location ID to the request.geoTargetConstants=>$geo_target_constants,# Set the network. To restrict to only Google Search, change the parameter below# to GOOGLE_SEARCH.keywordPlanNetwork=>GOOGLE_SEARCH_AND_PARTNERS,%$request_option_args});# Iterate over the results and print its detail.foreachmy$result(@{$keyword_ideas_response->{results}}){printf"Keyword idea text '%s' has %d average monthly searches "."and '%s' competition.\n",$result->{text},$result->{keywordIdeaMetrics}{avgMonthlySearches}?$result->{keywordIdeaMetrics}{avgMonthlySearches}:0,$result->{keywordIdeaMetrics}{competition}?$result->{keywordIdeaMetrics}{competition}:"undef";}return1;}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eThe Google Ads API's \u003ccode\u003eKeywordPlanIdeaService\u003c/code\u003e enables the generation of keyword ideas for search campaigns, providing historical metrics and targeting options for informed keyword selection.\u003c/p\u003e\n"],["\u003cp\u003eYou can provide seed keywords, URLs, or websites to generate ideas, refining your search by language, location, and network.\u003c/p\u003e\n"],["\u003cp\u003eResults include keyword suggestions alongside historical data such as average monthly searches and competition level to assist in building an effective keyword plan.\u003c/p\u003e\n"],["\u003cp\u003eCode examples in various languages demonstrate how to use the service, covering request creation, execution, and processing of the keyword idea results.\u003c/p\u003e\n"],["\u003cp\u003eA UI mapping is provided to illustrate the connection between Keyword Planner UI elements and their corresponding API request fields for better comprehension.\u003c/p\u003e\n"]]],[],null,[]]