google-adx-buyer-api 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/ChangeLog +4 -0
  2. data/README +39 -25
  3. data/examples/v201109/account_management/get_account_changes.rb +137 -0
  4. data/examples/{v201008 → v201109/basic_operations}/add_ad_group.rb +18 -28
  5. data/examples/{v201003 → v201109/basic_operations}/add_campaign.rb +28 -30
  6. data/examples/{v201008/add_ad_group_criteria.rb → v201109/basic_operations/add_placements.rb} +45 -46
  7. data/examples/v201109/basic_operations/add_thirdparty_redirect_ad.rb +105 -0
  8. data/examples/{v201008 → v201109/basic_operations}/delete_ad.rb +21 -35
  9. data/examples/{v201101 → v201109/basic_operations}/delete_ad_group.rb +18 -28
  10. data/examples/v201109/{delete_campaign.rb → basic_operations/delete_campaign.rb} +18 -28
  11. data/examples/{v201008/delete_ad_group_criterion.rb → v201109/basic_operations/delete_placement.rb} +24 -33
  12. data/examples/{v201101/get_all_ad_groups.rb → v201109/basic_operations/get_ad_groups.rb} +21 -35
  13. data/examples/{v201101/get_all_campaigns.rb → v201109/basic_operations/get_campaigns.rb} +14 -23
  14. data/examples/{v201008/get_all_ad_group_criteria.rb → v201109/basic_operations/get_placements.rb} +37 -36
  15. data/examples/v201109/{get_all_ads.rb → basic_operations/get_thirdparty_redirect_ads.rb} +37 -36
  16. data/examples/v201109/{get_client_customer_id.rb → basic_operations/pause_ad.rb} +27 -33
  17. data/examples/{v201003 → v201109/basic_operations}/update_ad_group.rb +18 -32
  18. data/examples/{v201101 → v201109/basic_operations}/update_campaign.rb +19 -33
  19. data/examples/{v201003/delete_ad_group_criterion.rb → v201109/basic_operations/update_placement.rb} +35 -36
  20. data/examples/v201109/{perform_mutate_job.rb → campaign_management/add_placements_in_bulk.rb} +11 -11
  21. data/examples/v201109/{get_all_disapproved_ads.rb → campaign_management/get_all_disapproved_ads.rb} +18 -28
  22. data/examples/v201109/error_handling/handle_captcha_challenge.rb +93 -0
  23. data/examples/v201109/error_handling/handle_partial_failures.rb +117 -0
  24. data/examples/v201109/error_handling/handle_policy_violation_error.rb +138 -0
  25. data/examples/v201109/error_handling/handle_two_factor_authorization_error.rb +87 -0
  26. data/examples/v201109/misc/get_all_images_and_videos.rb +101 -0
  27. data/examples/{v201101 → v201109/misc}/upload_image.rb +23 -33
  28. data/examples/v201109/{get_all_campaigns.rb → misc/use_oauth.rb} +42 -31
  29. data/examples/{v201003/get_related_placements.rb → v201109/optimization/get_placement_ideas.rb} +32 -37
  30. data/examples/{v201008/add_user_list.rb → v201109/remarketing/add_audience.rb} +49 -34
  31. data/examples/v201109/{add_conversion.rb → remarketing/add_conversion_tracker.rb} +31 -35
  32. data/examples/v201109/{download_adhoc_report.rb → reporting/download_criteria_report.rb} +24 -24
  33. data/examples/{v201008/download_report.rb → v201109/reporting/download_defined_report.rb} +20 -29
  34. data/examples/v201109/reporting/get_campaign_stats.rb +105 -0
  35. data/examples/{v201008/get_all_report_definitions.rb → v201109/reporting/get_defined_reports.rb} +18 -27
  36. data/examples/v201109/{get_all_carriers.rb → reporting/get_report_fields.rb} +18 -19
  37. data/examples/v201109/reporting/parallel_report_download.rb +159 -0
  38. data/examples/v201109/{add_campaign_targeting_criteria.rb → targeting/add_campaign_targeting_criteria.rb} +27 -19
  39. data/examples/v201109/targeting/get_campaign_targeting_criteria.rb +104 -0
  40. data/examples/v201109/{get_all_languages.rb → targeting/get_targetable_languages_and_carriers.rb} +18 -9
  41. data/examples/v201109/{get_location_criteria.rb → targeting/lookup_location.rb} +7 -7
  42. metadata +59 -191
  43. data/Rakefile +0 -50
  44. data/examples/v201003/add_ad_group.rb +0 -98
  45. data/examples/v201003/add_ad_group_criteria.rb +0 -100
  46. data/examples/v201003/add_ads.rb +0 -110
  47. data/examples/v201003/add_negative_campaign_criterion.rb +0 -95
  48. data/examples/v201003/add_placement_performance_report_definition.rb +0 -108
  49. data/examples/v201003/check_campaigns.rb +0 -132
  50. data/examples/v201003/delete_ad.rb +0 -94
  51. data/examples/v201003/delete_ad_group.rb +0 -86
  52. data/examples/v201003/delete_campaign.rb +0 -87
  53. data/examples/v201003/download_report.rb +0 -76
  54. data/examples/v201003/get_all_active_ad_group_criteria.rb +0 -93
  55. data/examples/v201003/get_all_ad_group_criteria.rb +0 -91
  56. data/examples/v201003/get_all_ad_groups.rb +0 -88
  57. data/examples/v201003/get_all_ads.rb +0 -91
  58. data/examples/v201003/get_all_campaign_targets.rb +0 -83
  59. data/examples/v201003/get_all_campaigns.rb +0 -83
  60. data/examples/v201003/get_all_disapproved_ads.rb +0 -94
  61. data/examples/v201003/get_all_images.rb +0 -88
  62. data/examples/v201003/get_all_paused_campaigns.rb +0 -86
  63. data/examples/v201003/get_all_report_definitions.rb +0 -84
  64. data/examples/v201003/get_criterion_bid_landscape.rb +0 -105
  65. data/examples/v201003/get_geo_location_info.rb +0 -108
  66. data/examples/v201003/get_report_fields.rb +0 -81
  67. data/examples/v201003/perform_bulk_mutate_job.rb +0 -222
  68. data/examples/v201003/set_campaign_targets.rb +0 -124
  69. data/examples/v201003/update_ad.rb +0 -95
  70. data/examples/v201003/update_ad_group_criterion.rb +0 -108
  71. data/examples/v201003/update_campaign.rb +0 -92
  72. data/examples/v201003/upload_image.rb +0 -97
  73. data/examples/v201008/add_ads.rb +0 -110
  74. data/examples/v201008/add_campaign.rb +0 -101
  75. data/examples/v201008/add_negative_campaign_criterion.rb +0 -95
  76. data/examples/v201008/add_placement_performance_report_definition.rb +0 -108
  77. data/examples/v201008/check_campaigns.rb +0 -132
  78. data/examples/v201008/delete_ad_group.rb +0 -86
  79. data/examples/v201008/delete_bulk_mutate_job.rb +0 -96
  80. data/examples/v201008/delete_campaign.rb +0 -87
  81. data/examples/v201008/delete_user_list.rb +0 -87
  82. data/examples/v201008/get_all_account_changes.rb +0 -144
  83. data/examples/v201008/get_all_active_ad_group_criteria.rb +0 -93
  84. data/examples/v201008/get_all_ad_groups.rb +0 -88
  85. data/examples/v201008/get_all_ads.rb +0 -91
  86. data/examples/v201008/get_all_campaign_targets.rb +0 -83
  87. data/examples/v201008/get_all_campaigns.rb +0 -83
  88. data/examples/v201008/get_all_disapproved_ads.rb +0 -94
  89. data/examples/v201008/get_all_images.rb +0 -88
  90. data/examples/v201008/get_all_paused_campaigns.rb +0 -86
  91. data/examples/v201008/get_all_user_lists.rb +0 -84
  92. data/examples/v201008/get_bulk_mutate_job.rb +0 -106
  93. data/examples/v201008/get_campaign_alerts.rb +0 -103
  94. data/examples/v201008/get_criterion_bid_landscape.rb +0 -105
  95. data/examples/v201008/get_geo_location_info.rb +0 -108
  96. data/examples/v201008/get_related_placements.rb +0 -114
  97. data/examples/v201008/get_report_fields.rb +0 -81
  98. data/examples/v201008/perform_bulk_mutate_job.rb +0 -222
  99. data/examples/v201008/set_campaign_targets.rb +0 -124
  100. data/examples/v201008/update_ad.rb +0 -95
  101. data/examples/v201008/update_ad_group.rb +0 -90
  102. data/examples/v201008/update_ad_group_criterion.rb +0 -108
  103. data/examples/v201008/update_campaign.rb +0 -93
  104. data/examples/v201008/update_user_list.rb +0 -91
  105. data/examples/v201008/upload_image.rb +0 -97
  106. data/examples/v201101/add_ad_group.rb +0 -98
  107. data/examples/v201101/add_ad_group_criteria.rb +0 -100
  108. data/examples/v201101/add_ads.rb +0 -110
  109. data/examples/v201101/add_campaign.rb +0 -108
  110. data/examples/v201101/add_conversion.rb +0 -97
  111. data/examples/v201101/add_negative_campaign_criterion.rb +0 -95
  112. data/examples/v201101/add_placement_performance_report_definition.rb +0 -108
  113. data/examples/v201101/add_rtb_campaign.rb +0 -113
  114. data/examples/v201101/add_user_list.rb +0 -122
  115. data/examples/v201101/check_campaigns.rb +0 -132
  116. data/examples/v201101/delete_ad.rb +0 -94
  117. data/examples/v201101/delete_ad_group_criterion.rb +0 -95
  118. data/examples/v201101/delete_bulk_mutate_job.rb +0 -96
  119. data/examples/v201101/delete_campaign.rb +0 -87
  120. data/examples/v201101/delete_user_list.rb +0 -87
  121. data/examples/v201101/download_report.rb +0 -76
  122. data/examples/v201101/get_ad_group_bid_landscape.rb +0 -103
  123. data/examples/v201101/get_all_account_changes.rb +0 -146
  124. data/examples/v201101/get_all_active_ad_group_criteria.rb +0 -106
  125. data/examples/v201101/get_all_ad_group_criteria.rb +0 -97
  126. data/examples/v201101/get_all_ads.rb +0 -99
  127. data/examples/v201101/get_all_campaign_targets.rb +0 -83
  128. data/examples/v201101/get_all_conversions.rb +0 -88
  129. data/examples/v201101/get_all_disapproved_ads.rb +0 -102
  130. data/examples/v201101/get_all_images.rb +0 -90
  131. data/examples/v201101/get_all_paused_campaigns.rb +0 -92
  132. data/examples/v201101/get_all_report_definitions.rb +0 -84
  133. data/examples/v201101/get_all_user_lists.rb +0 -87
  134. data/examples/v201101/get_bulk_mutate_job.rb +0 -106
  135. data/examples/v201101/get_campaign_alerts.rb +0 -103
  136. data/examples/v201101/get_criterion_bid_landscape.rb +0 -102
  137. data/examples/v201101/get_geo_location_info.rb +0 -108
  138. data/examples/v201101/get_related_placements.rb +0 -114
  139. data/examples/v201101/get_report_fields.rb +0 -81
  140. data/examples/v201101/perform_bulk_mutate_job.rb +0 -222
  141. data/examples/v201101/set_campaign_targets.rb +0 -111
  142. data/examples/v201101/update_ad.rb +0 -95
  143. data/examples/v201101/update_ad_group.rb +0 -90
  144. data/examples/v201101/update_ad_group_criterion.rb +0 -108
  145. data/examples/v201101/update_conversion.rb +0 -96
  146. data/examples/v201101/update_user_list.rb +0 -91
  147. data/examples/v201109/add_ad_group.rb +0 -98
  148. data/examples/v201109/add_ad_group_criteria.rb +0 -100
  149. data/examples/v201109/add_ads.rb +0 -110
  150. data/examples/v201109/add_campaign.rb +0 -108
  151. data/examples/v201109/add_negative_campaign_criterion.rb +0 -95
  152. data/examples/v201109/add_rtb_campaign.rb +0 -113
  153. data/examples/v201109/add_user_list.rb +0 -122
  154. data/examples/v201109/check_campaigns.rb +0 -132
  155. data/examples/v201109/delete_ad.rb +0 -94
  156. data/examples/v201109/delete_ad_group.rb +0 -86
  157. data/examples/v201109/delete_ad_group_criterion.rb +0 -95
  158. data/examples/v201109/delete_bulk_mutate_job.rb +0 -96
  159. data/examples/v201109/delete_user_list.rb +0 -87
  160. data/examples/v201109/download_report.rb +0 -76
  161. data/examples/v201109/get_ad_group_bid_landscape.rb +0 -103
  162. data/examples/v201109/get_all_account_changes.rb +0 -146
  163. data/examples/v201109/get_all_active_ad_group_criteria.rb +0 -106
  164. data/examples/v201109/get_all_ad_group_criteria.rb +0 -98
  165. data/examples/v201109/get_all_ad_groups.rb +0 -94
  166. data/examples/v201109/get_all_campaign_targets.rb +0 -83
  167. data/examples/v201109/get_all_conversions.rb +0 -88
  168. data/examples/v201109/get_all_images.rb +0 -90
  169. data/examples/v201109/get_all_paused_campaigns.rb +0 -92
  170. data/examples/v201109/get_all_report_definitions.rb +0 -84
  171. data/examples/v201109/get_all_user_lists.rb +0 -87
  172. data/examples/v201109/get_bulk_mutate_job.rb +0 -106
  173. data/examples/v201109/get_campaign_alerts.rb +0 -103
  174. data/examples/v201109/get_criterion_bid_landscape.rb +0 -102
  175. data/examples/v201109/get_geo_location_info.rb +0 -108
  176. data/examples/v201109/get_related_placements.rb +0 -114
  177. data/examples/v201109/get_report_fields.rb +0 -81
  178. data/examples/v201109/perform_bulk_mutate_job.rb +0 -222
  179. data/examples/v201109/update_ad.rb +0 -95
  180. data/examples/v201109/update_ad_group.rb +0 -90
  181. data/examples/v201109/update_ad_group_criterion.rb +0 -108
  182. data/examples/v201109/update_campaign.rb +0 -93
  183. data/examples/v201109/update_conversion.rb +0 -96
  184. data/examples/v201109/update_user_list.rb +0 -91
  185. data/examples/v201109/upload_image.rb +0 -97
