google-adwords-api 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/ChangeLog +3 -0
  2. data/README +3 -3
  3. data/examples/v201206/basic_operations/add_campaigns.rb +7 -2
  4. data/examples/v201209/account_management/create_account.rb +84 -0
  5. data/examples/v201209/account_management/get_account_alerts.rb +111 -0
  6. data/examples/v201209/account_management/get_account_changes.rb +137 -0
  7. data/examples/v201209/account_management/get_account_hierarchy.rb +92 -0
  8. data/examples/v201209/account_management/get_client_customer_id.rb +85 -0
  9. data/examples/v201209/account_management/get_client_unit_usage.rb +87 -0
  10. data/examples/v201209/basic_operations/add_ad_groups.rb +136 -0
  11. data/examples/v201209/basic_operations/add_campaigns.rb +159 -0
  12. data/examples/v201209/basic_operations/add_keywords.rb +108 -0
  13. data/examples/v201209/basic_operations/add_text_ads.rb +105 -0
  14. data/examples/v201209/basic_operations/delete_ad.rb +85 -0
  15. data/examples/v201209/basic_operations/delete_ad_group.rb +102 -0
  16. data/examples/v201209/basic_operations/delete_campaign.rb +103 -0
  17. data/examples/v201209/basic_operations/delete_keyword.rb +90 -0
  18. data/examples/v201209/basic_operations/get_ad_groups.rb +98 -0
  19. data/examples/v201209/basic_operations/get_campaigns.rb +93 -0
  20. data/examples/v201209/basic_operations/get_campaigns_with_awql.rb +85 -0
  21. data/examples/v201209/basic_operations/get_keywords.rb +104 -0
  22. data/examples/v201209/basic_operations/get_text_ads.rb +106 -0
  23. data/examples/v201209/basic_operations/pause_ad.rb +84 -0
  24. data/examples/v201209/basic_operations/update_ad_group.rb +81 -0
  25. data/examples/v201209/basic_operations/update_campaign.rb +82 -0
  26. data/examples/v201209/basic_operations/update_keyword.rb +99 -0
  27. data/examples/v201209/campaign_management/add_experiment.rb +158 -0
  28. data/examples/v201209/campaign_management/add_keywords_in_bulk.rb +150 -0
  29. data/examples/v201209/campaign_management/add_location_extension.rb +117 -0
  30. data/examples/v201209/campaign_management/add_location_extension_override.rb +83 -0
  31. data/examples/v201209/campaign_management/get_all_disapproved_ads.rb +93 -0
  32. data/examples/v201209/campaign_management/get_all_disapproved_ads_with_awql.rb +84 -0
  33. data/examples/v201209/campaign_management/promote_experiment.rb +77 -0
  34. data/examples/v201209/campaign_management/set_ad_parameters.rb +114 -0
  35. data/examples/v201209/campaign_management/validate_text_ad.rb +106 -0
  36. data/examples/v201209/error_handling/handle_captcha_challenge.rb +93 -0
  37. data/examples/v201209/error_handling/handle_partial_failures.rb +126 -0
  38. data/examples/v201209/error_handling/handle_policy_violation_error.rb +137 -0
  39. data/examples/v201209/error_handling/handle_two_factor_authorization_error.rb +87 -0
  40. data/examples/v201209/misc/get_all_images_and_videos.rb +100 -0
  41. data/examples/v201209/misc/upload_image.rb +89 -0
  42. data/examples/v201209/misc/use_oauth.rb +100 -0
  43. data/examples/v201209/misc/use_oauth2.rb +97 -0
  44. data/examples/v201209/optimization/estimate_keyword_traffic.rb +129 -0
  45. data/examples/v201209/optimization/get_keyword_bid_simulations.rb +92 -0
  46. data/examples/v201209/optimization/get_keyword_ideas.rb +114 -0
  47. data/examples/v201209/optimization/get_placement_ideas.rb +104 -0
  48. data/examples/v201209/remarketing/add_audience.rb +115 -0
  49. data/examples/v201209/remarketing/add_conversion_tracker.rb +97 -0
  50. data/examples/v201209/reporting/download_criteria_report.rb +79 -0
  51. data/examples/v201209/reporting/download_criteria_report_with_awql.rb +78 -0
  52. data/examples/v201209/reporting/get_campaign_stats.rb +105 -0
  53. data/examples/v201209/reporting/get_report_fields.rb +71 -0
  54. data/examples/v201209/reporting/parallel_report_download.rb +160 -0
  55. data/examples/v201209/targeting/add_campaign_targeting_criteria.rb +118 -0
  56. data/examples/v201209/targeting/add_demographic_targeting_criteria.rb +108 -0
  57. data/examples/v201209/targeting/get_campaign_targeting_criteria.rb +102 -0
  58. data/examples/v201209/targeting/get_targetable_languages_and_carriers.rb +86 -0
  59. data/examples/v201209/targeting/lookup_location.rb +104 -0
  60. data/lib/adwords_api/api_config.rb +64 -5
  61. data/lib/adwords_api/errors.rb +15 -0
  62. data/lib/adwords_api/report_utils.rb +30 -11
  63. data/lib/adwords_api/v201209/ad_extension_override_service.rb +38 -0
  64. data/lib/adwords_api/v201209/ad_extension_override_service_registry.rb +46 -0
  65. data/lib/adwords_api/v201209/ad_group_ad_service.rb +42 -0
  66. data/lib/adwords_api/v201209/ad_group_ad_service_registry.rb +46 -0
  67. data/lib/adwords_api/v201209/ad_group_criterion_service.rb +42 -0
  68. data/lib/adwords_api/v201209/ad_group_criterion_service_registry.rb +46 -0
  69. data/lib/adwords_api/v201209/ad_group_service.rb +42 -0
  70. data/lib/adwords_api/v201209/ad_group_service_registry.rb +46 -0
  71. data/lib/adwords_api/v201209/ad_param_service.rb +38 -0
  72. data/lib/adwords_api/v201209/ad_param_service_registry.rb +46 -0
  73. data/lib/adwords_api/v201209/alert_service.rb +34 -0
  74. data/lib/adwords_api/v201209/alert_service_registry.rb +46 -0
  75. data/lib/adwords_api/v201209/budget_order_service.rb +42 -0
  76. data/lib/adwords_api/v201209/budget_order_service_registry.rb +46 -0
  77. data/lib/adwords_api/v201209/budget_service.rb +38 -0
  78. data/lib/adwords_api/v201209/budget_service_registry.rb +46 -0
  79. data/lib/adwords_api/v201209/campaign_ad_extension_service.rb +38 -0
  80. data/lib/adwords_api/v201209/campaign_ad_extension_service_registry.rb +46 -0
  81. data/lib/adwords_api/v201209/campaign_criterion_service.rb +42 -0
  82. data/lib/adwords_api/v201209/campaign_criterion_service_registry.rb +46 -0
  83. data/lib/adwords_api/v201209/campaign_service.rb +42 -0
  84. data/lib/adwords_api/v201209/campaign_service_registry.rb +46 -0
  85. data/lib/adwords_api/v201209/constant_data_service.rb +62 -0
  86. data/lib/adwords_api/v201209/constant_data_service_registry.rb +46 -0
  87. data/lib/adwords_api/v201209/conversion_tracker_service.rb +38 -0
  88. data/lib/adwords_api/v201209/conversion_tracker_service_registry.rb +46 -0
  89. data/lib/adwords_api/v201209/customer_service.rb +34 -0
  90. data/lib/adwords_api/v201209/customer_service_registry.rb +46 -0
  91. data/lib/adwords_api/v201209/customer_sync_service.rb +34 -0
  92. data/lib/adwords_api/v201209/customer_sync_service_registry.rb +47 -0
  93. data/lib/adwords_api/v201209/data_service.rb +38 -0
  94. data/lib/adwords_api/v201209/data_service_registry.rb +46 -0
  95. data/lib/adwords_api/v201209/experiment_service.rb +38 -0
  96. data/lib/adwords_api/v201209/experiment_service_registry.rb +46 -0
  97. data/lib/adwords_api/v201209/geo_location_service.rb +34 -0
  98. data/lib/adwords_api/v201209/geo_location_service_registry.rb +46 -0
  99. data/lib/adwords_api/v201209/info_service.rb +34 -0
  100. data/lib/adwords_api/v201209/info_service_registry.rb +46 -0
  101. data/lib/adwords_api/v201209/location_criterion_service.rb +34 -0
  102. data/lib/adwords_api/v201209/location_criterion_service_registry.rb +46 -0
  103. data/lib/adwords_api/v201209/managed_customer_service.rb +38 -0
  104. data/lib/adwords_api/v201209/managed_customer_service_registry.rb +46 -0
  105. data/lib/adwords_api/v201209/media_service.rb +38 -0
  106. data/lib/adwords_api/v201209/media_service_registry.rb +46 -0
  107. data/lib/adwords_api/v201209/mutate_job_service.rb +42 -0
  108. data/lib/adwords_api/v201209/mutate_job_service_registry.rb +46 -0
  109. data/lib/adwords_api/v201209/report_definition_service.rb +34 -0
  110. data/lib/adwords_api/v201209/report_definition_service_registry.rb +46 -0
  111. data/lib/adwords_api/v201209/targeting_idea_service.rb +38 -0
  112. data/lib/adwords_api/v201209/targeting_idea_service_registry.rb +46 -0
  113. data/lib/adwords_api/v201209/traffic_estimator_service.rb +34 -0
  114. data/lib/adwords_api/v201209/traffic_estimator_service_registry.rb +46 -0
  115. data/lib/adwords_api/v201209/user_list_service.rb +38 -0
  116. data/lib/adwords_api/v201209/user_list_service_registry.rb +46 -0
  117. data/lib/adwords_api/version.rb +1 -1
  118. data/test/adwords_api/test_report_utils.rb +94 -6
  119. data/test/examples/v201209/test_account_management.rb +60 -0
  120. data/test/examples/v201209/test_basic_operations.rb +177 -0
  121. data/test/examples/v201209/test_campaign_management.rb +124 -0
  122. data/test/examples/v201209/test_error_handling.rb +63 -0
  123. data/test/examples/v201209/test_misc.rb +45 -0
  124. data/test/examples/v201209/test_optimization.rb +63 -0
  125. data/test/examples/v201209/test_remarketing.rb +39 -0
  126. data/test/examples/v201209/test_reporting.rb +84 -0
  127. data/test/examples/v201209/test_targeting.rb +60 -0
  128. data/test/examples/v201209/utils.rb +238 -0
  129. data/test/suite_exampletests_v201209.rb +37 -0
  130. metadata +123 -2
