/adx_buyer_api/examples/v201003/add_negative_campaign_criterion.rb

https://code.google.com/ · Ruby · 95 lines · 49 code · 11 blank · 35 comment · 7 complexity · cfd406767d696a812651b60eba140489 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 create a negative campaign criterion for a
  21. # given campaign. To create a campaign, run add_campaign.rb.
  22. #
  23. # Tags: CampaignCriterionService.mutate
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201003
  28. def add_negative_campaign_criterion()
  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_criterion_srv =
  36. adwords.service(:CampaignCriterionService, API_VERSION)
  37. campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
  38. # Prepare for adding criterion.
  39. operation = {
  40. :operator => 'ADD',
  41. :operand => {
  42. # The 'xsi_type' field allows you to specify the xsi:type of the object
  43. # being created. It's only necessary when you must provide an explicit
  44. # type that the client library can't infer.
  45. :xsi_type => 'NegativeCampaignCriterion',
  46. :campaign_id => campaign_id,
  47. :criterion => {
  48. :xsi_type => 'Placement',
  49. :url => 'http://jupiter.google.com'
  50. }
  51. }
  52. }
  53. # Add criterion.
  54. response = campaign_criterion_srv.mutate([operation])
  55. campaign_criterion = response[:value].first
  56. puts 'Campaign criterion id %d was successfully added.' %
  57. campaign_criterion[:criterion][:id]
  58. end
  59. if __FILE__ == $0
  60. begin
  61. add_negative_campaign_criterion()
  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