@@ -1,4 +1,5 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
2
3
  #
3
4
  # Author:: api.dklimkin@gmail.com (Danial Klimkin)
4
5
  #
@@ -19,12 +20,9 @@
19
20
  #
20
21
  # This example gets and downloads an Ad Hoc report from a XML report definition.
21
22
 
22
- require 'rubygems'
23
23
  require 'adwords_api'
24
24
 
25
- API_VERSION = :v201109
26
-
27
- def download_adhoc_report()
25
+ def download_criteria_report(file_name)
28
26
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
29
27
  # when called without parameters.
30
28
  adwords = AdwordsApi::Api.new
@@ -36,37 +34,39 @@ def download_adhoc_report()
36
34
  # Get report utilities for the version.
37
35
  report_utils = adwords.report_utils(API_VERSION)
38
36
 
39
- # File name to write report to. To retrieve the report as return value, use
40
- # "download_report" method.
41
- file_name = 'INSERT_OUTPUT_FILE_NAME_HERE'
42
-
43
37
  # Define report definition. You can also pass your own XML text as a string.
44
38
  report_definition = {
45
39
  :selector => {
46
- :fields => ['CampaignId', 'Id', 'Impressions', 'Clicks', 'Cost'],
47
- # Predicates are optional.
48
- :predicates => {
49
- :field => 'Status',
50
- :operator => 'IN',
51
- :values => ['ENABLED', 'PAUSED']
52
- }
53
- },
54
- :report_name => 'Custom ADGROUP_PERFORMANCE_REPORT',
55
- :report_type => 'ADGROUP_PERFORMANCE_REPORT',
56
- :download_format => 'CSV',
57
- :date_range_type => 'LAST_7_DAYS',
58
- # Enable to get rows with zero impressions.
59
- :include_zero_impressions => false
40
+ :fields => ['CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType',
41
+ 'Impressions', 'Clicks', 'Cost'],
42
+ # Predicates are optional.
43
+ :predicates => {
44
+ :field => 'Status',
45
+ :operator => 'IN',
46
+ :values => ['ACTIVE', 'PAUSED']
47
+ }
48
+ },
49
+ :report_name => 'Last 7 days CRITERIA_PERFORMANCE_REPORT',
50
+ :report_type => 'CRITERIA_PERFORMANCE_REPORT',
51
+ :download_format => 'CSV',
52
+ :date_range_type => 'LAST_7_DAYS',
53
+ # Enable to get rows with zero impressions.
54
+ :include_zero_impressions => false
60
55
  }
