Skip to content

Instantly share code, notes, and snippets.

@JokerCatz
Created March 31, 2016 11:22
Show Gist options
  • Save JokerCatz/bba356f6b20615bb09040b53f4d7c57b to your computer and use it in GitHub Desktop.
Save JokerCatz/bba356f6b20615bb09040b53f4d7c57b to your computer and use it in GitHub Desktop.
adwords keyword planner demo code
require 'adwords_api'
def get_keyword_ideas(keyword_text)
adwords = AdwordsApi::Api.new
targeting_idea_srv = adwords.service(:TargetingIdeaService, API_VERSION)
selector = {
:idea_type => 'KEYWORD',
:request_type => 'IDEAS',
:requested_attribute_types =>
['KEYWORD_TEXT', 'SEARCH_VOLUME', 'CATEGORY_PRODUCTS_AND_SERVICES' , 'AVERAGE_CPC'],
:search_parameters => [
# { #互斥 : RelatedToUrlSearchParameter
# :xsi_type => 'RelatedToQuerySearchParameter',
# :queries => [keyword_text]
# },
{
:xsi_type => 'LanguageSearchParameter',
:languages => [{:id => 1018}]
},
{
:xsi_type => 'RelatedToUrlSearchParameter',
:urls => ['http://ezprice.com.tw/ezpd_compare/20/46617-%E3%80%90ASUS%E3%80%91%20VS278H%2027%E5%9E%8B%20HDMI%20%E6%B6%B2%E6%99%B6%E8%9E%A2%E5%B9%95.htm']
},
{
:xsi_type => 'LocationSearchParameter',
:locations => [{
:id => 2158
}]
},
{
:xsi_type => 'CategoryProductsAndServicesSearchParameter',
:category_id => 12143
}
],
:paging => {
:start_index => 0,
:number_results => PAGE_SIZE
}
}
offset = 0
results = []
begin
page = targeting_idea_srv.get(selector)
results += page[:entries] if page and page[:entries]
offset += PAGE_SIZE
selector[:paging][:start_index] = offset
end while offset < page[:total_num_entries]
results.each do |result|
data = result[:data]
keyword = data['KEYWORD_TEXT'][:value]
puts "Found keyword with text '%s'" % keyword
products_and_services = data['CATEGORY_PRODUCTS_AND_SERVICES'][:value]
if products_and_services
puts "\tWith Products and Services categories: [%s]" %
products_and_services.join(', ')
end
average_monthly_searches = data['SEARCH_VOLUME'][:value]
if average_monthly_searches
puts "\tand average monthly search volume: %d" % average_monthly_searches
end
puts data['AVERAGE_CPC']
end
puts "Total keywords related to '%s': %d." % [keyword_text, results.length]
end
if __FILE__ == $0
API_VERSION = :v201601
PAGE_SIZE = 10000
begin
keyword_text = ARGV.join(' ').strip
puts "Search keyword : #{keyword_text}"
get_keyword_ideas(keyword_text)
rescue AdsCommon::Errors::OAuth2VerificationRequired => e
puts "Authorization credentials are not valid. Edit adwords_api.yml for " +
"OAuth2 client ID and secret and run misc/setup_oauth2.rb example " +
"to retrieve and store OAuth2 tokens."
puts "See this wiki page for more details:\n\n " +
'https://github.com/googleads/google-api-ads-ruby/wiki/OAuth2'
rescue AdsCommon::Errors::HttpError => e
puts "HTTP Error: %s" % e
rescue AdwordsApi::Errors::ApiException => e
puts "Message: %s" % e.message
puts 'Errors:'
e.errors.each_with_index do |error, index|
puts "\tError [%d]:" % (index + 1)
error.each do |field, value|
puts "\t\t%s: %s" % [field, value]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment