google-adwords-api 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/ChangeLog +3 -0
  2. data/README +33 -22
  3. data/adwords_api.yml +5 -5
  4. data/examples/v201302/adwords_for_video/add_video_call_to_action.rb +94 -0
  5. data/examples/v201302/adwords_for_video/add_video_campaign.rb +103 -0
  6. data/examples/v201302/adwords_for_video/find_videos.rb +92 -0
  7. data/examples/v201302/adwords_for_video/get_keyword_criteria.rb +101 -0
  8. data/examples/v201302/adwords_for_video/get_targeting_groups.rb +93 -0
  9. data/examples/v201302/adwords_for_video/get_video_campaign_criteria.rb +99 -0
  10. data/examples/v201302/adwords_for_video/get_video_campaign_stats.rb +126 -0
  11. data/examples/v201302/adwords_for_video/get_video_campaigns.rb +94 -0
  12. data/lib/adwords_api.rb +5 -5
  13. data/lib/adwords_api/api_config.rb +16 -2
  14. data/lib/adwords_api/v201302/video_ad_service.rb +38 -0
  15. data/lib/adwords_api/v201302/video_ad_service_registry.rb +46 -0
  16. data/lib/adwords_api/v201302/video_campaign_criterion_service.rb +38 -0
  17. data/lib/adwords_api/v201302/video_campaign_criterion_service_registry.rb +47 -0
  18. data/lib/adwords_api/v201302/video_campaign_service.rb +38 -0
  19. data/lib/adwords_api/v201302/video_campaign_service_registry.rb +46 -0
  20. data/lib/adwords_api/v201302/video_service.rb +42 -0
  21. data/lib/adwords_api/v201302/video_service_registry.rb +46 -0
  22. data/lib/adwords_api/v201302/video_targeting_group_criterion_service.rb +38 -0
  23. data/lib/adwords_api/v201302/video_targeting_group_criterion_service_registry.rb +46 -0
  24. data/lib/adwords_api/v201302/video_targeting_group_service.rb +38 -0
  25. data/lib/adwords_api/v201302/video_targeting_group_service_registry.rb +46 -0
  26. data/lib/adwords_api/version.rb +1 -1
  27. data/test/adwords_api/test_adwords_api.rb +23 -0
  28. data/test/templates/v201209/basic_operations_get_campaigns.def +1 -1
  29. data/test/templates/v201302/basic_operations_get_campaigns.def +1 -1
  30. data/test/templates/v201302/misc_use_oauth2_jwt.def +1 -1
  31. metadata +22 -2
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2013, 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 show how to retrieve all targeting groups for an account.
22
+ #
23
+ # Note: AdWords for Video API is a Beta feature.
24
+ #
25
+ # Tags: VideoTargetingGroupService.get
26
+
27
+ require 'adwords_api'
28
+
29
+ def get_targeting_groups()
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
+ video_targeting_group_srv =
39
+ adwords.service(:VideoTargetingGroupService, API_VERSION)
40
+
41
+ # Get all targeting groups for this account.
42
+ selector = {
43
+ :paging => {
44
+ :start_index => 0,
45
+ :number_results => PAGE_SIZE
46
+ }
47
+ }
48
+
49
+ # Set initial values.
50
+ offset, page = 0, {}
51
+
52
+ begin
53
+ page = video_targeting_group_srv.get(selector)
54
+ if page[:entries]
55
+ page[:entries].each do |group|
56
+ puts "Targeting group ID %d, campaign ID %d and name '%s'" %
57
+ [group[:id], group[:campaign_id], group[:name]]
58
+ end
59
+ # Increment values to request the next page.
60
+ offset += PAGE_SIZE
61
+ selector[:paging][:start_index] = offset
62
+ end
63
+ end while page[:total_num_entries] > offset
64
+
65
+ if page.include?(:total_num_entries)
66
+ puts "\tTotal number of targeting groups found: %d." %
67
+ [page[:total_num_entries]]
68
+ end
69
+ end
70
+
71
+ if __FILE__ == $0
72
+ API_VERSION = :v201302
73
+ PAGE_SIZE = 500
74
+
75
+ begin
76
+ get_targeting_groups()
77
+
78
+ # HTTP errors.
79
+ rescue AdsCommon::Errors::HttpError => e
80
+ puts "HTTP Error: %s" % e
81
+
82
+ # API errors.
83
+ rescue AdwordsApi::Errors::ApiException => e
84
+ puts "Message: %s" % e.message
85
+ puts 'Errors:'
86
+ e.errors.each_with_index do |error, index|
87
+ puts "\tError [%d]:" % (index + 1)
88
+ error.each do |field, value|
89
+ puts "\t\t%s: %s" % [field, value]
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2013, 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 demonstrates how to retrieve all campaign-level criteria.
22
+ #
23
+ # Note: AdWords for Video API is a Beta feature.
24
+ #
25
+ # Tags: VideoCampaignCriterionService.get
26
+
27
+ require 'adwords_api'
28
+
29
+ def get_video_campaign_criteria(campaign_id)
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
+ vtgc_srv =
39
+ adwords.service(:VideoCampaignCriterionService, API_VERSION)
40
+
41
+ # Get all criteria for the campaign.
42
+ selector = {
43
+ :campaign_ids => [campaign_id],
44
+ :paging => {
45
+ :start_index => 0,
46
+ :number_results => PAGE_SIZE
47
+ }
48
+ }
49
+
50
+ # Set initial values.
51
+ offset, page = 0, {}
52
+
53
+ begin
54
+ page = vtgc_srv.get(selector)
55
+ if page[:entries]
56
+ page[:entries].each do |criterion|
57
+ negative_str =
58
+ (criterion[:xsi_type] == 'NegativeVideoCampaignCriterion') ?
59
+ ' negative' : ''
60
+ puts "Video%s criterion ID %d or type '%s'" % [negative_str,
61
+ criterion[:criterion][:id], criterion[:criterion][:xsi_type]]
62
+ end
63
+ # Increment values to request the next page.
64
+ offset += PAGE_SIZE
65
+ selector[:paging][:start_index] = offset
66
+ end
67
+ end while page[:total_num_entries] > offset
68
+
69
+ if page.include?(:total_num_entries)
70
+ puts "\tTotal number of criteria found: %d." % [page[:total_num_entries]]
71
+ end
72
+ end
73
+
74
+ if __FILE__ == $0
75
+ API_VERSION = :v201302
76
+ PAGE_SIZE = 500
77
+
78
+ begin
79
+ # Campaign ID to get criteria for.
80
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
81
+
82
+ get_video_campaign_criteria(campaign_id)
83
+
84
+ # HTTP errors.
85
+ rescue AdsCommon::Errors::HttpError => e
86
+ puts "HTTP Error: %s" % e
87
+
88
+ # API errors.
89
+ rescue AdwordsApi::Errors::ApiException => e
90
+ puts "Message: %s" % e.message
91
+ puts 'Errors:'
92
+ e.errors.each_with_index do |error, index|
93
+ puts "\tError [%d]:" % (index + 1)
94
+ error.each do |field, value|
95
+ puts "\t\t%s: %s" % [field, value]
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2013, 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 illustrates how to retrieve stats for a video campaign.
22
+ #
23
+ # Note: AdWords for Video API is a Beta feature.
24
+ #
25
+ # Tags: VideoCampaignService.get
26
+
27
+ require 'adwords_api'
28
+ require 'date'
29
+
30
+ def get_campaign_stats(campaign_id)
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
+ video_campaign_srv = adwords.service(:VideoCampaignService, API_VERSION)
40
+
41
+ # Preparing dates to get stats for this year.
42
+ today = Date.today
43
+ min_date_time = today.strftime("%Y0101")
44
+ max_date_time = today.strftime("%Y%m%d")
45
+
46
+ # Get stats for the campaign for given dates.
47
+ selector = {
48
+ :ids => [campaign_id],
49
+ :stats_selector => {
50
+ :date_range => {:min => min_date_time, :max => max_date_time},
51
+ :segmentation_dimensions => ['DATE_MONTH'],
52
+ :metrics => ['VIEWS', 'COST', 'AVERAGE_CPV'],
53
+ :summary_types => ['ALL'],
54
+ :segmented_summary_type => 'ALL'
55
+ },
56
+ :paging => {
57
+ :start_index => 0,
58
+ :number_results => PAGE_SIZE
59
+ }
60
+ }
61
+
62
+ page = video_campaign_srv.get(selector)
63
+ if page[:entries]
64
+ campaign = page[:entries].first
65
+ puts "Campaign ID %d, name '%s' and status '%s'" %
66
+ [campaign[:id], campaign[:name], campaign[:status]]
67
+ if campaign[:stats]
68
+ puts "\tCampaign stats:"
69
+ puts "\t\t" + format_stats(campaign[:stats])
70
+ end
71
+ if campaign[:segmented_stats]
72
+ campaign[:segmented_stats].each do |stats|
73
+ segment_key_str = stats[:segment_key][:date_key][:date]
74
+ puts "\tCampaign segmented stat for month of %s" % segment_key_str
75
+ puts "\t\t" + format_stats(stats)
76
+ end
77
+ end
78
+ end
79
+ if page[:summary_stats]
80
+ page[:summary_stats].each do |stats|
81
+ puts "\tSummary of type %s" % stats[:summary_type]
82
+ puts "\t\t" + format_stats(stats)
83
+ end
84
+ end
85
+ end
86
+
87
+ def format_stats(stats)
88
+ return ("Views: %s, Cost: %s, Avg. CPC: %s, Avg. CPV: %s, " +
89
+ "Avg. CPM: %s, 25%%: %s, 50%%: %s, 75%%: %s, 100%%: %s") % [
90
+ stats[:views].nil? ? '--' : stats[:views],
91
+ stats[:cost].nil? ? '--' : stats[:cost][:micro_amount],
92
+ stats[:average_cpc].nil? ? '--' : stats[:average_cpc][:micro_amount],
93
+ stats[:average_cpv].nil? ? '--' : stats[:average_cpv][:micro_amount],
94
+ stats[:average_cpm].nil? ? '--' : stats[:average_cpm][:micro_amount],
95
+ stats[:quartile25_percents].nil? ? '--' : stats[:quartile25_percents],
96
+ stats[:quartile50_percents].nil? ? '--' : stats[:quartile50_percents],
97
+ stats[:quartile75_percents].nil? ? '--' : stats[:quartile75_percents],
98
+ stats[:quartile100_percents].nil? ? '--' : stats[:quartile100_percents]
99
+ ]
100
+ end
101
+
102
+ if __FILE__ == $0
103
+ API_VERSION = :v201302
104
+ PAGE_SIZE = 500
105
+
106
+ begin
107
+ # Campaign ID to get stats for.
108
+ campaign_id = 'INSERT_CAMPAIGN_ID_HERE'.to_i
109
+ get_campaign_stats(campaign_id)
110
+
111
+ # HTTP errors.
112
+ rescue AdsCommon::Errors::HttpError => e
113
+ puts 'HTTP Error: %s' % e
114
+
115
+ # API errors.
116
+ rescue AdwordsApi::Errors::ApiException => e
117
+ puts 'Message: %s' % e.message
118
+ puts 'Errors:'
119
+ e.errors.each_with_index do |error, index|
120
+ puts "\tError [%d]:" % (index + 1)
121
+ error.each do |field, value|
122
+ puts "\t\t%s: %s" % [field, value]
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2013, 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 illustrates how to retrieve all non-deleted video campaigns for
22
+ # an account.
23
+ #
24
+ # Note: AdWords for Video API is a Beta feature.
25
+ #
26
+ # Tags: VideoCampaignService.get
27
+
28
+ require 'adwords_api'
29
+ require 'date'
30
+
31
+ def get_video_campaigns()
32
+ # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
33
+ # when called without parameters.
34
+ adwords = AdwordsApi::Api.new
35
+
36
+ # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
37
+ # the configuration file or provide your own logger:
38
+ # adwords.logger = Logger.new('adwords_xml.log')
39
+
40
+ video_campaign_srv = adwords.service(:VideoCampaignService, API_VERSION)
41
+
42
+ # Get all active and paused campaigns for this account.
43
+ selector = {
44
+ :campaign_statuses => ['ACTIVE', 'PAUSED'],
45
+ :paging => {
46
+ :start_index => 0,
47
+ :number_results => PAGE_SIZE
48
+ }
49
+ }
50
+
51
+ # Set initial values.
52
+ offset, page = 0, {}
53
+
54
+ begin
55
+ page = video_campaign_srv.get(selector)
56
+ if page[:entries]
57
+ page[:entries].each do |campaign|
58
+ puts "Campaign ID %d, name '%s' and status '%s'" %
59
+ [campaign[:id], campaign[:name], campaign[:status]]
60
+ end
61
+ # Increment values to request the next page.
62
+ offset += PAGE_SIZE
63
+ selector[:paging][:start_index] = offset
64
+ end
65
+ end while page[:total_num_entries] > offset
66
+
67
+ if page.include?(:total_num_entries)
68
+ puts "\tTotal number of campaigns found: %d." % [page[:total_num_entries]]
69
+ end
70
+ end
71
+
72
+ if __FILE__ == $0
73
+ API_VERSION = :v201302
74
+ PAGE_SIZE = 500
75
+
76
+ begin
77
+ get_video_campaigns()
78
+
79
+ # HTTP errors.
80
+ rescue AdsCommon::Errors::HttpError => e
81
+ puts 'HTTP Error: %s' % e
82
+
83
+ # API errors.
84
+ rescue AdwordsApi::Errors::ApiException => e
85
+ puts 'Message: %s' % e.message
86
+ puts 'Errors:'
87
+ e.errors.each_with_index do |error, index|
88
+ puts "\tError [%d]:" % (index + 1)
89
+ error.each do |field, value|
90
+ puts "\t\t%s: %s" % [field, value]
91
+ end
92
+ end
93
+ end
94
+ end
data/lib/adwords_api.rb CHANGED
@@ -61,15 +61,15 @@ module AdwordsApi
61
61
  # - SOAP header handler