61
56
 
62
57
  # Download report, using "download_report_as_file" utility method.
58
+ # To retrieve the report as return value, use "download_report" method.
63
59
  report_utils.download_report_as_file(report_definition, file_name)
64
60
  puts "Report was downloaded to '%s'." % file_name
65
61
  end
66
62
 
67
63
  if __FILE__ == $0
64
+ API_VERSION = :v201109
65
+
68
66
  begin
69
- download_adhoc_report()
67
+ # File name to write report to.
68
+ file_name = 'INSERT_OUTPUT_FILE_NAME_HERE'
69
+ download_criteria_report(file_name)
70
70
 
71
71
  # HTTP errors.
72
72
  rescue AdsCommon::Errors::HttpError => e
@@ -1,4 +1,5 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
2
3
  #
3
4
  # Author:: api.sgomes@gmail.com (Sérgio Gomes)
4
5
  #
@@ -17,16 +18,13 @@
17
18
  # See the License for the specific language governing permissions and
18
19
  # limitations under the License.
19
20
  #
20
- # This example gets and downloads a report from a report definition. To get a
21
- # report definition, run add_keywords_performance_report_definition.rb.
21
+ # This example gets and downloads a report from a report definition.
22
+ #
23
+ # Note: defined reports are deprecated, please use AdHoc reporting.
22
24
 
