google-adx-buyer-api 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. data/ChangeLog +7 -0
  2. data/README +43 -41
  3. data/Rakefile +3 -3
  4. data/examples/v201003/add_ad_group.rb +5 -5
  5. data/examples/v201003/add_ad_group_criteria.rb +5 -5
  6. data/examples/v201003/add_ads.rb +5 -5
  7. data/examples/v201003/add_campaign.rb +5 -5
  8. data/examples/v201003/add_negative_campaign_criterion.rb +5 -5
  9. data/examples/v201003/add_placement_performance_report_definition.rb +5 -5
  10. data/examples/v201003/check_campaigns.rb +5 -5
  11. data/examples/v201003/delete_ad.rb +5 -5
  12. data/examples/v201003/delete_ad_group.rb +5 -5
  13. data/examples/v201003/delete_ad_group_criterion.rb +5 -5
  14. data/examples/v201003/delete_campaign.rb +5 -5
  15. data/examples/v201003/download_report.rb +5 -0
  16. data/examples/v201003/get_all_active_ad_group_criteria.rb +5 -5
  17. data/examples/v201003/get_all_ad_group_criteria.rb +5 -5
  18. data/examples/v201003/get_all_ad_groups.rb +5 -5
  19. data/examples/v201003/get_all_ads.rb +5 -5
  20. data/examples/v201003/get_all_campaign_targets.rb +5 -5
  21. data/examples/v201003/get_all_campaigns.rb +5 -5
  22. data/examples/v201003/get_all_disapproved_ads.rb +5 -5
  23. data/examples/v201003/get_all_images.rb +5 -5
  24. data/examples/v201003/get_all_paused_campaigns.rb +5 -5
  25. data/examples/v201003/get_all_report_definitions.rb +5 -5
  26. data/examples/v201003/get_criterion_bid_landscape.rb +5 -5
  27. data/examples/v201003/get_geo_location_info.rb +5 -5
  28. data/examples/v201003/get_related_placements.rb +5 -5
  29. data/examples/v201003/get_report_fields.rb +5 -5
  30. data/examples/v201003/perform_bulk_mutate_job.rb +5 -5
  31. data/examples/v201003/set_campaign_targets.rb +5 -5
  32. data/examples/v201003/update_ad.rb +5 -5
  33. data/examples/v201003/update_ad_group.rb +5 -5
  34. data/examples/v201003/update_ad_group_criterion.rb +5 -5
  35. data/examples/v201003/update_campaign.rb +5 -5
  36. data/examples/v201003/upload_image.rb +5 -5
  37. data/examples/v201008/add_ad_group.rb +5 -5
  38. data/examples/v201008/add_ad_group_criteria.rb +5 -5
  39. data/examples/v201008/add_ads.rb +5 -5
  40. data/examples/v201008/add_campaign.rb +5 -5
  41. data/examples/v201008/add_negative_campaign_criterion.rb +5 -5
  42. data/examples/v201008/add_placement_performance_report_definition.rb +5 -5
  43. data/examples/v201008/add_user_list.rb +5 -5
  44. data/examples/v201008/check_campaigns.rb +5 -5
  45. data/examples/v201008/delete_ad.rb +5 -5
  46. data/examples/v201008/delete_ad_group.rb +5 -5
  47. data/examples/v201008/delete_ad_group_criterion.rb +5 -5
  48. data/examples/v201008/delete_bulk_mutate_job.rb +5 -5
  49. data/examples/v201008/delete_campaign.rb +5 -5
  50. data/examples/v201008/delete_user_list.rb +5 -5
  51. data/examples/v201008/download_report.rb +5 -0
  52. data/examples/v201008/get_all_account_changes.rb +5 -5
  53. data/examples/v201008/get_all_active_ad_group_criteria.rb +5 -5
  54. data/examples/v201008/get_all_ad_group_criteria.rb +5 -5
  55. data/examples/v201008/get_all_ad_groups.rb +5 -5
  56. data/examples/v201008/get_all_ads.rb +5 -5
  57. data/examples/v201008/get_all_campaign_targets.rb +5 -5
  58. data/examples/v201008/get_all_campaigns.rb +5 -5
  59. data/examples/v201008/get_all_disapproved_ads.rb +5 -5
  60. data/examples/v201008/get_all_images.rb +5 -5
  61. data/examples/v201008/get_all_paused_campaigns.rb +5 -5
  62. data/examples/v201008/get_all_report_definitions.rb +5 -5
  63. data/examples/v201008/get_all_user_lists.rb +5 -5
  64. data/examples/v201008/get_bulk_mutate_job.rb +5 -5
  65. data/examples/v201008/get_campaign_alerts.rb +5 -5
  66. data/examples/v201008/get_criterion_bid_landscape.rb +5 -5
  67. data/examples/v201008/get_geo_location_info.rb +5 -5
  68. data/examples/v201008/get_related_placements.rb +5 -5
  69. data/examples/v201008/get_report_fields.rb +5 -5
  70. data/examples/v201008/perform_bulk_mutate_job.rb +5 -5
  71. data/examples/v201008/set_campaign_targets.rb +5 -5
  72. data/examples/v201008/update_ad.rb +5 -5
  73. data/examples/v201008/update_ad_group.rb +5 -5
  74. data/examples/v201008/update_ad_group_criterion.rb +5 -5
  75. data/examples/v201008/update_campaign.rb +5 -5
  76. data/examples/v201008/update_user_list.rb +5 -5
  77. data/examples/v201008/upload_image.rb +5 -5
  78. data/examples/v201101/add_ad_group.rb +5 -5
  79. data/examples/v201101/add_ad_group_criteria.rb +5 -5
  80. data/examples/v201101/add_ads.rb +5 -5
  81. data/examples/v201101/add_campaign.rb +5 -5
  82. data/examples/v201101/add_conversion.rb +5 -5
  83. data/examples/v201101/add_negative_campaign_criterion.rb +5 -5
  84. data/examples/v201101/add_placement_performance_report_definition.rb +5 -5
  85. data/examples/v201101/add_rtb_campaign.rb +5 -5
  86. data/examples/v201101/add_user_list.rb +5 -5
  87. data/examples/v201101/check_campaigns.rb +5 -5
  88. data/examples/v201101/delete_ad.rb +5 -5
  89. data/examples/v201101/delete_ad_group.rb +5 -5
  90. data/examples/v201101/delete_ad_group_criterion.rb +5 -5
  91. data/examples/v201101/delete_bulk_mutate_job.rb +5 -5
  92. data/examples/v201101/delete_campaign.rb +5 -5
  93. data/examples/v201101/delete_user_list.rb +5 -5
  94. data/examples/v201101/download_report.rb +5 -0
  95. data/examples/v201101/get_ad_group_bid_landscape.rb +5 -5
  96. data/examples/v201101/get_all_account_changes.rb +5 -5
  97. data/examples/v201101/get_all_active_ad_group_criteria.rb +5 -5
  98. data/examples/v201101/get_all_ad_group_criteria.rb +5 -5
  99. data/examples/v201101/get_all_ad_groups.rb +5 -5
  100. data/examples/v201101/get_all_ads.rb +5 -5
  101. data/examples/v201101/get_all_campaign_targets.rb +5 -5
  102. data/examples/v201101/get_all_campaigns.rb +5 -5
  103. data/examples/v201101/get_all_conversions.rb +5 -5
  104. data/examples/v201101/get_all_disapproved_ads.rb +5 -5
  105. data/examples/v201101/get_all_images.rb +5 -5
  106. data/examples/v201101/get_all_paused_campaigns.rb +5 -5
  107. data/examples/v201101/get_all_report_definitions.rb +5 -5
  108. data/examples/v201101/get_all_user_lists.rb +5 -5
  109. data/examples/v201101/get_bulk_mutate_job.rb +5 -5
  110. data/examples/v201101/get_campaign_alerts.rb +5 -5
  111. data/examples/v201101/get_criterion_bid_landscape.rb +5 -5
  112. data/examples/v201101/get_geo_location_info.rb +5 -5
  113. data/examples/v201101/get_related_placements.rb +5 -5
  114. data/examples/v201101/get_report_fields.rb +5 -5
  115. data/examples/v201101/perform_bulk_mutate_job.rb +5 -5
  116. data/examples/v201101/set_campaign_targets.rb +5 -5
  117. data/examples/v201101/update_ad.rb +5 -5
  118. data/examples/v201101/update_ad_group.rb +5 -5
  119. data/examples/v201101/update_ad_group_criterion.rb +5 -5
  120. data/examples/v201101/update_campaign.rb +5 -5
  121. data/examples/v201101/update_conversion.rb +5 -5
  122. data/examples/v201101/update_user_list.rb +5 -5
  123. data/examples/v201101/upload_image.rb +5 -5
  124. data/examples/v201109/add_ad_group.rb +98 -0
  125. data/examples/v201109/add_ad_group_criteria.rb +100 -0
  126. data/examples/v201109/add_ads.rb +110 -0
  127. data/examples/v201109/add_campaign.rb +108 -0
  128. data/examples/v201109/add_campaign_targeting_criteria.rb +102 -0
  129. data/examples/v201109/add_conversion.rb +97 -0
  130. data/examples/{v201008/get_all_experiments.rb → v201109/add_negative_campaign_criterion.rb} +32 -30
  131. data/examples/v201109/add_rtb_campaign.rb +113 -0
  132. data/examples/v201109/add_user_list.rb +122 -0
  133. data/examples/v201109/check_campaigns.rb +132 -0
  134. data/examples/v201109/delete_ad.rb +94 -0
  135. data/examples/{v201101/delete_experiment.rb → v201109/delete_ad_group.rb} +20 -21
  136. data/examples/v201109/delete_ad_group_criterion.rb +95 -0
  137. data/examples/v201109/delete_bulk_mutate_job.rb +96 -0
  138. data/examples/{v201008/delete_experiment.rb → v201109/delete_campaign.rb} +21 -21
  139. data/examples/{v201008/promote_experiment.rb → v201109/delete_user_list.rb} +21 -22
  140. data/examples/v201109/download_adhoc_report.rb +79 -0
  141. data/examples/v201109/download_report.rb +76 -0
  142. data/examples/v201109/get_ad_group_bid_landscape.rb +103 -0
  143. data/examples/v201109/get_all_account_changes.rb +146 -0
  144. data/examples/v201109/get_all_active_ad_group_criteria.rb +106 -0
  145. data/examples/v201109/get_all_ad_group_criteria.rb +98 -0
  146. data/examples/{v201101/get_all_experiments.rb → v201109/get_all_ad_groups.rb} +21 -25
  147. data/examples/v201109/get_all_ads.rb +99 -0
  148. data/examples/v201109/get_all_campaign_targets.rb +83 -0
  149. data/examples/v201109/get_all_campaigns.rb +86 -0
  150. data/examples/v201109/get_all_carriers.rb +72 -0
  151. data/examples/v201109/get_all_conversions.rb +88 -0
  152. data/examples/v201109/get_all_disapproved_ads.rb +102 -0
  153. data/examples/v201109/get_all_images.rb +90 -0
  154. data/examples/v201109/get_all_languages.rb +73 -0
  155. data/examples/v201109/get_all_paused_campaigns.rb +92 -0
  156. data/examples/v201109/get_all_report_definitions.rb +84 -0
  157. data/examples/v201109/get_all_user_lists.rb +87 -0
  158. data/examples/v201109/get_bulk_mutate_job.rb +106 -0
  159. data/examples/v201109/get_campaign_alerts.rb +103 -0
  160. data/examples/v201109/get_client_customer_id.rb +87 -0
  161. data/examples/v201109/get_criterion_bid_landscape.rb +102 -0
  162. data/examples/v201109/get_geo_location_info.rb +108 -0
  163. data/examples/v201109/get_location_criteria.rb +103 -0
  164. data/examples/v201109/get_related_placements.rb +114 -0
  165. data/examples/v201109/get_report_fields.rb +81 -0
  166. data/examples/v201109/perform_bulk_mutate_job.rb +222 -0
  167. data/examples/v201109/perform_mutate_job.rb +151 -0
  168. data/examples/{v201101/promote_experiment.rb → v201109/update_ad.rb} +29 -22
  169. data/examples/v201109/update_ad_group.rb +90 -0
  170. data/examples/v201109/update_ad_group_criterion.rb +108 -0
  171. data/examples/v201109/update_campaign.rb +93 -0
  172. data/examples/v201109/update_conversion.rb +96 -0
  173. data/examples/v201109/update_user_list.rb +91 -0
  174. data/examples/v201109/upload_image.rb +97 -0
  175. metadata +164 -114
  176. data/examples/v201008/add_experiment.rb +0 -162
  177. data/examples/v201101/add_experiment.rb +0 -162
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
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 gets location criteria by name.
21
+ #
22
+ # Tags: LocationCriterionService.get
23
+
24
+ require 'rubygems'
25
+ require 'adwords_api'
26
+
27
+ API_VERSION = :v201109
28
+
29
+ def get_location_criteria()
30
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
31
+ # when called without parameters.
32
+ adwords = AdwordsApi::Api.new
33
+
34
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
35
+ # the configuration file or provide your own logger:
36
+ # adwords.logger = Logger.new('adwords_xml.log')
37
+
38
+ location_criterion_srv =
39
+ adwords.service(:LocationCriterionService, API_VERSION)
40
+
41
+ # List of locations to look up.
42
+ location_names = ['Paris', 'Quebec', 'Spain', 'Deutschland']
43
+ # Locale to retrieve names in.
44
+ locale = 'en'
45
+
46
+ # Get the criteria by names.
47
+ selector = {
48
+ :fields => ['Id', 'LocationName', 'CanonicalName', 'DisplayType',
49
+ 'ParentLocations', 'Reach'],
50
+ :predicates => [
51
+ # Location names must match exactly, only EQUALS and IN are supported.
52
+ {:field => 'LocationName',
53
+ :operator => 'IN',
54
+ :values => location_names},
55
+ # Set the locale of the returned location names.
56
+ {:field => 'Locale', :operator => 'EQUALS', :values => locale}
57
+ ]
58
+ }
59
+ criteria = location_criterion_srv.get(selector)
60
+
61
+ if criteria
62
+ criteria.each do |criterion|
63
+ # Extract all parent location names as one comma-separated string.
64
+ parent_location = if criterion[:location][:parent_locations] and
65
+ !criterion[:location][:parent_locations].empty?
66
+ locations_array = criterion[:location][:parent_locations].map do |loc|
67
+ loc[:location_name]
68
+ end
69
+ locations_array.join(', ')
70
+ else
71
+ 'N/A'
72
+ end
73
+ puts ("The search term '%s' returned the location '%s' of type '%s' " +
74
+ "with ID %d, parent locations '%s' and reach %d") %
75
+ [criterion[:search_term], criterion[:location][:location_name],
76
+ criterion[:location][:criterion_type], criterion[:location][:id],
77
+ parent_location, criterion[:reach]]
78
+ end
79
+ else
80
+ puts 'No criteria were returned.'
81
+ end
82
+ end
83
+
84
+ if __FILE__ == $0
85
+ begin
86
+ get_location_criteria()
87
+
88
+ # HTTP errors.
89
+ rescue AdsCommon::Errors::HttpError => e
90
+ puts "HTTP Error: %s" % e
91
+
92
+ # API errors.
93
+ rescue AdwordsApi::Errors::ApiException => e
94
+ puts "Message: %s" % e.message
95
+ puts 'Errors:'
96
+ e.errors.each_with_index do |error, index|
97
+ puts "\tError [%d]:" % (index + 1)
98
+ error.each do |field, value|
99
+ puts "\t\t%s: %s" % [field, value]
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,114 @@
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 retrieves URLs that have content keywords related to a given
21
+ # website.
22
+ #
23
+ # Tags: TargetingIdeaService.get
24
+
25
+ require 'rubygems'
26
+ gem 'google-adwords-api'
27
+ require 'adwords_api'
28
+
29
+ API_VERSION = :v201109
30
+
31
+ def get_related_placements()
32
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
33
+ # when called without parameters.
34
+ adwords = AdwordsApi::Api.new
35
+
36
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
37
+ # the configuration file or provide your own logger:
38
+ # adwords.logger = Logger.new('adwords_xml.log')
39
+
40
+ targeting_idea_srv = adwords.service(:TargetingIdeaService, API_VERSION)
41
+
42
+ results_per_page = 10
43
+ url = 'mars.google.com'
44
+
45
+ # Construct selector.
46
+ selector = {
47
+ :idea_type => 'PLACEMENT',
48
+ :request_type => 'IDEAS',
49
+ :requested_attribute_types => ['CRITERION'],
50
+ :search_parameters => [{
51
+ # The 'xsi_type' field allows you to specify the xsi:type of the object
52
+ # being created. It's only necessary when you must provide an explicit
53
+ # type that the client library can't infer.
54
+ :xsi_type => 'RelatedToUrlSearchParameter',
55
+ :urls => [url],
56
+ :include_sub_urls => false
57
+ }],
58
+ :paging => {
59
+ :start_index => 0,
60
+ :number_results => results_per_page
61
+ }
62
+ }
63
+
64
+ # Perform request.
65
+ results = []
66
+ index = 0
67
+ while true
68
+ page = targeting_idea_srv.get(selector)
69
+ if page and page[:entries]
70
+ results += page[:entries]
71
+ end
72
+ break if page[:total_num_entries] <= index
73
+ index += results_per_page
74
+ selector[:paging][:start_index] = index
75
+ end
76
+
77
+ # Display results.
78
+ results.each do |result|
79
+ data = AdwordsApi::Utils.map(result[:data])
80
+ placement = data['CRITERION'][:value]
81
+ puts " Related content keywords found at URL \"%s\"" % placement[:url]
82
+ end
83
+ puts "Total urls found with content keywords related to keywords at " +
84
+ " \"#{url}\": #{results.length}."
85
+ end
86
+
87
+ if __FILE__ == $0
88
+ begin
89
+ get_related_placements()
90
+
91
+ # Connection error. Likely transitory.
92
+ rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
93
+ puts 'Connection Error: %s' % e
94
+ puts 'Source: %s' % e.backtrace.first
95
+
96
+ # API Error.
97
+ rescue AdwordsApi::Errors::ApiException => e
98
+ puts 'API Exception caught.'
99
+ puts 'Message: %s' % e.message
100
+ puts 'Code: %d' % e.code if e.code
101
+ puts 'Trigger: %s' % e.trigger if e.trigger
102
+ puts 'Errors:'
103
+ if e.errors
104
+ e.errors.each_with_index do |error, index|
105
+ puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
106
+ error.each_pair do |field, value|
107
+ if field != :xsi_type
108
+ puts ' %s: %s' % [field, value]
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,81 @@
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 gets the list of possible report fields for a report type.
21
+ #
22
+ # Tags: ReportDefinitionService.getReportFields
23
+
24
+ require 'rubygems'
25
+ gem 'google-adwords-api'
26
+ require 'adwords_api'
27
+
28
+ API_VERSION = :v201109
29
+
30
+ def get_report_fields()
31
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
32
+ # when called without parameters.
33
+ adwords = AdwordsApi::Api.new
34
+
35
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
36
+ # the configuration file or provide your own logger:
37
+ # adwords.logger = Logger.new('adwords_xml.log')
38
+
39
+ report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)
40
+
41
+ report_type = 'INSERT_REPORT_TYPE_HERE'
42
+
43
+ # Get report fields.
44
+ fields = report_def_srv.get_report_fields(report_type)
45
+ if fields
46
+ puts 'Report type \'%s\' contains the following fields:' % report_type
47
+ fields.each do |field|
48
+ puts ' - %s (%s)' % [field[:field_name], field[:field_type]]
49
+ puts ' := [%s]' % field[:enum_values].join(', ') if field[:enum_values]
50
+ end
51
+ end
52
+ end
53
+
54
+ if __FILE__ == $0
55
+ begin
56
+ get_report_fields()
57
+
58
+ # Connection error. Likely transitory.
59
+ rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
60
+ puts 'Connection Error: %s' % e
61
+ puts 'Source: %s' % e.backtrace.first
62
+
63
+ # API Error.
64
+ rescue AdwordsApi::Errors::ApiException => e
65
+ puts 'API Exception caught.'
66
+ puts 'Message: %s' % e.message
67
+ puts 'Code: %d' % e.code if e.code
68
+ puts 'Trigger: %s' % e.trigger if e.trigger
69
+ puts 'Errors:'
70
+ if e.errors
71
+ e.errors.each_with_index do |error, index|
72
+ puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
73
+ error.each_pair do |field, value|
74
+ if field != :xsi_type
75
+ puts ' %s: %s' % [field, value]
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Author:: sgomes@google.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 code sample illustrates how to perform asynchronous requests using the
21
+ # BulkMutateJobService.
22
+ #
23
+ # Tags: BulkMutateJobService.mutate
24
+
25
+ require 'rubygems'
26
+ gem 'google-adwords-api'
27
+ require 'adwords_api'
28
+
29
+ API_VERSION = :v201109
30
+ ITEM_COUNT = 100
31
+
32
+ def perform_bulk_mutate_job()
33
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
34
+ # when called without parameters.
35
+ adwords = AdwordsApi::Api.new
36
+
37
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
38
+ # the configuration file or provide your own logger:
39
+ # adwords.logger = Logger.new('adwords_xml.log')
40
+
41
+ bulk_mutate_job_srv = adwords.service(:BulkMutateJobService, API_VERSION)
42
+
43
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
44
+ ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
45
+
46
+ # Prepare image data and encode it.
47
+ image_url = 'https://sandbox.google.com/sandboximages/image.jpg'
48
+ image_data = AdsCommon::Http.get(image_url, adwords.config)
49
+ base64_image_data = Base64.encode64(image_data)
50
+
51
+ # Create AdGroupAdOperations to add ads.
52
+ ads_operations = Array.new(ITEM_COUNT) do |index|
53
+ ads_operation = {
54
+ :xsi_type => 'AdGroupAddOperation',
55
+ :operator => 'ADD',
56
+ :operand => {
57
+ :ad_group_id => ad_group_id
58
+ :ad => {
59
+ :xsi_type => 'ImageAd',
60
+ :name => "My Image Ad %d" % index,
61
+ :url => "http://www.example.com/%d" % index,
62
+ :display_url => "www.example.com",
63
+ :image => {
64
+ :data => base64_image_data
65
+ }
66
+ }
67
+ }
68
+ }
69
+ end
70
+
71
+ # Create an operation stream for the ads.
72
+ ads_stream = {
73
+ :scoping_entity_id => {
74
+ :type => 'CAMPAIGN_ID',
75
+ :value => campaign_id
76
+ },
77
+ :operations => ads_operations
78
+ }
79
+
80
+ # Create a placement in this ad group for ads.
81
+ plc_operation = {
82
+ :xsi_type => 'AdGroupCriterionOperation',
83
+ :operator => 'ADD',
84
+ :operand => {
85
+ :xsi_type => 'BiddableAdGroupCriterion',
86
+ :ad_group_id => ad_group_id,
87
+ :criterion => {
88
+ :xsi_type => 'Placement',
89
+ :url => "http://mars.google.com"
90
+ }
91
+ }
92
+ }
93
+
94
+ # Create an operation stream for the placement.
95
+ plc_stream = {
96
+ :scoping_entity_id => {
97
+ :type => 'CAMPAIGN_ID',
98
+ :value => campaign_id
99
+ },
100
+ :operations => [plc_operation]
101
+ }
102
+
103
+ # Create a BulkMutateJobService operation with the first part of the job.
104
+ job_operation_1 = {
105
+ :xsi_type => 'JobOperation',
106
+ # We're using ADD here because we're creating a new job.
107
+ :operator => 'ADD',
108
+ :operand => {
109
+ :xsi_type => 'BulkMutateJob',
110
+ :num_request_parts => 2,
111
+ :request => {
112
+ # The part index is zero-based.
113
+ :part_index => 0,
114
+ :operation_streams => [ads_stream]
115
+ }
116
+ }
117
+ }
118
+
119
+ # Call mutate() to create a new job by uploading the first part.
120
+ response_1 = bulk_mutate_job_srv.mutate(job_operation_1)
121
+ job_id = response_1[:id]
122
+ puts 'Job id %d was successfully created.' % job_id
123
+
124
+ # Similarly, create the next part of the job.
125
+ job_operation_2 = {
126
+ :xsi_type => 'JobOperation',
127
+ # We're using SET now because we're adding a new part to an existing job.
128
+ # Using ADD again would create a new job altogether.
129
+ :operator => 'SET',
130
+ :operand => {
131
+ :xsi_type => 'BulkMutateJob',
132
+ :num_request_parts => 2,
133
+ # Use the ID from the job we added.
134
+ :id => job_id,
135
+ :request => {
136
+ # Second part, hence index 1.
137
+ :part_index => 1,
138
+ :operation_streams => [plc_stream]
139
+ }
140
+ }
141
+ }
142
+
143
+ # And call mutate() to upload the second part. Since this is the final part,
144
+ # the job is automatically cleared for execution.
145
+ response_2 = bulk_mutate_job_srv.mutate(job_operation_2)
146
+ puts 'Second part of job %d successfully submitted.' % job_id
147
+
148
+ # Wait for the job to complete.
149
+ selector = {
150
+ :job_ids => [job_id]
151
+ }
152
+ sleep_interval = 10
153
+ status = nil
154
+ get_response = nil
155
+ while status != 'COMPLETED' && status != 'FAILED'
156
+ unless status.nil?
157
+ puts "Current status is #{status}, waiting #{sleep_interval} seconds"
158
+ sleep(sleep_interval) unless status.nil?
159
+ end
160
+ get_response = bulk_mutate_job_srv.get(selector)
161
+ if get_response and get_response.size > 0
162
+ status = get_response.first[:status]
163
+ else
164
+ puts 'Error retrieving job status; aborting.'
165
+ exit
166
+ end
167
+ end
168
+
169
+ if status == 'COMPLETED'
170
+ puts 'Job completed!'
171
+ # Retrieve the results for the parts.
172
+ 0.upto(job_operation_1[:operand][:num_request_parts] - 1) do |part_index|
173
+ selector = {
174
+ :job_ids => [job_id],
175
+ # Retrieve results for the current part.
176
+ :result_part_index => part_index
177
+ }
178
+ results = bulk_mutate_job_srv.get(selector)
179
+ if results
180
+ results.each do |result|
181
+ puts 'Part %d/%d of job #%d has successfully completed' %
182
+ [part_index + 1, job_operation_1[:operand][:num_request_parts],
183
+ job_id]
184
+ end
185
+ else
186
+ puts 'Error retrieving job results; aborting.'
187
+ exit
188
+ end
189
+ end
190
+ else
191
+ puts 'Job failed: ' + get_response.first[:failure_reason]
192
+ end
193
+ end
194
+
195
+ if __FILE__ == $0
196
+ begin
197
+ perform_bulk_mutate_job()
198
+
199
+ # Connection error. Likely transitory.
200
+ rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
201
+ puts 'Connection Error: %s' % e
202
+ puts 'Source: %s' % e.backtrace.first
203
+
204
+ # API Error.
205
+ rescue AdwordsApi::Errors::ApiException => e
206
+ puts 'API Exception caught.'
207
+ puts 'Message: %s' % e.message
208
+ puts 'Code: %d' % e.code if e.code
209
+ puts 'Trigger: %s' % e.trigger if e.trigger
210
+ puts 'Errors:'
211
+ if e.errors
212
+ e.errors.each_with_index do |error, index|
213
+ puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
214
+ error.each_pair do |field, value|
215
+ if field != :xsi_type
216
+ puts ' %s: %s' % [field, value]
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end