62
62
  #
63
63
  def soap_header_handler(auth_handler, version, header_ns, default_ns)
64
- auth_method = @config.read('authentication.method', :CLIENTLOGIN)
64
+ auth_method = @config.read('authentication.method', :OAUTH2)
65
65
  handler_class = case auth_method
66
66
  when :CLIENTLOGIN
67
- AdwordsApi::ClientLoginHeaderHandler
67
+ AdwordsApi::ClientLoginHeaderHandler
68
68
  when :OAUTH2, :OAUTH2_JWT
69
- AdsCommon::SavonHeaders::OAuthHeaderHandler
69
+ AdsCommon::SavonHeaders::OAuthHeaderHandler
70
70
  else
71
- raise AdsCommon::Errors::AuthError,
72
- "Unknown auth method: %s" % auth_method
71
+ raise AdsCommon::Errors::AuthError,
72
+ "Unknown auth method: %s" % auth_method
73
73
  end
74
74
  return handler_class.new(@credential_handler, auth_handler, header_ns,
75
75
  default_ns, version)
@@ -117,7 +117,14 @@ module AdwordsApi
117
117
  :SharedSetService,
118
118
  :TargetingIdeaService,
119
119
  :TrafficEstimatorService,
120
- :UserListService
120
+ :UserListService,
121
+ # AdWords for Video API Beta services.
122
+ :VideoAdService,
123
+ :VideoCampaignCriterionService,
124
+ :VideoCampaignService,
125
+ :VideoService,
126
+ :VideoTargetingGroupCriterionService,
127
+ :VideoTargetingGroupService
121
128
  ]
122
129
  }
123
130
 
@@ -205,7 +212,14 @@ module AdwordsApi
205
212
  [:v201302, :UserListService] => 'cm/',
206
213
  [:v201302, :ManagedCustomerService] => 'mcm/',
207
214
  [:v201302, :CustomerService] => 'mcm/',
208
- [:v201302, :BudgetService] => 'cm/'
215
+ [:v201302, :BudgetService] => 'cm/',
216
+ # AdWords for Video API Beta services.
217
+ [:v201302, :VideoAdService] => 'video/',
218
+ [:v201302, :VideoCampaignCriterionService] => 'video/',
219
+ [:v201302, :VideoCampaignService] => 'video/',
220
+ [:v201302, :VideoService] => 'video/',
221
+ [:v201302, :VideoTargetingGroupCriterionService] => 'video/',
222
+ [:v201302, :VideoTargetingGroupService] => 'video/'
209
223
  }
210
224
 
211
225
  # Auth constants for ClientLogin method.