23
- require 'rubygems'
24
- gem 'google-adwords-api'
25
25
  require 'adwords_api'
26
26
 
27
- API_VERSION = :v201008
28
-
29
- def download_report()
27
+ def download_defined_report(report_definition_id, file_name)
30
28
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
31
29
  # when called without parameters.
32
30
  adwords = AdwordsApi::Api.new
@@ -37,9 +35,6 @@ def download_report()
37
35
 
38
36
  report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)
39
37
 
40
- report_definition_id = 'INSERT_REPORT_DEFINITION_ID'.to_i
41
- file_name = 'INSERT_OUTPUT_FILE_NAME_HERE'
42
-
43
38
  # Download report, using "download_report_as_file" extension method.
44
39
  report_def_srv.download_report_as_file(report_definition_id, file_name)
45
40
  puts 'Report with definition id %d was downloaded to \'%s\'.' %
@@ -47,29 +42,25 @@ def download_report()
47
42
  end
48
43
 
49
44
  if __FILE__ == $0
45
+ API_VERSION = :v201109
46
+
50
47
  begin
51
- download_report()
48
+ report_definition_id = 'INSERT_REPORT_DEFINITION_ID'.to_i
49
+ file_name = 'INSERT_OUTPUT_FILE_NAME_HERE'
50
+ download_defined_report(report_definition_id, file_name)
52
51
 
