/adx_buyer_api/examples/v201109/get_all_ad_groups.rb

https://code.google.com/ · Ruby · 94 lines · 53 code · 10 blank · 31 comment · 9 complexity · 80b1524dba8e1e372b950bc9eadae72a MD5 · raw file

  1. #!/usr/bin/ruby
  2. #
  3. # Author:: api.sgomes@gmail.com (S?Šrgio Gomes)
  4. #
  5. # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
  6. #
  7. # License:: Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  16. # implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. # This example illustrates how to retrieve all the ad groups for a campaign. To
  21. # create an ad group, run add_ad_group.rb.
  22. #
  23. # Tags: AdGroupService.get
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. def get_all_ad_groups()
  29. # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
  30. # when called without parameters.
  31. adwords = AdwordsApi::Api.new
  32. # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
  33. # the configuration file or provide your own logger:
  34. # adwords.logger = Logger.new('adwords_xml.log')
  35. ad_group_srv = adwords.service(:AdGroupService, API_VERSION)
  36. campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
  37. # Get all the ad groups for this campaign.
  38. selector = {
  39. :fields => ['Id', 'Name'],
  40. :ordering => [{:field => 'Name', :sort_order => 'ASCENDING'}],
  41. :predicates => [{
  42. :field => 'CampaignId',
  43. :operator => 'IN',
  44. :values => [campaign_id]
  45. }]
  46. }
  47. response = ad_group_srv.get(selector)
  48. if response and response[:entries]
  49. ad_groups = response[:entries]
  50. puts "Campaign ##{campaign_id} has #{ad_groups.length} ad group(s)."
  51. ad_groups.each do |ad_group|
  52. puts " Ad group name is \"#{ad_group[:name]}\" and id is " +
  53. "#{ad_group[:id]}."
  54. end
  55. else
  56. puts "No ad groups found for campaign ##{campaign_id}."
  57. end
  58. end
  59. if __FILE__ == $0
  60. begin
  61. get_all_ad_groups()
  62. # Connection error. Likely transitory.
  63. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  64. puts 'Connection Error: %s' % e
  65. puts 'Source: %s' % e.backtrace.first
  66. # API Error.
  67. rescue AdwordsApi::Errors::ApiException => e
  68. puts 'API Exception caught.'
  69. puts 'Message: %s' % e.message
  70. puts 'Code: %d' % e.code if e.code
  71. puts 'Trigger: %s' % e.trigger if e.trigger
  72. puts 'Errors:'
  73. if e.errors
  74. e.errors.each_with_index do |error, index|
  75. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  76. error.each_pair do |field, value|
  77. if field != :xsi_type
  78. puts ' %s: %s' % [field, value]
  79. end
  80. end
  81. end
  82. end
  83. end
  84. end