/adx_buyer_api/examples/v201109/update_conversion.rb

https://code.google.com/ · Ruby · 96 lines · 50 code · 11 blank · 35 comment · 8 complexity · ae0ef1df369ca03608d93df1b50dc3f1 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 update a conversion by setting its status to
  21. # 'DISABLED'. To get conversions, run get_all_conversions.rb.
  22. #
  23. # Tags: ConversionTrackerService.mutate
  24. require 'rubygems'
  25. gem 'google-adwords-api'
  26. require 'adwords_api'
  27. API_VERSION = :v201109
  28. def update_conversion()
  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. conv_tracker_srv = adwords.service(:ConversionTrackerService, API_VERSION)
  36. conversion_id = 'INSERT_CONVERSION_ID_HERE'.to_i
  37. # Prepare for updating conversion.
  38. operation = {
  39. :operator => 'SET',
  40. :operand => {
  41. # The 'xsi_type' field allows you to specify the xsi:type of the object
  42. # being created. It's only necessary when you must provide an explicit
  43. # type that the client library can't infer.
  44. :xsi_type => 'AdWordsConversionTracker',
  45. :id => conversion_id,
  46. :status => 'DISABLED'
  47. }
  48. }
  49. # Update conversion.
  50. response = conv_tracker_srv.mutate([operation])
  51. if response and response[:value]
  52. conversion = response[:value].first
  53. puts "Conversion with id #{conversion[:id]}, name " +
  54. "\"#{conversion[:name]}\", status \"#{conversion[:status]}\" and " +
  55. " category \"#{conversion[:category]}\" was disabled."
  56. else
  57. puts 'No conversions were updated.'
  58. end
  59. end
  60. if __FILE__ == $0
  61. begin
  62. update_conversion()
  63. # Connection error. Likely transitory.
  64. rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
  65. puts 'Connection Error: %s' % e
  66. puts 'Source: %s' % e.backtrace.first
  67. # API Error.
  68. rescue AdwordsApi::Errors::ApiException => e
  69. puts 'API Exception caught.'
  70. puts 'Message: %s' % e.message
  71. puts 'Code: %d' % e.code if e.code
  72. puts 'Trigger: %s' % e.trigger if e.trigger
  73. puts 'Errors:'
  74. if e.errors
  75. e.errors.each_with_index do |error, index|
  76. puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
  77. error.each_pair do |field, value|
  78. if field != :xsi_type
  79. puts ' %s: %s' % [field, value]
  80. end
  81. end
  82. end
  83. end
  84. end
  85. end