53
- # Connection error. Likely transitory.
54
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
55
- puts 'Connection Error: %s' % e
56
- puts 'Source: %s' % e.backtrace.first
52
+ # HTTP errors.
53
+ rescue AdsCommon::Errors::HttpError => e
54
+ puts "HTTP Error: %s" % e
57
55
 
58
- # API Error.
56
+ # API errors.
59
57
  rescue AdwordsApi::Errors::ApiException => e
60
- puts 'API Exception caught.'
61
- puts 'Message: %s' % e.message
62
- puts 'Code: %d' % e.code if e.code
63
- puts 'Trigger: %s' % e.trigger if e.trigger
58
+ puts "Message: %s" % e.message
64
59
  puts 'Errors:'
65
- if e.errors
66
- e.errors.each_with_index do |error, index|
67
- puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
68
- error.each_pair do |field, value|
69
- if field != :xsi_type
70
- puts ' %s: %s' % [field, value]
71
- end
72
- end
60
+ e.errors.each_with_index do |error, index|
61
+ puts "\tError [%d]:" % (index + 1)
62
+ error.each do |field, value|
63
+ puts "\t\t%s: %s" % [field, value]
73
64
  end
74
65
  end
75
66
  end
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2012, 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 various statistics for campaigns that received at least one
22
+ # impression during the last week. To get campaigns, run get_campaigns.rb.
23
+ #
24
+ # Tags: CampaignService.get
25
+
26
+ require 'adwords_api'
27
+ require 'date'
28
+
29
+ def get_campaign_stats()
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
+ campaign_srv = adwords.service(:CampaignService, API_VERSION)
39
+
40
+ # Prepare start and end date for the last week.
41
+ start_date = DateTime.parse((Date.today - 7).to_s).strftime("%Y%m%d")
42
+ end_date = DateTime.parse((Date.today - 1).to_s).strftime("%Y%m%d")
43
+
44
+ # Get all the campaigns for this account.
45
+ selector = {
46
+ :fields => ['Id', 'Name', 'Impressions', 'Clicks', 'Cost', 'Ctr'],
47
+ :predicates => [
48
+ {:field => 'Impressions', :operator => 'GREATER_THAN', :values => [0]}
49
+ ],
50
+ :date_range => {:min => start_date, :max => end_date},
51
+ :paging => {
52
+ :start_index => 0,
53
+ :number_results => PAGE_SIZE
54
+ }
55
+ }
56
+
57
+ # Set initial values.
58
+ offset, page = 0, {}
59
+
60
+ begin
61
+ page = campaign_srv.get(selector)
62
+ if page[:entries]
63
+ page[:entries].each do |campaign|
64
+ puts ("Campaign with ID %d, name '%s' had the following stats during" +
65
+ " the last week: ") % [campaign[:id], campaign[:name]]
66
+ stats = campaign[:campaign_stats]
67
+ puts "\tImpressions: %d" % stats[:impressions]
68
+ puts "\tClicks: %d" % stats[:clicks]
69
+ puts "\tCost: %.2f" % (stats[:cost][:micro_amount] / 1000000)
70
+ puts "\tCTR: %.2f%%" % (stats[:ctr] * 100)
71
+ end
72
+ # Increment values to request the next page.
73
+ offset += PAGE_SIZE
74
+ selector[:paging][:start_index] = offset
75
+ end
76
+ end while page[:total_num_entries] > offset
77
+
78
+ if page.include?(:total_num_entries)
79
+ puts "Total number of campaigns found: %d." % [page[:total_num_entries]]
80
+ end
81
+ end
82
+
83
+ if __FILE__ == $0
84
+ API_VERSION = :v201109
85
+ PAGE_SIZE = 500
86
+
87
+ begin
88
+ get_campaign_stats()
89
+
90
+ # HTTP errors.
91
+ rescue AdsCommon::Errors::HttpError => e
92
+ puts "HTTP Error: %s" % e
93
+
94
+ # API errors.
95
+ rescue AdwordsApi::Errors::ApiException => e
96
+ puts "Message: %s" % e.message
97
+ puts 'Errors:'
98
+ e.errors.each_with_index do |error, index|
99
+ puts "\tError [%d]:" % (index + 1)
100
+ error.each do |field, value|
101
+ puts "\t\t%s: %s" % [field, value]
102
+ end
103
+ end
104
+ end
105
+ end
@@ -1,4 +1,5 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
2
3
  #
