google-adwords-api 0.14.1 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. checksums.yaml +8 -8
  2. data/ChangeLog +3 -0
  3. data/examples/v201406/optimization/estimate_keyword_traffic.rb +30 -12
  4. data/examples/v201409/basic_operations/add_keywords.rb +3 -1
  5. data/examples/v201409/{advanced_operations → extensions}/add_google_my_business_location_extensions.rb +1 -1
  6. data/examples/v201409/extensions/add_site_links.rb +167 -0
  7. data/examples/v201409/{advanced_operations/add_site_links.rb → extensions/add_site_links_using_feeds.rb} +0 -0
  8. data/examples/v201409/migration/migrate_to_extension_settings.rb +365 -0
  9. data/examples/v201409/optimization/estimate_keyword_traffic.rb +30 -13
  10. data/examples/v201502/account_management/create_account.rb +92 -0
  11. data/examples/v201502/account_management/get_account_changes.rb +143 -0
  12. data/examples/v201502/account_management/get_account_hierarchy.rb +98 -0
  13. data/examples/v201502/advanced_operations/add_ad_customizer.rb +277 -0
  14. data/examples/v201502/advanced_operations/add_ad_group_bid_modifier.rb +105 -0
  15. data/examples/v201502/advanced_operations/add_click_to_download_ad.rb +137 -0
  16. data/examples/v201502/advanced_operations/add_text_ad_with_upgraded_urls.rb +137 -0
  17. data/examples/v201502/advanced_operations/create_and_attach_shared_keyword_set.rb +137 -0
  18. data/examples/v201502/advanced_operations/find_and_remove_criteria_from_shared_set.rb +171 -0
  19. data/examples/v201502/advanced_operations/get_ad_group_bid_modifiers.rb +106 -0
  20. data/examples/v201502/advanced_operations/upload_offline_conversions.rb +117 -0
  21. data/examples/v201502/advanced_operations/use_shared_bidding_strategy.rb +152 -0
  22. data/examples/v201502/basic_operations/add_ad_groups.rb +144 -0
  23. data/examples/v201502/basic_operations/add_campaigns.rb +143 -0
  24. data/examples/v201502/basic_operations/add_keywords.rb +118 -0
  25. data/examples/v201502/basic_operations/add_text_ads.rb +113 -0
  26. data/examples/v201502/basic_operations/get_ad_groups.rb +106 -0
  27. data/examples/v201502/basic_operations/get_campaigns.rb +101 -0
  28. data/examples/v201502/basic_operations/get_campaigns_with_awql.rb +93 -0
  29. data/examples/v201502/basic_operations/get_keywords.rb +112 -0
  30. data/examples/v201502/basic_operations/get_text_ads.rb +114 -0
  31. data/examples/v201502/basic_operations/pause_ad.rb +92 -0
  32. data/examples/v201502/basic_operations/remove_ad.rb +93 -0
  33. data/examples/v201502/basic_operations/remove_ad_group.rb +89 -0
  34. data/examples/v201502/basic_operations/remove_campaign.rb +91 -0
  35. data/examples/v201502/basic_operations/remove_keyword.rb +98 -0
  36. data/examples/v201502/basic_operations/update_ad_group.rb +89 -0
  37. data/examples/v201502/basic_operations/update_campaign.rb +90 -0
  38. data/examples/v201502/basic_operations/update_keyword.rb +110 -0
  39. data/examples/v201502/campaign_management/add_campaign_labels.rb +86 -0
  40. data/examples/v201502/campaign_management/add_experiment.rb +166 -0
  41. data/examples/v201502/campaign_management/add_keywords_in_bulk.rb +158 -0
  42. data/examples/v201502/campaign_management/add_location_extension.rb +125 -0
  43. data/examples/v201502/campaign_management/get_all_disapproved_ads.rb +101 -0
  44. data/examples/v201502/campaign_management/get_all_disapproved_ads_with_awql.rb +93 -0
  45. data/examples/v201502/campaign_management/get_campaigns_by_label.rb +112 -0
  46. data/examples/v201502/campaign_management/promote_experiment.rb +85 -0
  47. data/examples/v201502/campaign_management/set_ad_parameters.rb +122 -0
  48. data/examples/v201502/campaign_management/set_criterion_bid_modifier.rb +108 -0
  49. data/examples/v201502/campaign_management/validate_text_ad.rb +114 -0
  50. data/examples/v201502/error_handling/handle_captcha_challenge.rb +93 -0
  51. data/examples/v201502/error_handling/handle_partial_failures.rb +134 -0
  52. data/examples/v201502/error_handling/handle_policy_violation_error.rb +145 -0
  53. data/examples/v201502/error_handling/handle_two_factor_authorization_error.rb +88 -0
  54. data/examples/v201502/extensions/add_google_my_business_location_extensions.rb +183 -0
  55. data/examples/v201502/extensions/add_site_links.rb +167 -0
  56. data/examples/v201502/extensions/add_site_links_using_feeds.rb +306 -0
  57. data/examples/v201502/migration/migrate_to_extension_settings.rb +365 -0
  58. data/examples/v201502/migration/upgrade_ad_url.rb +97 -0
  59. data/examples/v201502/misc/create_ad_words_session_without_properties_file.rb +94 -0
  60. data/examples/v201502/misc/get_all_images_and_videos.rb +108 -0
  61. data/examples/v201502/misc/setup_oauth2.rb +88 -0
  62. data/examples/v201502/misc/upload_image.rb +97 -0
  63. data/examples/v201502/misc/use_oauth2_jwt.rb +97 -0
  64. data/examples/v201502/optimization/estimate_keyword_traffic.rb +155 -0
  65. data/examples/v201502/optimization/get_keyword_bid_simulations.rb +99 -0
  66. data/examples/v201502/optimization/get_keyword_ideas.rb +130 -0
  67. data/examples/v201502/remarketing/add_audience.rb +122 -0
  68. data/examples/v201502/remarketing/add_conversion_tracker.rb +105 -0
  69. data/examples/v201502/remarketing/add_rule_based_user_lists.rb +171 -0
  70. data/examples/v201502/reporting/download_criteria_report.rb +87 -0
  71. data/examples/v201502/reporting/download_criteria_report_with_awql.rb +86 -0
  72. data/examples/v201502/reporting/get_report_fields.rb +79 -0
  73. data/examples/v201502/reporting/parallel_report_download.rb +168 -0
  74. data/examples/v201502/shopping_campaigns/add_product_partition_tree.rb +269 -0
  75. data/examples/v201502/shopping_campaigns/add_product_scope.rb +133 -0
  76. data/examples/v201502/shopping_campaigns/add_shopping_campaign.rb +133 -0
  77. data/examples/v201502/shopping_campaigns/get_product_category_taxonomy.rb +117 -0
  78. data/examples/v201502/targeting/add_campaign_targeting_criteria.rb +173 -0
  79. data/examples/v201502/targeting/add_demographic_targeting_criteria.rb +116 -0
  80. data/examples/v201502/targeting/get_campaign_targeting_criteria.rb +110 -0
  81. data/examples/v201502/targeting/get_targetable_languages_and_carriers.rb +94 -0
  82. data/examples/v201502/targeting/lookup_location.rb +112 -0
  83. data/lib/adwords_api/api_config.rb +98 -4
  84. data/lib/adwords_api/v201409/ad_customizer_feed_service.rb +38 -0
  85. data/lib/adwords_api/v201409/ad_customizer_feed_service_registry.rb +46 -0
  86. data/lib/adwords_api/v201409/ad_group_extension_setting_service.rb +42 -0
  87. data/lib/adwords_api/v201409/ad_group_extension_setting_service_registry.rb +46 -0
  88. data/lib/adwords_api/v201409/campaign_extension_setting_service.rb +42 -0
  89. data/lib/adwords_api/v201409/campaign_extension_setting_service_registry.rb +46 -0
  90. data/lib/adwords_api/v201409/customer_extension_setting_service.rb +42 -0
  91. data/lib/adwords_api/v201409/customer_extension_setting_service_registry.rb +46 -0
  92. data/lib/adwords_api/v201502/account_label_service.rb +38 -0
  93. data/lib/adwords_api/v201502/account_label_service_registry.rb +46 -0
  94. data/lib/adwords_api/v201502/ad_customizer_feed_service.rb +38 -0
  95. data/lib/adwords_api/v201502/ad_customizer_feed_service_registry.rb +46 -0
  96. data/lib/adwords_api/v201502/ad_group_ad_service.rb +50 -0
  97. data/lib/adwords_api/v201502/ad_group_ad_service_registry.rb +46 -0
  98. data/lib/adwords_api/v201502/ad_group_bid_modifier_service.rb +42 -0
  99. data/lib/adwords_api/v201502/ad_group_bid_modifier_service_registry.rb +46 -0
  100. data/lib/adwords_api/v201502/ad_group_criterion_service.rb +46 -0
  101. data/lib/adwords_api/v201502/ad_group_criterion_service_registry.rb +46 -0
  102. data/lib/adwords_api/v201502/ad_group_extension_setting_service.rb +42 -0
  103. data/lib/adwords_api/v201502/ad_group_extension_setting_service_registry.rb +46 -0
  104. data/lib/adwords_api/v201502/ad_group_feed_service.rb +42 -0
  105. data/lib/adwords_api/v201502/ad_group_feed_service_registry.rb +46 -0
  106. data/lib/adwords_api/v201502/ad_group_service.rb +46 -0
  107. data/lib/adwords_api/v201502/ad_group_service_registry.rb +46 -0
  108. data/lib/adwords_api/v201502/ad_param_service.rb +38 -0
  109. data/lib/adwords_api/v201502/ad_param_service_registry.rb +46 -0
  110. data/lib/adwords_api/v201502/adwords_user_list_service.rb +38 -0
  111. data/lib/adwords_api/v201502/adwords_user_list_service_registry.rb +46 -0
  112. data/lib/adwords_api/v201502/bidding_strategy_service.rb +42 -0
  113. data/lib/adwords_api/v201502/bidding_strategy_service_registry.rb +46 -0
  114. data/lib/adwords_api/v201502/budget_order_service.rb +42 -0
  115. data/lib/adwords_api/v201502/budget_order_service_registry.rb +46 -0
  116. data/lib/adwords_api/v201502/budget_service.rb +42 -0
  117. data/lib/adwords_api/v201502/budget_service_registry.rb +46 -0
  118. data/lib/adwords_api/v201502/campaign_criterion_service.rb +42 -0
  119. data/lib/adwords_api/v201502/campaign_criterion_service_registry.rb +46 -0
  120. data/lib/adwords_api/v201502/campaign_extension_setting_service.rb +42 -0
  121. data/lib/adwords_api/v201502/campaign_extension_setting_service_registry.rb +46 -0
  122. data/lib/adwords_api/v201502/campaign_feed_service.rb +42 -0
  123. data/lib/adwords_api/v201502/campaign_feed_service_registry.rb +46 -0
  124. data/lib/adwords_api/v201502/campaign_service.rb +46 -0
  125. data/lib/adwords_api/v201502/campaign_service_registry.rb +46 -0
  126. data/lib/adwords_api/v201502/campaign_shared_set_service.rb +38 -0
  127. data/lib/adwords_api/v201502/campaign_shared_set_service_registry.rb +46 -0
  128. data/lib/adwords_api/v201502/constant_data_service.rb +66 -0
  129. data/lib/adwords_api/v201502/constant_data_service_registry.rb +46 -0
  130. data/lib/adwords_api/v201502/conversion_tracker_service.rb +42 -0
  131. data/lib/adwords_api/v201502/conversion_tracker_service_registry.rb +46 -0
  132. data/lib/adwords_api/v201502/customer_extension_setting_service.rb +42 -0
  133. data/lib/adwords_api/v201502/customer_extension_setting_service_registry.rb +46 -0
  134. data/lib/adwords_api/v201502/customer_feed_service.rb +42 -0
  135. data/lib/adwords_api/v201502/customer_feed_service_registry.rb +46 -0
  136. data/lib/adwords_api/v201502/customer_service.rb +38 -0
  137. data/lib/adwords_api/v201502/customer_service_registry.rb +46 -0
  138. data/lib/adwords_api/v201502/customer_sync_service.rb +34 -0
  139. data/lib/adwords_api/v201502/customer_sync_service_registry.rb +47 -0
  140. data/lib/adwords_api/v201502/data_service.rb +54 -0
  141. data/lib/adwords_api/v201502/data_service_registry.rb +46 -0
  142. data/lib/adwords_api/v201502/experiment_service.rb +38 -0
  143. data/lib/adwords_api/v201502/experiment_service_registry.rb +46 -0
  144. data/lib/adwords_api/v201502/feed_item_service.rb +42 -0
  145. data/lib/adwords_api/v201502/feed_item_service_registry.rb +46 -0
  146. data/lib/adwords_api/v201502/feed_mapping_service.rb +42 -0
  147. data/lib/adwords_api/v201502/feed_mapping_service_registry.rb +46 -0
  148. data/lib/adwords_api/v201502/feed_service.rb +42 -0
  149. data/lib/adwords_api/v201502/feed_service_registry.rb +46 -0
  150. data/lib/adwords_api/v201502/geo_location_service.rb +34 -0
  151. data/lib/adwords_api/v201502/geo_location_service_registry.rb +46 -0
  152. data/lib/adwords_api/v201502/label_service.rb +42 -0
  153. data/lib/adwords_api/v201502/label_service_registry.rb +46 -0
  154. data/lib/adwords_api/v201502/location_criterion_service.rb +38 -0
  155. data/lib/adwords_api/v201502/location_criterion_service_registry.rb +46 -0
  156. data/lib/adwords_api/v201502/managed_customer_service.rb +54 -0
  157. data/lib/adwords_api/v201502/managed_customer_service_registry.rb +46 -0
  158. data/lib/adwords_api/v201502/media_service.rb +42 -0
  159. data/lib/adwords_api/v201502/media_service_registry.rb +46 -0
  160. data/lib/adwords_api/v201502/mutate_job_service.rb +42 -0
  161. data/lib/adwords_api/v201502/mutate_job_service_registry.rb +46 -0
  162. data/lib/adwords_api/v201502/offline_conversion_feed_service.rb +34 -0
  163. data/lib/adwords_api/v201502/offline_conversion_feed_service_registry.rb +46 -0
  164. data/lib/adwords_api/v201502/report_definition_service.rb +34 -0
  165. data/lib/adwords_api/v201502/report_definition_service_registry.rb +46 -0
  166. data/lib/adwords_api/v201502/shared_criterion_service.rb +38 -0
  167. data/lib/adwords_api/v201502/shared_criterion_service_registry.rb +46 -0
  168. data/lib/adwords_api/v201502/shared_set_service.rb +38 -0
  169. data/lib/adwords_api/v201502/shared_set_service_registry.rb +46 -0
  170. data/lib/adwords_api/v201502/targeting_idea_service.rb +34 -0
  171. data/lib/adwords_api/v201502/targeting_idea_service_registry.rb +46 -0
  172. data/lib/adwords_api/v201502/traffic_estimator_service.rb +34 -0
  173. data/lib/adwords_api/v201502/traffic_estimator_service_registry.rb +46 -0
  174. data/lib/adwords_api/version.rb +1 -1
  175. data/test/adwords_api/test_adwords_api.rb +1 -12
  176. data/test/templates/v201406/basic_operations_get_campaigns.def +1 -1
  177. data/test/templates/v201406/misc_use_oauth2_jwt.def +1 -1
  178. data/test/templates/v201409/basic_operations_get_campaigns.def +1 -1
  179. data/test/templates/v201409/misc_use_oauth2_jwt.def +1 -1
  180. data/test/templates/v201502/basic_operations_get_campaigns.def +114 -0
  181. data/test/templates/v201502/misc_use_oauth2_jwt.def +131 -0
  182. metadata +173 -8
  183. data/examples/v201406/advanced_operations/update_site_links.rb +0 -194
  184. data/examples/v201409/advanced_operations/update_site_links.rb +0 -194
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.sgomes@gmail.com (Sérgio Gomes)
5
+ #
6
+ # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
7
+ #
8
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
+ # implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ # This example gets the list of possible report fields for a report type.
22
+ #
23
+ # Tags: ReportDefinitionService.getReportFields
24
+
25
+ require 'adwords_api'
26
+
27
+ def get_report_fields(report_type)
28
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
29
+ # when called without parameters.
30
+ adwords = AdwordsApi::Api.new
31
+
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
+
36
+ report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)
37
+
38
+ # Get report fields.
39
+ fields = report_def_srv.get_report_fields(report_type)
40
+ if fields
41
+ puts "Report type '%s' contains the following fields:" % report_type
42
+ fields.each do |field|
43
+ puts ' - %s (%s)' % [field[:field_name], field[:field_type]]
44
+ puts ' := [%s]' % field[:enum_values].join(', ') if field[:enum_values]
45
+ end
46
+ end
47
+ end
48
+
49
+ if __FILE__ == $0
50
+ API_VERSION = :v201502
51
+
52
+ begin
53
+ report_type = 'INSERT_REPORT_TYPE_HERE'
54
+ get_report_fields(report_type)
55
+
56
+ # Authorization error.
57
+ rescue AdsCommon::Errors::OAuth2VerificationRequired => e
58
+ puts "Authorization credentials are not valid. Edit adwords_api.yml for " +
59
+ "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " +
60
+ "to retrieve and store OAuth2 tokens."
61
+ puts "See this wiki page for more details:\n\n " +
62
+ 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2'
63
+
64
+ # HTTP errors.
65
+ rescue AdsCommon::Errors::HttpError => e
66
+ puts "HTTP Error: %s" % e
67
+
68
+ # API errors.
69
+ rescue AdwordsApi::Errors::ApiException => e
70
+ puts "Message: %s" % e.message
71
+ puts 'Errors:'
72
+ e.errors.each_with_index do |error, index|
73
+ puts "\tError [%d]:" % (index + 1)
74
+ error.each do |field, value|
75
+ puts "\t\t%s: %s" % [field, value]
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
7
+ #
8
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
+ # implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ # This example gets and downloads an Ad Hoc report from a XML report definition
22
+ # for all accounts in hierarchy in multiple parallel threads. This example
23
+ # needs to be run against an MCC account.
24
+
25
+ require 'thread'
26
+
27
+ require 'adwords_api'
28
+ require 'adwords_api/utils'
29
+
30
+ def parallel_report_download()
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
+ # Determine list of customer IDs to retrieve report for. For this example we
40
+ # will use ManagedCustomerService to get all IDs in hierarchy.
41
+
42
+ managed_customer_srv = adwords.service(:ManagedCustomerService, API_VERSION)
43
+
44
+ # Get the account hierarchy for this account.
45
+ selector = {:fields => ['CustomerId']}
46
+
47
+ graph = managed_customer_srv.get(selector)
48
+
49
+ # Using queue to balance load between threads.
50
+ queue = Queue.new()
51
+
52
+ if graph and graph[:entries] and !graph[:entries].empty?
53
+ graph[:entries].each {|account| queue << account[:customer_id]}
54
+ else
55
+ raise StandardError, 'Can not retrieve any customer ID'
56
+ end
57
+
58
+ # Get report utilities for the version.
59
+ report_utils = adwords.report_utils(API_VERSION)
60
+
61
+ # Define report definition. You can also pass your own XML text as a string.
62
+ report_definition = {
63
+ :selector => {
64
+ :fields => ['CampaignId', 'AdGroupId', 'Impressions', 'Clicks', 'Cost'],
65
+ # Predicates are optional.
66
+ :predicates => {
67
+ :field => 'AdGroupStatus',
68
+ :operator => 'IN',
69
+ :values => ['ENABLED', 'PAUSED']
70
+ }
71
+ },
72
+ :report_name => 'Custom ADGROUP_PERFORMANCE_REPORT',
73
+ :report_type => 'ADGROUP_PERFORMANCE_REPORT',
74
+ :download_format => 'CSV',
75
+ :date_range_type => 'LAST_7_DAYS',
76
+ # Enable to get rows with zero impressions.
77
+ :include_zero_impressions => false
78
+ }
79
+
80
+ puts 'Retrieving %d reports with %d threads:' % [queue.size, THREADS]
81
+
82
+ reports_succeeded = Queue.new()
83
+ reports_failed = Queue.new()
84
+
85
+ # Creating a mutex to control access to the queue.
86
+ queue_mutex = Mutex.new
87
+
88
+ # Start all the threads.
89
+ threads = (1..THREADS).map do |thread_id|
90
+ Thread.new(report_definition) do |local_def|
91
+ cid = nil
92
+ begin
93
+ cid = queue_mutex.synchronize {(queue.empty?) ? nil : queue.pop(true)}
94
+ if cid
95
+ retry_count = 0
96
+ file_name = 'adgroup_%010d.csv' % cid
97
+ puts "[%2d/%d] Loading report for customer ID %s into '%s'..." %
98
+ [thread_id, retry_count,
99
+ AdwordsApi::Utils.format_id(cid), file_name]
100
+ begin
101
+ report_utils.download_report_as_file(local_def, file_name, cid)
102
+ reports_succeeded << {:cid => cid, :file_name => file_name}
103
+ rescue AdwordsApi::Errors::ReportError => e
104
+ if e.http_code == 500 && retry_count < MAX_RETRIES
105
+ retry_count += 1
106
+ sleep(retry_count * BACKOFF_FACTOR)
107
+ retry
108
+ else
109
+ puts(('Report failed for customer ID %s with code %d after %d ' +
110
+ 'retries.') % [cid, e.http_code, retry_count + 1])
111
+ reports_failed <<
112
+ {:cid => cid, :http_code => e.http_code, :message => e.message}
113
+ end
114
+ end
115
+ end
116
+ end while (cid != nil)
117
+ end
118
+ end
119
+
120
+ # Wait for all threads to finish.
121
+ threads.each { |aThread| aThread.join }
122
+
123
+ puts 'Download completed, results:'
124
+ puts 'Successful reports:'
125
+ while !reports_succeeded.empty? do
126
+ result = reports_succeeded.pop()
127
+ puts "\tClient ID %s => '%s'" %
128
+ [AdwordsApi::Utils.format_id(result[:cid]), result[:file_name]]
129
+ end
130
+ puts 'Failed reports:'
131
+ while !reports_failed.empty? do
132
+ result = reports_failed.pop()
133
+ puts "\tClient ID %s => Code: %d, Message: '%s'" %
134
+ [AdwordsApi::Utils.format_id(result[:cid]),
135
+ result[:http_code], result[:message]]
136
+ end
137
+ puts 'End of results.'
138
+ end
139
+
140
+ if __FILE__ == $0
141
+ API_VERSION = :v201502
142
+ # Number of parallel threads to spawn.
143
+ THREADS = 10
144
+ # Maximum number of retries for 500 errors.
145
+ MAX_RETRIES = 5
146
+ # Timeout between retries in seconds.
147
+ BACKOFF_FACTOR = 5
148
+
149
+ begin
150
+ parallel_report_download()
151
+
152
+ # Authorization error.
153
+ rescue AdsCommon::Errors::OAuth2VerificationRequired => e
154
+ puts "Authorization credentials are not valid. Edit adwords_api.yml for " +
155
+ "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " +
156
+ "to retrieve and store OAuth2 tokens."
157
+ puts "See this wiki page for more details:\n\n " +
158
+ 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2'
159
+
160
+ # HTTP errors.
161
+ rescue AdsCommon::Errors::HttpError => e
162
+ puts 'HTTP Error: %s' % e
163
+
164
+ # API errors.
165
+ rescue AdwordsApi::Errors::ReportError => e
166
+ puts 'Reporting Error: %s' % e.message
167
+ end
168
+ end
@@ -0,0 +1,269 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.mcloonan@gmail.com (Michael Cloonan)
5
+ #
6
+ # Copyright:: Copyright 2014, Google Inc. All Rights Reserved.
7
+ #
8
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
+ # implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ # This example creates a ProductPartition tree.
22
+
23
+ require 'adwords_api'
24
+
25
+ class ProductPartitionHelper
26
+ attr_reader :operations
27
+
28
+ def initialize(ad_group_id)
29
+ # The next temporary criterion ID to be used.
30
+ #
31
+ # When creating our tree we need to specify the parent-child relationships
32
+ # between nodes. However, until a criterion has been created on the server
33
+ # we do not have a criterionId with which to refer to it.
34
+ #
35
+ # Instead we can specify temporary IDs that are specific to a single mutate
36
+ # request. Once the criteria have been created they are assigned an ID as
37
+ # normal and the temporary ID will no longer refer to it.
38
+ #
39
+ # A valid temporary ID is any negative integer.
40
+ @next_id = -1
41
+
42
+ # The set of mutate operations needed to create the current tree.
43
+ @operations = []
44
+
45
+ # The ID of the AdGroup that we wish to attach the partition tree to.
46
+ @ad_group_id = ad_group_id
47
+ end
48
+
49
+ def create_subdivision(parent = nil, value = nil)
50
+ division = {
51
+ :xsi_type => 'ProductPartition',
52
+ :partition_type => 'SUBDIVISION',
53
+ :id => @next_id
54
+ }
55
+
56
+ @next_id -= 1
57
+
58
+ unless parent.nil? || value.nil?
59
+ division[:parent_criterion_id] = parent[:id]
60
+ division[:case_value] = value
61
+ end
62
+
63
+ ad_group_criterion = {
64
+ :xsi_type => 'BiddableAdGroupCriterion',
65
+ :ad_group_id => @ad_group_id,
66
+ :criterion => division
67
+ }
68
+
69
+ create_add_operation(ad_group_criterion)
70
+
71
+ return division
72
+ end
73
+
74
+ def create_unit(parent = nil, value = nil, bid_amount = nil)
75
+ unit = {
76
+ :xsi_type => 'ProductPartition',
77
+ :partition_type => 'UNIT'
78
+ }
79
+
80
+ unless parent.nil? || value.nil?
81
+ unit[:parent_criterion_id] = parent[:id]
82
+ unit[:case_value] = value
83
+ end
84
+
85
+ ad_group_criterion = {}
86
+ if bid_amount && bid_amount > 0
87
+ bidding_strategy_configuration = {
88
+ :bids => [{
89
+ :xsi_type => 'CpcBid',
90
+ :bid => {
91
+ :xsi_type => 'Money',
92
+ :micro_amount => bid_amount
93
+ }
94
+ }]
95
+ }
96
+ ad_group_criterion[:xsi_type] = 'BiddableAdGroupCriterion'
97
+ ad_group_criterion[:bidding_strategy_configuration] =
98
+ bidding_strategy_configuration
99
+ else
100
+ ad_group_criterion[:xsi_type] = 'NegativeAdGroupCriterion'
101
+ end
102
+ ad_group_criterion[:ad_group_id] = @ad_group_id
103
+ ad_group_criterion[:criterion] = unit
104
+
105
+ create_add_operation(ad_group_criterion)
106
+
107
+ return unit
108
+ end
109
+
110
+ private
111
+
112
+ def create_add_operation(ad_group_criterion)
113
+ operation = {
114
+ :operator => 'ADD',
115
+ :operand => ad_group_criterion
116
+ }
117
+
118
+ @operations << operation
119
+ end
120
+ end
121
+
122
+ def display_tree(node, children, level = 0)
123
+ value = ''
124
+ type = ''
125
+
126
+ if node[:case_value]
127
+ type = node[:case_value][:product_dimension_type]
128
+
129
+ value = case type
130
+ when 'ProductCanonicalCondition'
131
+ node[:case_value][:condition]
132
+ when 'ProductBiddingCategory'
133
+ "%s(%s)" % [node[:case_value][:type], node[:case_value][:value]]
134
+ else
135
+ node[:case_value][:value]
136
+ end
137
+ end
138
+
139
+ puts "%sid: %s, type: %s, value: %s" %
140
+ [' ' * level, node[:id], type, value]
141
+
142
+ children[node[:id]].each do |child_node|
143
+ display_tree(child_node, children, level + 1)
144
+ end
145
+ end
146
+
147
+ def add_product_partition_tree(ad_group_id)
148
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
149
+ # when called without parameters.
150
+ adwords = AdwordsApi::Api.new
151
+
152
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
153
+ # the configuration file or provide your own logger:
154
+ # adwords.logger = Logger.new('adwords_xml.log')
155
+
156
+ ad_group_criterion_srv =
157
+ adwords.service(:AdGroupCriterionService, API_VERSION)
158
+
159
+ helper = ProductPartitionHelper.new(ad_group_id)
160
+
161
+ root = helper.create_subdivision()
162
+
163
+ new_product_canonical_condition = {
164
+ :xsi_type => 'ProductCanonicalCondition',
165
+ :condition => 'NEW'
166
+ }
167
+
168
+ used_product_canonical_condition = {
169
+ :xsi_type => 'ProductCanonicalCondition',
170
+ :condition => 'USED'
171
+ }
172
+
173
+ other_product_canonical_condition = {
174
+ :xsi_type => 'ProductCanonicalCondition'
175
+ }
176
+
177
+ helper.create_unit(root, new_product_canonical_condition, 200000)
178
+ helper.create_unit(root, used_product_canonical_condition, 100000)
179
+ other_condition =
180
+ helper.create_subdivision(root, other_product_canonical_condition)
181
+
182
+ cool_product_brand = {
183
+ :xsi_type => 'ProductBrand',
184
+ :value => 'CoolBrand'
185
+ }
186
+
187
+ cheap_product_brand = {
188
+ :xsi_type => 'ProductBrand',
189
+ :value => 'CheapBrand'
190
+ }
191
+
192
+ other_product_brand = {
193
+ :xsi_type => 'ProductBrand'
194
+ }
195
+
196
+ helper.create_unit(other_condition, cool_product_brand, 900000)
197
+ helper.create_unit(other_condition, cheap_product_brand, 10000)
198
+ other_brand = helper.create_subdivision(other_condition, other_product_brand)
199
+
200
+ # The value for the bidding category is a fixed ID for the 'Luggage & Bags'
201
+ # category. You can retrieve IDs for categories from the ConstantDataService.
202
+ # See the get_product_taxonomy example for more details.
203
+ luggage_category = {
204
+ :xsi_type => 'ProductBiddingCategory',
205
+ :type => 'BIDDING_CATEGORY_L1',
206
+ :value => '-5914235892932915235'
207
+ }
208
+
209
+ generic_category = {
210
+ :xsi_type => 'ProductBiddingCategory',
211
+ :type => 'BIDDING_CATEGORY_L1'
212
+ }
213
+
214
+ helper.create_unit(other_brand, luggage_category, 750000)
215
+ helper.create_unit(other_brand, generic_category, 110000)
216
+
217
+ # Make the mutate request.
218
+ result = ad_group_criterion_srv.mutate(helper.operations)
219
+
220
+ children = {}
221
+ root_node = nil
222
+ # For each criterion, make an array containing each of its children.
223
+ # We always create the parent before the child, so we can rely on that here.
224
+ result[:value].each do |criterion|
225
+ children[criterion[:criterion][:id]] = []
226
+
227
+ if criterion[:criterion][:parent_criterion_id]
228
+ children[criterion[:criterion][:parent_criterion_id]] <<
229
+ criterion[:criterion]
230
+ else
231
+ root_node = criterion[:criterion]
232
+ end
233
+ end
234
+
235
+ display_tree(root_node, children)
236
+ end
237
+
238
+ if __FILE__ == $0
239
+ API_VERSION = :v201502
240
+
241
+ begin
242
+ ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
243
+
244
+ add_product_partition_tree(ad_group_id)
245
+
246
+ # Authorization error.
247
+ rescue AdsCommon::Errors::OAuth2VerificationRequired => e
248
+ puts "Authorization credentials are not valid. Edit adwords_api.yml for " +
249
+ "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " +
250
+ "to retrieve and store OAuth2 tokens."
251
+ puts "See this wiki page for more details:\n\n " +
252
+ 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2'
253
+
254
+ # HTTP errors.
255
+ rescue AdsCommon::Errors::HttpError => e
256
+ puts "HTTP Error: %s" % e
257
+
258
+ # API errors.
259
+ rescue AdwordsApi::Errors::ApiException => e
260
+ puts "Message: %s" % e.message
261
+ puts 'Errors:'
262
+ e.errors.each_with_index do |error, index|
263
+ puts "\tError [%d]:" % (index + 1)
264
+ error.each do |field, value|
265
+ puts "\t\t%s: %s" % [field, value]
266
+ end
267
+ end
268
+ end
269
+ end