/adx_buyer_api/examples/v201109/get_all_ad_group_criteria.rb

https://code.google.com/ · Ruby · 98 lines · 57 code · 10 blank · 31 comment · 9 complexity · d65c7d92cd9ce1b1ab76a8eef93cab88 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 criteria for an ad group.
  21. # To add criteria to an existing ad group, run add_ad_group_criteria.rb.
  22. #
  23. # Tags: AdGroupCriterionService.get
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. def get_all_ad_group_criteria()
  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_criterion_srv =
  36. adwords.service(:AdGroupCriterionService, API_VERSION)
  37. ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
  38. # Get all the criteria for this ad group.
  39. selector = {
  40. :fields => ['Id'],
  41. :ordering => [{
  42. :field => 'AdGroupId',
  43. :sort_order => 'ASCENDING'
  44. }],
  45. :predicates => [{
  46. :field => 'AdGroupId',
  47. :operator => 'IN',
  48. :values => [ad_group_id]
  49. }]
  50. }
  51. response = ad_group_criterion_srv.get(selector)
  52. if response and response[:entries]
  53. ad_group_criteria = response[:entries]
  54. puts "Ad group ##{ad_group_id} has #{ad_group_criteria.length} criteria."
  55. ad_group_criteria.each do |ad_group_criterion|
  56. puts " Criterion id is #{ad_group_criterion[:criterion][:id]} and " +
  57. "type is #{ad_group_criterion[:criterion][:xsi_type]}."
  58. end
  59. else
  60. puts "No criteria found for ad group ##{ad_group_id}."
  61. end
  62. end
  63. if __FILE__ == $0
  64. begin
  65. get_all_ad_group_criteria()
  66. # Connection error. Likely transitory.
  67. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  68. puts 'Connection Error: %s' % e
  69. puts 'Source: %s' % e.backtrace.first
  70. # API Error.
  71. rescue AdwordsApi::Errors::ApiException => e
  72. puts 'API Exception caught.'
  73. puts 'Message: %s' % e.message
  74. puts 'Code: %d' % e.code if e.code
  75. puts 'Trigger: %s' % e.trigger if e.trigger
  76. puts 'Errors:'
  77. if e.errors
  78. e.errors.each_with_index do |error, index|
  79. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  80. error.each_pair do |field, value|
  81. if field != :xsi_type
  82. puts ' %s: %s' % [field, value]
  83. end
  84. end
  85. end
  86. end
  87. end
  88. end