3
4
  # Author:: api.sgomes@gmail.com (Sérgio Gomes)
4
5
  #
@@ -17,18 +18,14 @@
17
18
  # See the License for the specific language governing permissions and
18
19
  # limitations under the License.
19
20
  #
20
- # This example gets all report definitions. To add a report definition, run
21
- # add_keywords_performance_report.rb.
21
+ # This example gets all report definitions. Adding new definitions is
22
+ # deprecated, please use AdHoc reporting.
22
23
  #
23
24
  # Tags: ReportDefinitionService.get
24
25
 
25
- require 'rubygems'
26
- gem 'google-adwords-api'
27
26
  require 'adwords_api'
28
27
 
29
- API_VERSION = :v201008
30
-
31
- def get_all_report_definitions()
28
+ def get_defined_reports()
32
29
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
33
30
  # when called without parameters.
34
31
  adwords = AdwordsApi::Api.new
@@ -50,34 +47,28 @@ def get_all_report_definitions()
50
47
  "\"#{entry[:report_name]}\"."
51
48
  end
52
49
  else
53
- puts "No report definitions found."
50
+ puts 'No report definitions found.'
54
51
  end
55
52
  end
56
53
 
57
54
  if __FILE__ == $0
55
+ API_VERSION = :v201109
56
+
58
57
  begin
