/adx_buyer_api/examples/v201003/set_campaign_targets.rb

https://code.google.com/ · Ruby · 124 lines · 77 code · 13 blank · 34 comment · 7 complexity · 753e9904db52a48a3455709028aecd3d 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 adds geo, language and network targeting to an existing campaign.
  21. # To create a campaign, run add_campaign.rb.
  22. #
  23. # Tags: CampaignTargetService.mutate
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201003
  28. def set_campaign_targets()
  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. campaign_target_srv = adwords.service(:CampaignTargetService, API_VERSION)
  36. campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
  37. # Language targeting.
  38. language_operation = {
  39. :operator => 'SET',
  40. :operand => {
  41. :xsi_type => 'LanguageTargetList',
  42. :campaign_id => campaign_id,
  43. :targets => [{:language_code => 'fr'}, {:language_code => 'ja'}]
  44. }
  45. }
  46. # Geo targeting.
  47. geo_operation = {
  48. :operator => 'SET',
  49. :operand => {
  50. :xsi_type => 'GeoTargetList',
  51. :campaign_id => campaign_id,
  52. :targets => [
  53. {
  54. :xsi_type => 'CountryTarget',
  55. :excluded => false,
  56. :country_code => 'US'
  57. },
  58. {
  59. :xsi_type => 'CountryTarget',
  60. :excluded => false,
  61. :country_code => 'JP'
  62. }
  63. ]
  64. }
  65. }
  66. # Network targeting.
  67. network_operation = {
  68. :operator => 'SET',
  69. :operand => {
  70. :xsi_type => 'NetworkTargetList',
  71. :campaign_id => campaign_id,
  72. :targets => [
  73. {:network_coverage_type => 'CONTENT_NETWORK'}
  74. ]
  75. }
  76. }
  77. # Set campaign targeting.
  78. response = campaign_target_srv.mutate([language_operation, geo_operation,
  79. network_operation])
  80. targets = response[:value]
  81. targets.each do |target|
  82. puts "Campaign target of type #{target[:xsi_type]} for campaign id " +
  83. "#{target[:campaign_id]} was set."
  84. end
  85. end
  86. if __FILE__ == $0
  87. begin
  88. set_campaign_targets()
  89. # Connection error. Likely transitory.
  90. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  91. puts 'Connection Error: %s' % e
  92. puts 'Source: %s' % e.backtrace.first
  93. # API Error.
  94. rescue AdwordsApi::Errors::ApiException => e
  95. puts 'API Exception caught.'
  96. puts 'Message: %s' % e.message
  97. puts 'Code: %d' % e.code if e.code
  98. puts 'Trigger: %s' % e.trigger if e.trigger
  99. puts 'Errors:'
  100. if e.errors
  101. e.errors.each_with_index do |error, index|
  102. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  103. error.each_pair do |field, value|
  104. if field != :xsi_type
  105. puts ' %s: %s' % [field, value]
  106. end
  107. end
  108. end
  109. end
  110. end
  111. end