google-adwords-api 0.15.2 → 0.16.0

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 (167) hide show
  1. checksums.yaml +8 -8
  2. data/ChangeLog +7 -0
  3. data/adwords_api.yml +3 -2
  4. data/examples/v201506/basic_operations/get_keywords.rb +4 -3
  5. data/examples/v201506/misc/{create_ad_words_session_without_properties_file.rb → use_runtime_config.rb} +2 -2
  6. data/lib/adwords_api.rb +1 -1
  7. data/lib/adwords_api/api_config.rb +0 -86
  8. data/lib/adwords_api/report_header_handler.rb +4 -4
  9. data/lib/adwords_api/version.rb +1 -1
  10. data/test/templates/v201502/basic_operations_get_campaigns.def +1 -1
  11. data/test/templates/v201502/{misc_use_oauth2_jwt.def → misc_use_oauth2_service_account.def} +2 -2
  12. data/test/templates/v201506/basic_operations_get_campaigns.def +1 -1
  13. data/test/templates/v201506/{misc_use_oauth2_jwt.def → misc_use_oauth2_service_account.def} +2 -2
  14. metadata +7 -160
  15. data/examples/v201409/account_management/create_account.rb +0 -92
  16. data/examples/v201409/account_management/get_account_changes.rb +0 -145
  17. data/examples/v201409/account_management/get_account_hierarchy.rb +0 -102
  18. data/examples/v201409/advanced_operations/add_ad_customizers.rb +0 -277
  19. data/examples/v201409/advanced_operations/add_ad_group_bid_modifier.rb +0 -105
  20. data/examples/v201409/advanced_operations/add_click_to_download_ad.rb +0 -137
  21. data/examples/v201409/advanced_operations/add_text_ad_with_upgraded_urls.rb +0 -137
  22. data/examples/v201409/advanced_operations/get_ad_group_bid_modifiers.rb +0 -106
  23. data/examples/v201409/advanced_operations/upload_offline_conversions.rb +0 -117
  24. data/examples/v201409/advanced_operations/use_shared_bidding_strategy.rb +0 -152
  25. data/examples/v201409/basic_operations/add_ad_groups.rb +0 -144
  26. data/examples/v201409/basic_operations/add_campaigns.rb +0 -143
  27. data/examples/v201409/basic_operations/add_keywords.rb +0 -118
  28. data/examples/v201409/basic_operations/add_text_ads.rb +0 -113
  29. data/examples/v201409/basic_operations/get_ad_groups.rb +0 -106
  30. data/examples/v201409/basic_operations/get_campaigns.rb +0 -101
  31. data/examples/v201409/basic_operations/get_campaigns_with_awql.rb +0 -93
  32. data/examples/v201409/basic_operations/get_keywords.rb +0 -112
  33. data/examples/v201409/basic_operations/get_text_ads.rb +0 -114
  34. data/examples/v201409/basic_operations/pause_ad.rb +0 -92
  35. data/examples/v201409/basic_operations/remove_ad.rb +0 -93
  36. data/examples/v201409/basic_operations/remove_ad_group.rb +0 -89
  37. data/examples/v201409/basic_operations/remove_campaign.rb +0 -91
  38. data/examples/v201409/basic_operations/remove_keyword.rb +0 -98
  39. data/examples/v201409/basic_operations/update_ad_group.rb +0 -89
  40. data/examples/v201409/basic_operations/update_campaign.rb +0 -90
  41. data/examples/v201409/basic_operations/update_keyword.rb +0 -110
  42. data/examples/v201409/campaign_management/add_campaign_labels.rb +0 -86
  43. data/examples/v201409/campaign_management/add_experiment.rb +0 -166
  44. data/examples/v201409/campaign_management/add_keywords_in_bulk.rb +0 -158
  45. data/examples/v201409/campaign_management/add_location_extension.rb +0 -125
  46. data/examples/v201409/campaign_management/get_all_disapproved_ads.rb +0 -101
  47. data/examples/v201409/campaign_management/get_all_disapproved_ads_with_awql.rb +0 -93
  48. data/examples/v201409/campaign_management/get_campaigns_by_label.rb +0 -112
  49. data/examples/v201409/campaign_management/promote_experiment.rb +0 -85
  50. data/examples/v201409/campaign_management/set_ad_parameters.rb +0 -122
  51. data/examples/v201409/campaign_management/set_criterion_bid_modifier.rb +0 -108
  52. data/examples/v201409/campaign_management/validate_text_ad.rb +0 -114
  53. data/examples/v201409/error_handling/handle_partial_failures.rb +0 -134
  54. data/examples/v201409/error_handling/handle_policy_violation_error.rb +0 -145
  55. data/examples/v201409/extensions/add_google_my_business_location_extensions.rb +0 -183
  56. data/examples/v201409/extensions/add_site_links.rb +0 -167
  57. data/examples/v201409/extensions/add_site_links_using_feeds.rb +0 -306
  58. data/examples/v201409/migration/migrate_to_extension_settings.rb +0 -392
  59. data/examples/v201409/migration/upgrade_ad_url.rb +0 -97
  60. data/examples/v201409/misc/create_ad_words_session_without_properties_file.rb +0 -94
  61. data/examples/v201409/misc/get_all_images_and_videos.rb +0 -108
  62. data/examples/v201409/misc/setup_oauth2.rb +0 -88
  63. data/examples/v201409/misc/upload_image.rb +0 -97
  64. data/examples/v201409/misc/use_oauth2_jwt.rb +0 -97
  65. data/examples/v201409/optimization/estimate_keyword_traffic.rb +0 -155
  66. data/examples/v201409/optimization/get_keyword_bid_simulations.rb +0 -100
  67. data/examples/v201409/optimization/get_keyword_ideas.rb +0 -130
  68. data/examples/v201409/remarketing/add_audience.rb +0 -122
  69. data/examples/v201409/remarketing/add_conversion_tracker.rb +0 -105
  70. data/examples/v201409/remarketing/add_rule_based_user_lists.rb +0 -171
  71. data/examples/v201409/reporting/download_criteria_report.rb +0 -87
  72. data/examples/v201409/reporting/download_criteria_report_with_awql.rb +0 -86
  73. data/examples/v201409/reporting/get_report_fields.rb +0 -79
  74. data/examples/v201409/reporting/parallel_report_download.rb +0 -171
  75. data/examples/v201409/shopping_campaigns/add_product_partition_tree.rb +0 -269
  76. data/examples/v201409/shopping_campaigns/add_product_scope.rb +0 -133
  77. data/examples/v201409/shopping_campaigns/add_shopping_campaign.rb +0 -131
  78. data/examples/v201409/shopping_campaigns/get_product_category_taxonomy.rb +0 -117
  79. data/examples/v201409/targeting/add_campaign_targeting_criteria.rb +0 -173
  80. data/examples/v201409/targeting/add_demographic_targeting_criteria.rb +0 -116
  81. data/examples/v201409/targeting/get_campaign_targeting_criteria.rb +0 -110
  82. data/examples/v201409/targeting/get_targetable_languages_and_carriers.rb +0 -94
  83. data/examples/v201409/targeting/lookup_location.rb +0 -112
  84. data/lib/adwords_api/v201409/ad_customizer_feed_service.rb +0 -38
  85. data/lib/adwords_api/v201409/ad_customizer_feed_service_registry.rb +0 -46
  86. data/lib/adwords_api/v201409/ad_group_ad_service.rb +0 -50
  87. data/lib/adwords_api/v201409/ad_group_ad_service_registry.rb +0 -46
  88. data/lib/adwords_api/v201409/ad_group_bid_modifier_service.rb +0 -42
  89. data/lib/adwords_api/v201409/ad_group_bid_modifier_service_registry.rb +0 -46
  90. data/lib/adwords_api/v201409/ad_group_criterion_service.rb +0 -46
  91. data/lib/adwords_api/v201409/ad_group_criterion_service_registry.rb +0 -46
  92. data/lib/adwords_api/v201409/ad_group_extension_setting_service.rb +0 -42
  93. data/lib/adwords_api/v201409/ad_group_extension_setting_service_registry.rb +0 -46
  94. data/lib/adwords_api/v201409/ad_group_feed_service.rb +0 -42
  95. data/lib/adwords_api/v201409/ad_group_feed_service_registry.rb +0 -46
  96. data/lib/adwords_api/v201409/ad_group_service.rb +0 -46
  97. data/lib/adwords_api/v201409/ad_group_service_registry.rb +0 -46
  98. data/lib/adwords_api/v201409/ad_param_service.rb +0 -38
  99. data/lib/adwords_api/v201409/ad_param_service_registry.rb +0 -46
  100. data/lib/adwords_api/v201409/adwords_user_list_service.rb +0 -38
  101. data/lib/adwords_api/v201409/adwords_user_list_service_registry.rb +0 -46
  102. data/lib/adwords_api/v201409/bidding_strategy_service.rb +0 -42
  103. data/lib/adwords_api/v201409/bidding_strategy_service_registry.rb +0 -46
  104. data/lib/adwords_api/v201409/budget_order_service.rb +0 -42
  105. data/lib/adwords_api/v201409/budget_order_service_registry.rb +0 -46
  106. data/lib/adwords_api/v201409/budget_service.rb +0 -42
  107. data/lib/adwords_api/v201409/budget_service_registry.rb +0 -46
  108. data/lib/adwords_api/v201409/campaign_ad_extension_service.rb +0 -38
  109. data/lib/adwords_api/v201409/campaign_ad_extension_service_registry.rb +0 -46
  110. data/lib/adwords_api/v201409/campaign_criterion_service.rb +0 -42
  111. data/lib/adwords_api/v201409/campaign_criterion_service_registry.rb +0 -46
  112. data/lib/adwords_api/v201409/campaign_extension_setting_service.rb +0 -42
  113. data/lib/adwords_api/v201409/campaign_extension_setting_service_registry.rb +0 -46
  114. data/lib/adwords_api/v201409/campaign_feed_service.rb +0 -42
  115. data/lib/adwords_api/v201409/campaign_feed_service_registry.rb +0 -46
  116. data/lib/adwords_api/v201409/campaign_service.rb +0 -46
  117. data/lib/adwords_api/v201409/campaign_service_registry.rb +0 -46
  118. data/lib/adwords_api/v201409/campaign_shared_set_service.rb +0 -38
  119. data/lib/adwords_api/v201409/campaign_shared_set_service_registry.rb +0 -47
  120. data/lib/adwords_api/v201409/constant_data_service.rb +0 -66
  121. data/lib/adwords_api/v201409/constant_data_service_registry.rb +0 -46
  122. data/lib/adwords_api/v201409/conversion_tracker_service.rb +0 -38
  123. data/lib/adwords_api/v201409/conversion_tracker_service_registry.rb +0 -46
  124. data/lib/adwords_api/v201409/customer_extension_setting_service.rb +0 -42
  125. data/lib/adwords_api/v201409/customer_extension_setting_service_registry.rb +0 -46
  126. data/lib/adwords_api/v201409/customer_feed_service.rb +0 -42
  127. data/lib/adwords_api/v201409/customer_feed_service_registry.rb +0 -46
  128. data/lib/adwords_api/v201409/customer_service.rb +0 -38
  129. data/lib/adwords_api/v201409/customer_service_registry.rb +0 -46
  130. data/lib/adwords_api/v201409/customer_sync_service.rb +0 -34
  131. data/lib/adwords_api/v201409/customer_sync_service_registry.rb +0 -47
  132. data/lib/adwords_api/v201409/data_service.rb +0 -54
  133. data/lib/adwords_api/v201409/data_service_registry.rb +0 -46
  134. data/lib/adwords_api/v201409/experiment_service.rb +0 -38
  135. data/lib/adwords_api/v201409/experiment_service_registry.rb +0 -46
  136. data/lib/adwords_api/v201409/feed_item_service.rb +0 -42
  137. data/lib/adwords_api/v201409/feed_item_service_registry.rb +0 -46
  138. data/lib/adwords_api/v201409/feed_mapping_service.rb +0 -42
  139. data/lib/adwords_api/v201409/feed_mapping_service_registry.rb +0 -46
  140. data/lib/adwords_api/v201409/feed_service.rb +0 -42
  141. data/lib/adwords_api/v201409/feed_service_registry.rb +0 -46
  142. data/lib/adwords_api/v201409/geo_location_service.rb +0 -34
  143. data/lib/adwords_api/v201409/geo_location_service_registry.rb +0 -46
  144. data/lib/adwords_api/v201409/label_service.rb +0 -42
  145. data/lib/adwords_api/v201409/label_service_registry.rb +0 -46
  146. data/lib/adwords_api/v201409/location_criterion_service.rb +0 -38
  147. data/lib/adwords_api/v201409/location_criterion_service_registry.rb +0 -46
  148. data/lib/adwords_api/v201409/managed_customer_service.rb +0 -50
  149. data/lib/adwords_api/v201409/managed_customer_service_registry.rb +0 -46
  150. data/lib/adwords_api/v201409/media_service.rb +0 -42
  151. data/lib/adwords_api/v201409/media_service_registry.rb +0 -46
  152. data/lib/adwords_api/v201409/mutate_job_service.rb +0 -42
  153. data/lib/adwords_api/v201409/mutate_job_service_registry.rb +0 -46
  154. data/lib/adwords_api/v201409/offline_conversion_feed_service.rb +0 -34
  155. data/lib/adwords_api/v201409/offline_conversion_feed_service_registry.rb +0 -46
  156. data/lib/adwords_api/v201409/report_definition_service.rb +0 -34
  157. data/lib/adwords_api/v201409/report_definition_service_registry.rb +0 -46
  158. data/lib/adwords_api/v201409/shared_criterion_service.rb +0 -38
  159. data/lib/adwords_api/v201409/shared_criterion_service_registry.rb +0 -47
  160. data/lib/adwords_api/v201409/shared_set_service.rb +0 -38
  161. data/lib/adwords_api/v201409/shared_set_service_registry.rb +0 -47
  162. data/lib/adwords_api/v201409/targeting_idea_service.rb +0 -34
  163. data/lib/adwords_api/v201409/targeting_idea_service_registry.rb +0 -46
  164. data/lib/adwords_api/v201409/traffic_estimator_service.rb +0 -34
  165. data/lib/adwords_api/v201409/traffic_estimator_service_registry.rb +0 -46
  166. data/test/templates/v201409/basic_operations_get_campaigns.def +0 -116
  167. data/test/templates/v201409/misc_use_oauth2_jwt.def +0 -131