59
- get_all_report_definitions()
58
+ get_defined_reports()
60
59
 
61
- # Connection error. Likely transitory.
62
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
63
- puts 'Connection Error: %s' % e
64
- puts 'Source: %s' % e.backtrace.first
60
+ # HTTP errors.
61
+ rescue AdsCommon::Errors::HttpError => e
62
+ puts "HTTP Error: %s" % e
65
63
 
66
- # API Error.
64
+ # API errors.
67
65
  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
66
+ puts "Message: %s" % e.message
72
67
  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
68
+ e.errors.each_with_index do |error, index|
69
+ puts "\tError [%d]:" % (index + 1)
70
+ error.each do |field, value|
71
+ puts "\t\t%s: %s" % [field, value]
81
72
  end
82
73
  end
83
74
  end
@@ -1,6 +1,7 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
2
3
  #
3
- # Author:: api.dklimkin@gmail.com (Danial Klimkin)
4
+ # Author:: api.sgomes@gmail.com (Sérgio Gomes)
4
5
  #
5
6
  # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
6
7
  #
@@ -17,16 +18,13 @@
17
18
  # See the License for the specific language governing permissions and
18
19
  # limitations under the License.
19
20
  #
20
- # This example illustrates how to retrieve all carriers available for targeting.
21
+ # This example gets the list of possible report fields for a report type.
21
22
  #
22
- # Tags: ConstantDataService.getCarrierCriterion
23
+ # Tags: ReportDefinitionService.getReportFields
23
24
 
24
- require 'rubygems'
25
25
  require 'adwords_api'
26
26
 
27
- API_VERSION = :v201109
28
-
29
- def get_all_carriers()
27
+ def get_report_fields(report_type)
30
28
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
31
29
  # when called without parameters.
32
30
  adwords = AdwordsApi::Api.new
@@ -35,24 +33,25 @@ def get_all_carriers()
35
33
  # the configuration file or provide your own logger:
36
34
  # adwords.logger = Logger.new('adwords_xml.log')
37
35
 
38
- constant_data_srv = adwords.service(:ConstantDataService, API_VERSION)
39
-
40
- # Get all carriers from ConstantDataService.
41
- carriers = constant_data_srv.get_carrier_criterion()
36
+ report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)
42
37
 
43
- if carriers
44
- carriers.each do |carrier|
45
- puts "Carrier name is '%s', ID is %d and country code is '%s'." %
46
- [carrier[:name], carrier[:id], carrier[:country_code]]
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]
47
45
  end
48
- else
49
- puts 'No carriers were retrieved.'
50
46
  end
51
47
  end
52
48
 
53
49
  if __FILE__ == $0
50
+ API_VERSION = :v201109
51
+
54
52
  begin
55
- get_all_carriers()
53
+ report_type = 'INSERT_REPORT_TYPE_HERE'
54
+ get_report_fields(report_type)
56
55
 
57
56
  # HTTP errors.
58
57
  rescue AdsCommon::Errors::HttpError => e