@@ -0,0 +1,78 @@
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 an Ad Hoc report using AdWords Query Language.
22
+ # See AWQL guide for more details:
23
+ # https://developers.google.com/adwords/api/docs/guides/awql
24
+
25
+ require 'date'
26
+
27
+ require 'adwords_api'
28
+
29
+ def download_criteria_report_with_awql(file_name, report_format)
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
+ # Get report utilities for the version.
39
+ report_utils = adwords.report_utils(API_VERSION)
40
+
41
+ # Prepare a date range for the last week. Instead you can use 'LAST_7_DAYS'.
42
+ date_range = '%s,%s' % [
43
+ DateTime.parse((Date.today - 7).to_s).strftime('%Y%m%d'),
44
+ DateTime.parse((Date.today - 1).to_s).strftime('%Y%m%d')
45
+ ]
46
+
47
+ # Define report definition. You can also pass your own XML text as a string.
48
+ report_query = 'SELECT CampaignId, AdGroupId, Id, Criteria, CriteriaType, ' +
49
+ 'Impressions, Clicks, Cost FROM CRITERIA_PERFORMANCE_REPORT ' +
50
+ 'WHERE Status IN [ACTIVE, PAUSED] ' +
51
+ 'DURING %s' % date_range
52
+
53
+ # Download report, using "download_report_as_file_with_awql" utility method.
54
+ # To retrieve the report as return value, use "download_report_with_awql"
55
+ # method.
56
+ report_utils.download_report_as_file_with_awql(report_query, report_format,
57
+ file_name)
58
+ puts "Report was downloaded to '%s'." % file_name
59
+ end
60
+
61
+ if __FILE__ == $0
62
+ API_VERSION = :v201209
63
+
64
+ begin
65
+ # File name to write report to.
66
+ file_name = 'INSERT_OUTPUT_FILE_NAME_HERE'
67
+ report_format = 'CSV'
68
+ download_criteria_report_with_awql(file_name, report_format)
69
+
70
+ # HTTP errors.
71
+ rescue AdsCommon::Errors::HttpError => e
72
+ puts 'HTTP Error: %s' % e
73
+
74
+ # API errors.
75
+ rescue AdwordsApi::Errors::ReportError => e
76
+ puts 'Reporting Error: %s' % e.message
77
+ end
78
+ 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 = :v201209
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
@@ -0,0 +1,71 @@
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 = :v201209
51
+
52
+ begin
53
+ report_type = 'INSERT_REPORT_TYPE_HERE'
54
+ get_report_fields(report_type)
55
+
56
+ # HTTP errors.
57
+ rescue AdsCommon::Errors::HttpError => e
58
+ puts "HTTP Error: %s" % e
59
+
60
+ # API errors.
61
+ rescue AdwordsApi::Errors::ApiException => e
62
+ puts "Message: %s" % e.message
63
+ puts 'Errors:'
64
+ e.errors.each_with_index do |error, index|
65
+ puts "\tError [%d]:" % (index + 1)
66
+ error.each do |field, value|
67
+ puts "\t\t%s: %s" % [field, value]
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,160 @@
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
+ require 'thread'
25
+
26
+ require 'adwords_api'
27
+ require 'adwords_api/utils'
28
+
29
+ def parallel_report_download()
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
+ # Determine list of customer IDs to retrieve report for. For this example we
39
+ # will use ManagedCustomerService to get all IDs in hierarchy.
40
+
41
+ managed_customer_srv = adwords.service(:ManagedCustomerService, API_VERSION)
42
+
43
+ # Get the account hierarchy for this account.
44
+ selector = {:fields => ['CustomerId']}
45
+
46
+ # Run the request at the MCC level.
47
+ graph = adwords.use_mcc() {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', 'Id', 'Impressions', 'Clicks', 'Cost'],
65
+ # Predicates are optional.
66
+ :predicates => {
67
+ :field => 'Status',
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 = :v201209
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
+ # HTTP errors.
153
+ rescue AdsCommon::Errors::HttpError => e
154
+ puts 'HTTP Error: %s' % e
155
+
156
+ # API errors.
157
+ rescue AdwordsApi::Errors::ReportError => e
158
+ puts 'Reporting Error: %s' % e.message
159
+ end
160
+ end
@@ -0,0 +1,118 @@
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 adds various types of targeting criteria to a campaign. To get
22
+ # campaigns list, run get_campaigns.rb.
23
+ #
24
+ # Tags: CampaignCriterionService.mutate
25
+
26
+ require 'adwords_api'
27
+
28
+ def add_campaign_targeting_criteria(campaign_id)
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
+ campaign_criterion_srv =
38
+ adwords.service(:CampaignCriterionService, API_VERSION)
39
+
40
+ # Create campaign criteria.
41
+ campaign_criteria = [
42
+ # Location criteria. The IDs can be found in the documentation or retrieved
43
+ # with the LocationCriterionService.
44
+ {:xsi_type => 'Location', :id => 21137}, # California, USA
45
+ {:xsi_type => 'Location', :id => 2484}, # Mexico
46
+ # Language criteria. The IDs can be found in the documentation or retrieved
47
+ # with the ConstantDataService.
48
+ {:xsi_type => 'Language', :id => 1000}, # English
49
+ {:xsi_type => 'Language', :id => 1003}, # Spanish
50
+ # Platform criteria. The IDs can be found in the documentation.
51
+ {:xsi_type => 'Platform', :id => 30001}, # Mobile
52
+ {:xsi_type => 'Platform', :id => 30002} # Tablets
53
+ ]
54
+
55
+ # Create operations.
56
+ operations = campaign_criteria.map do |criterion|
57
+ {:operator => 'ADD',
58
+ :operand => {
59
+ :campaign_id => campaign_id,
60
+ :criterion => criterion}
61
+ }
62
+ end
63
+
64
+ # Add negative campaign criterion.
65
+ operations << {
66
+ :operator => 'ADD',
67
+ :operand => {
68
+ # The 'xsi_type' field allows you to specify the xsi:type of the object
69
+ # being created. It's only necessary when you must provide an explicit
70
+ # type that the client library can't infer.
71
+ :xsi_type => 'NegativeCampaignCriterion',
72
+ :campaign_id => campaign_id,
73
+ :criterion => {
74
+ :xsi_type => 'Keyword',
75
+ :text => 'jupiter cruise',
76
+ :match_type => 'BROAD'
77
+ }
78
+ }
79
+ }
80
+
81
+ response = campaign_criterion_srv.mutate(operations)
82
+
83
+ if response and response[:value]
84
+ criteria = response[:value]
85
+ criteria.each do |campaign_criterion|
86
+ criterion = campaign_criterion[:criterion]
87
+ puts ("Campaign criterion with campaign ID %d, criterion ID %d and " +
88
+ "type '%s' was added.") % [campaign_criterion[:campaign_id],
89
+ criterion[:id], criterion[:criterion_type]]
90
+ end
91
+ else
92
+ puts 'No criteria were returned.'
93
+ end
94
+ end
95
+
96
+ if __FILE__ == $0
97
+ API_VERSION = :v201209
98
+
99
+ begin
100
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'
101
+ add_campaign_targeting_criteria(campaign_id)
102
+
103
+ # HTTP errors.
104
+ rescue AdsCommon::Errors::HttpError => e
105
+ puts "HTTP Error: %s" % e
106
+
107
+ # API errors.
108
+ rescue AdwordsApi::Errors::ApiException => e
109
+ puts "Message: %s" % e.message
110
+ puts 'Errors:'
111
+ e.errors.each_with_index do |error, index|
112
+ puts "\tError [%d]:" % (index + 1)
113
+ error.each do |field, value|
114
+ puts "\t\t%s: %s" % [field, value]
115
+ end
116
+ end
117
+ end
118
+ end