@@ -1,79 +0,0 @@
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 = :v201409
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
@@ -1,171 +0,0 @@
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.
23
- #
24
- # Note: This code example uses MCC-level calls and won't work with Test
25
- # Accounts, see: https://developers.google.com/adwords/api/docs/test-accounts
26
-
27
- require 'thread'
28
-
29
- require 'adwords_api'
30
- require 'adwords_api/utils'
31
-
32
- def parallel_report_download()
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
- # Determine list of customer IDs to retrieve report for. For this example we
42
- # will use ManagedCustomerService to get all IDs in hierarchy.
43
-
44
- managed_customer_srv = adwords.service(:ManagedCustomerService, API_VERSION)
45
-
46
- # Get the account hierarchy for this account.
47
- selector = {:fields => ['CustomerId']}
48
-
49
- # Run the request at the MCC level.
50
- graph = managed_customer_srv.get(selector)
51
-
52
- # Using queue to balance load between threads.
53
- queue = Queue.new()
54
-
55
- if graph and graph[:entries] and !graph[:entries].empty?
56
- graph[:entries].each {|account| queue << account[:customer_id]}
57
- else
58
- raise StandardError, 'Can not retrieve any customer ID'
59
- end
60
-
61
- # Get report utilities for the version.
62
- report_utils = adwords.report_utils(API_VERSION)
63
-
64
- # Define report definition. You can also pass your own XML text as a string.
65
- report_definition = {
66
- :selector => {
67
- :fields => ['CampaignId', 'Id', 'Impressions', 'Clicks', 'Cost'],
68
- # Predicates are optional.
69
- :predicates => {
70
- :field => 'Status',
71
- :operator => 'IN',
72
- :values => ['ENABLED', 'PAUSED']
73
- }
74
- },
75
- :report_name => 'Custom ADGROUP_PERFORMANCE_REPORT',
76
- :report_type => 'ADGROUP_PERFORMANCE_REPORT',
77
- :download_format => 'CSV',
78
- :date_range_type => 'LAST_7_DAYS',
79
- # Enable to get rows with zero impressions.
80
- :include_zero_impressions => false
81
- }
82
-
83
- puts 'Retrieving %d reports with %d threads:' % [queue.size, THREADS]
84
-
85
- reports_succeeded = Queue.new()
86
- reports_failed = Queue.new()
87
-
88
- # Creating a mutex to control access to the queue.
89
- queue_mutex = Mutex.new
90
-
91
- # Start all the threads.
92
- threads = (1..THREADS).map do |thread_id|
93
- Thread.new(report_definition) do |local_def|
94
- cid = nil
95
- begin
96
- cid = queue_mutex.synchronize {(queue.empty?) ? nil : queue.pop(true)}
97
- if cid
98
- retry_count = 0
99
- file_name = 'adgroup_%010d.csv' % cid
100
- puts "[%2d/%d] Loading report for customer ID %s into '%s'..." %
101
- [thread_id, retry_count,
102
- AdwordsApi::Utils.format_id(cid), file_name]
103
- begin
104
- report_utils.download_report_as_file(local_def, file_name, cid)
105
- reports_succeeded << {:cid => cid, :file_name => file_name}
106
- rescue AdwordsApi::Errors::ReportError => e
107
- if e.http_code == 500 && retry_count < MAX_RETRIES
108
- retry_count += 1
109
- sleep(retry_count * BACKOFF_FACTOR)
110
- retry
111
- else
112
- puts(('Report failed for customer ID %s with code %d after %d ' +
113
- 'retries.') % [cid, e.http_code, retry_count + 1])
114
- reports_failed <<
115
- {:cid => cid, :http_code => e.http_code, :message => e.message}
116
- end
117
- end
118
- end
119
- end while (cid != nil)
120
- end
121
- end
122
-
123
- # Wait for all threads to finish.
124
- threads.each { |aThread| aThread.join }
125
-
126
- puts 'Download completed, results:'
127
- puts 'Successful reports:'
128
- while !reports_succeeded.empty? do
129
- result = reports_succeeded.pop()
130
- puts "\tClient ID %s => '%s'" %
131
- [AdwordsApi::Utils.format_id(result[:cid]), result[:file_name]]
132
- end
133
- puts 'Failed reports:'
134
- while !reports_failed.empty? do
135
- result = reports_failed.pop()
136
- puts "\tClient ID %s => Code: %d, Message: '%s'" %
137
- [AdwordsApi::Utils.format_id(result[:cid]),
138
- result[:http_code], result[:message]]
139
- end
140
- puts 'End of results.'
141
- end
142
-
143
- if __FILE__ == $0
144
- API_VERSION = :v201409
145
- # Number of parallel threads to spawn.
146
- THREADS = 10
147
- # Maximum number of retries for 500 errors.
148
- MAX_RETRIES = 5
149
- # Timeout between retries in seconds.
150
- BACKOFF_FACTOR = 5
151
-
152
- begin
153
- parallel_report_download()
154
-
155
- # Authorization error.
156
- rescue AdsCommon::Errors::OAuth2VerificationRequired => e
157
- puts "Authorization credentials are not valid. Edit adwords_api.yml for " +
158
- "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " +
159
- "to retrieve and store OAuth2 tokens."
160
- puts "See this wiki page for more details:\n\n " +
161
- 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2'
162
-
163
- # HTTP errors.
164
- rescue AdsCommon::Errors::HttpError => e
165
- puts 'HTTP Error: %s' % e
166
-
167
- # API errors.
168
- rescue AdwordsApi::Errors::ReportError => e
169
- puts 'Reporting Error: %s' % e.message
170
- end
171
- end
@@ -1,269 +0,0 @@
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 = :v201409
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