@@ -0,0 +1,159 @@
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
+
23
+ require 'thread'
24
+
25
+ require 'adwords_api'
26
+ require 'adwords_api/utils'
27
+
28
+ def parallel_report_download()
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
+
33
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
34
+ # the configuration file or provide your own logger:
35
+ # adwords.logger = Logger.new('adwords_xml.log')
36
+
37
+ # Determine list of customer IDs to retrieve report for. For this example we
38
+ # will use ServicedAccountService to get all IDs in hierarchy.
39
+
40
+ serviced_account_srv = adwords.service(:ServicedAccountService, API_VERSION)
41
+
42
+ # Get the account hierarchy for this account.
43
+ selector = {:enable_paging => false}
44
+
45
+ # Run the request at the MCC level.
46
+ graph = adwords.use_mcc() {serviced_account_srv.get(selector)}
47
+
48
+ # Using queue to balance load between threads.
49
+ queue = Queue.new()
50
+
51
+ if graph and graph[:accounts] and !graph[:accounts].empty?
52
+ graph[:accounts].each {|account| queue << account[:customer_id]}
53
+ else
54
+ raise StandardError, 'Can not retrieve any customer ID'
55
+ end
56
+
57
+ # Get report utilities for the version.
58
+ report_utils = adwords.report_utils(API_VERSION)
59
+
60
+ # Define report definition. You can also pass your own XML text as a string.
61
+ report_definition = {
62
+ :selector => {
63
+ :fields => ['CampaignId', 'Id', 'Impressions', 'Clicks', 'Cost'],
64
+ # Predicates are optional.
65
+ :predicates => {
66
+ :field => 'Status',
67
+ :operator => 'IN',
68
+ :values => ['ENABLED', 'PAUSED']
69
+ }
70
+ },
71
+ :report_name => 'Custom ADGROUP_PERFORMANCE_REPORT',
72
+ :report_type => 'ADGROUP_PERFORMANCE_REPORT',
73
+ :download_format => 'CSV',
74
+ :date_range_type => 'LAST_7_DAYS',
75
+ # Enable to get rows with zero impressions.
76
+ :include_zero_impressions => false
77
+ }
78
+
79
+ puts "Retrieving %d reports with %d threads:" % [queue.size, THREADS]
80
+
81
+ reports_succeeded = Queue.new()
82
+ reports_failed = Queue.new()
83
+
84
+ # Creating a mutex to control access to the queue.
85
+ queue_mutex = Mutex.new
86
+
87
+ # Start all the threads.
88
+ threads = (1..THREADS).map do |thread_id|
89
+ Thread.new(report_definition) do |local_def|
90
+ cid = nil
91
+ begin
92
+ cid = queue_mutex.synchronize {(queue.empty?) ? nil : queue.pop(true)}
93
+ if cid
94
+ retry_count = 0
95
+ file_name = "adgroup_%010d.csv" % cid
96
+ puts "[%2d/%d] Loading report for customer ID %s into '%s'..." %
97
+ [thread_id, retry_count,
98
+ AdwordsApi::Utils.format_id(cid), file_name]
99
+ begin
100
+ report_utils.download_report_as_file(local_def, file_name, cid)
101
+ reports_succeeded << {:cid => cid, :file_name => file_name}
102
+ rescue AdwordsApi::Errors::ReportError => e
103
+ if e.http_code == 500 && retry_count < MAX_RETRIES
104
+ retry_count += 1
105
+ sleep(retry_count * BACKOFF_FACTOR)
106
+ retry
107
+ else
108
+ puts(("Report failed for customer ID %s with code %d after %d " +
109
+ "retries.") % [cid, e.http_code, retry_count + 1])
110
+ reports_failed <<
111
+ {:cid => cid, :http_code => e.http_code, :message => e.message}
112
+ end
113
+ end
114
+ end
115
+ end while (cid != nil)
116
+ end
117
+ end
118
+
119
+ # Wait for all threads to finish.
120
+ threads.each { |aThread| aThread.join }
121
+
122
+ puts 'Download completed, results:'
123
+ puts 'Successful reports:'
124
+ while !reports_succeeded.empty? do
125
+ result = reports_succeeded.pop()
126
+ puts "\tClient ID %s => '%s'" %
127
+ [AdwordsApi::Utils.format_id(result[:cid]), result[:file_name]]
128
+ end
129
+ puts 'Failed reports:'
130
+ while !reports_failed.empty? do
131
+ result = reports_failed.pop()
132
+ puts "\tClient ID %s => Code: %d, Message: '%s'" %
133
+ [AdwordsApi::Utils.format_id(result[:cid]),
134
+ result[:http_code], result[:message]]
135
+ end
136
+ puts 'End of results.'
137
+ end
138
+
139
+ if __FILE__ == $0
140
+ API_VERSION = :v201109
141
+ # Number of parallel threads to spawn.
142
+ THREADS = 10
143
+ # Maximum number of retries for 500 errors.
144
+ MAX_RETRIES = 5
145
+ # Timeout between retries in seconds.
146
+ BACKOFF_FACTOR = 5
147
+
148
+ begin
149
+ parallel_report_download()
150
+
151
+ # HTTP errors.
152
+ rescue AdsCommon::Errors::HttpError => e
153
+ puts "HTTP Error: %s" % e
154
+
155
+ # API errors.
156
+ rescue AdwordsApi::Errors::ReportError => e
157
+ puts "Reporting Error: %s" % e.message
158
+ end
159
+ end