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,6 +1,7 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
2
3
  #
3
- # Author:: api.sgomes@gmail.com (Sérgio Gomes)
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
4
5
  #
5
6
  # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
6
7
  #
@@ -17,17 +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 the campaigns for an account.
21
+ # This example illustrates how to use OAuth authentication method.
21
22
  #
22
23
  # Tags: CampaignService.get
23
24
 
24
- require 'rubygems'
25
- gem 'google-adwords-api'
26
25
  require 'adwords_api'
27
26
 
28
- API_VERSION = :v201109
29
-
30
- def get_all_campaigns()
27
+ def use_oauth()
31
28
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
32
29
  # when called without parameters.
33
30
  adwords = AdwordsApi::Api.new
@@ -41,45 +38,59 @@ def get_all_campaigns()
41
38
  # Get all the campaigns for this account; empty selector.
42
39
  selector = {
43
40
  :fields => ['Id', 'Name', 'Status'],
44
- :ordering => [{:field => 'Name', :sort_order => 'ASCENDING'}]
41
+ :ordering => [
42
+ {:field => 'Name', :sort_order => 'ASCENDING'}
43
+ ]
45
44
  }
46
- response = campaign_srv.get(selector)
45
+
46
+ retry_count = 0
47
+
48
+ begin
49
+ response = campaign_srv.get(selector)
50
+ rescue AdsCommon::Errors::OAuthVerificationRequired => e
51
+ if retry_count < MAX_RETRIES
52
+ puts "Hit Auth error, please navigate to URL:\n\t%s" % e.oauth_url
53
+ print 'log in and type the verification code: '
54
+ verification_code = gets.chomp
55
+ adwords.credential_handler.set_credential(
56
+ :oauth_verification_code, verification_code)
57
+ retry_count += 1
58
+ retry
59
+ else
60
+ raise AdsCommon::Errors::AuthError, 'Failed to authenticate.'
61
+ end
62
+ end
47
63
 
48
64
  if response and response[:entries]
49
65
  campaigns = response[:entries]
50
66
  campaigns.each do |campaign|
51
- puts "Campaign name is \"#{campaign[:name]}\", id is #{campaign[:id]} " +
52
- "and status is \"#{campaign[:status]}\"."
67
+ puts "Campaign ID %d, name '%s' and status '%s'" %
68
+ [campaign[:id], campaign[:name], campaign[:status]]
53
69
  end
54
70
  else
55
- puts "No campaigns were found."
71
+ puts 'No campaigns were found.'
56
72
  end
57
73
  end
58
74
 
59
75
  if __FILE__ == $0
76
+ API_VERSION = :v201109
77
+ MAX_RETRIES = 3
78
+
60
79
  begin
61
- get_all_campaigns()
80
+ use_oauth()
62
81
 
63
- # Connection error. Likely transitory.
64
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
65
- puts 'Connection Error: %s' % e
66
- puts 'Source: %s' % e.backtrace.first
82
+ # HTTP errors.
83
+ rescue AdsCommon::Errors::HttpError => e
84
+ puts "HTTP Error: %s" % e
67
85
 
68
- # API Error.
86
+ # API errors.
69
87
  rescue AdwordsApi::Errors::ApiException => e
70
- puts 'API Exception caught.'
71
- puts 'Message: %s' % e.message
72
- puts 'Code: %d' % e.code if e.code
73
- puts 'Trigger: %s' % e.trigger if e.trigger
88
+ puts "Message: %s" % e.message
74
89
  puts 'Errors:'
75
- if e.errors
76
- e.errors.each_with_index do |error, index|
77
- puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
78
- error.each_pair do |field, value|
79
- if field != :xsi_type
80
- puts ' %s: %s' % [field, value]
81
- end
82
- end
90
+ e.errors.each_with_index do |error, index|
91
+ puts "\tError [%d]:" % (index + 1)
92
+ error.each do |field, value|
93
+ puts "\t\t%s: %s" % [field, value]
83
94
  end
84
95
  end
85
96
  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
  #
@@ -22,13 +23,10 @@
22
23
  #
23
24
  # Tags: TargetingIdeaService.get
24
25
 
25
- require 'rubygems'
26
- gem 'google-adwords-api'
27
26
  require 'adwords_api'
27
+ require 'adwords_api/utils'
28
28
 
29
- API_VERSION = :v201003
30
-
31
- def get_related_placements()
29
+ def get_placement_ideas(url)
32
30
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
33
31
  # when called without parameters.
34
32
  adwords = AdwordsApi::Api.new
@@ -39,14 +37,11 @@ def get_related_placements()
39
37
 
40
38
  targeting_idea_srv = adwords.service(:TargetingIdeaService, API_VERSION)
41
39
 
42
- results_per_page = 10
43
- url = 'mars.google.com'
44
-
45
40
  # Construct selector.
46
41
  selector = {
47
42
  :idea_type => 'PLACEMENT',
48
43
  :request_type => 'IDEAS',
49
- :requested_attribute_types => ['PLACEMENT'],
44
+ :requested_attribute_types => ['CRITERION'],
50
45
  :search_parameters => [{
51
46
  # The 'xsi_type' field allows you to specify the xsi:type of the object
52
47
  # being created. It's only necessary when you must provide an explicit
@@ -57,54 +52,54 @@ def get_related_placements()
57
52
  }],
58
53
  :paging => {
59
54
  :start_index => 0,
60
- :number_results => results_per_page
55
+ :number_results => PAGE_SIZE
61
56
  }
62
57
  }
63
58
 
59
+ # Define initial values.
60
+ offset = 0
64
61
  results = []
65
- index = 0
62
+
66
63
  begin
67
- selector[:paging][:start_index] = index
68
64
  # Perform request.
69
65
  page = targeting_idea_srv.get(selector)
70
66
  results += page[:entries] if page and page[:entries]
71
- index += results_per_page
72
- end while index < page[:total_num_entries]
67
+
68
+ # Prepare next page request.
69
+ offset += PAGE_SIZE
70
+ selector[:paging][:start_index] = offset
71
+ end while offset < page[:total_num_entries]
73
72
 
74
73
  # Display results.
75
74
  results.each do |result|
76
75
  data = AdwordsApi::Utils.map(result[:data])
77
- placement = data['PLACEMENT'][:value]
78
- puts "Related content keywords found at URL \"%s\"" % placement[:url]
76
+ placement = data['CRITERION'][:value]
77
+ puts "Related placement found at URL [%s]" % placement[:url]
79
78
  end
80
- puts "Total urls found with content keywords related to keywords at " +
81
- " \"#{url}\": #{results.length}."
79
+ puts "Total URLs found with keywords related to keywords at [%s]: %d." %
80
+ [url, results.length]
82
81
  end
83
82
 
84
83
  if __FILE__ == $0
84
+ API_VERSION = :v201109
85
+ PAGE_SIZE = 100
86
+
85
87
  begin
86
- get_related_placements()
88
+ url = 'INSERT_PLACEMENT_URL_HERE'
89
+ get_placement_ideas(url)
87
90
 
88
- # Connection error. Likely transitory.
89
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
90
- puts 'Connection Error: %s' % e
91
- puts 'Source: %s' % e.backtrace.first
91
+ # HTTP errors.
92
+ rescue AdsCommon::Errors::HttpError => e
93
+ puts "HTTP Error: %s" % e
92
94
 
93
- # API Error.
95
+ # API errors.
94
96
  rescue AdwordsApi::Errors::ApiException => e
95
- puts 'API Exception caught.'
96
- puts 'Message: %s' % e.message
97
- puts 'Code: %d' % e.code if e.code
98
- puts 'Trigger: %s' % e.trigger if e.trigger
97
+ puts "Message: %s" % e.message
99
98
  puts 'Errors:'
100
- if e.errors
101
- e.errors.each_with_index do |error, index|
102
- puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
103
- error.each_pair do |field, value|
104
- if field != :xsi_type
105
- puts ' %s: %s' % [field, value]
106
- end
107
- end
99
+ e.errors.each_with_index do |error, index|
100
+ puts "\tError [%d]:" % (index + 1)
101
+ error.each do |field, value|
102
+ puts "\t\t%s: %s" % [field, value]
108
103
  end
109
104
  end
110
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,17 +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 illustrates how to create a user list.
21
+ # This example illustrates how to create a user list (a.k.a. Audience) and shows
22
+ # its associated conversion tracker code snippet.
21
23
  #
22
- # Tags: UserListService.mutate
24
+ # Tags: UserListService.mutate, ConversionTrackerService.get
23
25
 
24
- require 'rubygems'
25
- gem 'google-adwords-api'
26
26
  require 'adwords_api'
27
27
 
28
- API_VERSION = :v201008
29
-
30
- def add_user_list()
28
+ def add_audience()
31
29
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
32
30
  # when called without parameters.
33
31
  adwords = AdwordsApi::Api.new
@@ -37,9 +35,10 @@ def add_user_list()
37
35
  # adwords.logger = Logger.new('adwords_xml.log')
38
36
 
39
37
  user_list_srv = adwords.service(:UserListService, API_VERSION)
38
+ conv_tracker_srv = adwords.service(:ConversionTrackerService, API_VERSION)
40
39
 
41
40
  # Prepare for adding remarketing user list.
42
- name = 'Mars cruise customers #%s' % (Time.new.to_f * 1000).to_i
41
+ name = "Mars cruise customers #%d" % (Time.new.to_f * 1000).to_i
43
42
  operation = {
44
43
  :operator => 'ADD',
45
44
  :operand => {
@@ -51,45 +50,61 @@ def add_user_list()
51
50
  :description => 'A list of mars cruise customers in the last year',
52
51
  :status => 'OPEN',
53
52
  :membership_life_span => 365,
54
- :conversion_types => [{:name => name}]
53
+ :conversion_types => [{:name => name}],
54
+ # Optional field.
55
+ :status => 'OPEN'
55
56
  }
56
57
  }
57
58
 
58
59
  # Add user list.
59
60
  response = user_list_srv.mutate([operation])
60
- if response and response[:value]
61
- user_list = response[:value].first
62
- puts 'User list with name "%s" and id %d was added.' %
63
- [user_list[:name], user_list[:id]]
64
- else
65
- puts 'No user lists were added.'
61
+ user_list = response[:value].first
62
+
63
+ # Get conversion snippets.
64
+ if user_list and user_list[:conversion_types]
65
+ conversion_ids = user_list[:conversion_types].map {|type| type[:id]}
66
+ selector = {
67
+ # We're actually interested in the 'Snippet' field, which is returned
68
+ # automatically.
69
+ :fields => ['Id'],
70
+ :predicates => [
71
+ {:field => 'Id', :operator => 'IN', :values => conversion_ids}
72
+ ]
73
+ }
74
+ conv_tracker_response = conv_tracker_srv.get(selector)
75
+ if conv_tracker_response and conv_tracker_response[:entries]
76
+ conversions = conv_tracker_response[:entries]
77
+ end
78
+ end
79
+ puts "User list with name '%s' and ID %d was added." %
80
+ [user_list[:name], user_list[:id]]
81
+ # Display user list associated conversion code snippets.
82
+ if conversions
83
+ conversions.each do |conversion|
84
+ puts "Conversion type code snipped associated to the list:\n\t\t%s\n" %
85
+ conversion[:snippet]
86
+ end
66
87
  end
67
88
  end
68
89
 
69
90
  if __FILE__ == $0
91
+ API_VERSION = :v201109
92
+
70
93
  begin
71
- add_user_list()
94
+ add_audience()
72
95
 
73
- # Connection error. Likely transitory.
74
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
75
- puts 'Connection Error: %s' % e
76
- puts 'Source: %s' % e.backtrace.first
96
+ # HTTP errors.
97
+ rescue AdsCommon::Errors::HttpError => e
98
+ puts "HTTP Error: %s" % e
77
99
 
78
- # API Error.
100
+ # API errors.
79
101
  rescue AdwordsApi::Errors::ApiException => e
80
- puts 'API Exception caught.'
81
- puts 'Message: %s' % e.message
82
- puts 'Code: %d' % e.code if e.code
83
- puts 'Trigger: %s' % e.trigger if e.trigger
102
+ puts "Message: %s" % e.message
84
103
  puts 'Errors:'
85
- if e.errors
86
- e.errors.each_with_index do |error, index|
87
- puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
88
- error.each_pair do |field, value|
89
- if field != :xsi_type
90
- puts ' %s: %s' % [field, value]
91
- end
92
- end
104
+ e.errors.each_with_index do |error, index|
105
+ puts "\tError [%d]:" % (index + 1)
106
+ error.each do |field, value|
107
+ puts "\t\t%s: %s" % [field, value]
93
108
  end
94
109
  end
95
110
  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,17 +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 add an AdWords conversion.
21
+ # This example illustrates how to add an AdWords conversion tracker.
21
22
  #
22
23
  # Tags: ConversionTrackerService.mutate
23
24
 
24
- require 'rubygems'
25
- gem 'google-adwords-api'
26
25
  require 'adwords_api'
27
26
 
28
- API_VERSION = :v201109
29
-
30
- def add_conversion()
27
+ def add_conversion_tracker()
31
28
  # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
32
29
  # when called without parameters.
33
30
  adwords = AdwordsApi::Api.new
@@ -46,51 +43,50 @@ def add_conversion()
46
43
  # being created. It's only necessary when you must provide an explicit
47
44
  # type that the client library can't infer.
48
45
  :xsi_type => 'AdWordsConversionTracker',
49
- :name => 'Earth to Mars Cruises Conversion #%s' %
46
+ :name => "Earth to Mars Cruises Conversion #%d" %
50
47
  (Time.new.to_f * 1000).to_i,
51
48
  :category => 'DEFAULT',
52
49
  :markup_language => 'HTML',
53
50
  :http_protocol => 'HTTP',
54
- :text_format => 'HIDDEN'
51
+ :text_format => 'HIDDEN',
52
+ # Optional fields:
53
+ :status => 'ENABLED',
54
+ :viewthrough_lookback_window => 15,
55
+ :viewthrough_conversion_de_dup_search => true,
56
+ :is_product_ads_chargeable => true,
57
+ :product_ads_chargeable_conversion_window => 15,
58
+ :conversion_page_language => 'en',
59
+ :background_color => '#0000FF',
60
+ :user_revenue_value => 'someJavascriptVariable'
55
61
  }
56
62
  }
57
63
 
58
64
  # Add conversion.
59
65
  response = conv_tracker_srv.mutate([operation])
60
- if response and response[:value]
61
- conversion = response[:value].first
62
- puts "Conversion with id #{conversion[:id]}, name " +
63
- "\"#{conversion[:name]}\", status \"#{conversion[:status]}\" and " +
64
- " category \"#{conversion[:category]}\" was added."
65
- else
66
- puts 'No conversions were added.'
67
- end
66
+ conversion = response[:value].first
67
+ puts ("Conversion with ID %d, name '%s', status '%s' and category '%s'" +
68
+ " was added.") % [conversion[:id], conversion[:name],
69
+ conversion[:status], conversion[:category]]
68
70
  end
69
71
 
70
72
  if __FILE__ == $0
73
+ API_VERSION = :v201109
74
+
71
75
  begin
72
- add_conversion()
76
+ add_conversion_tracker()
73
77
 
74
- # Connection error. Likely transitory.
75
- rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
76
- puts 'Connection Error: %s' % e
77
- puts 'Source: %s' % e.backtrace.first
78
+ # HTTP errors.
79
+ rescue AdsCommon::Errors::HttpError => e
80
+ puts "HTTP Error: %s" % e
78
81
 
79
- # API Error.
82
+ # API errors.
80
83
  rescue AdwordsApi::Errors::ApiException => e
81
- puts 'API Exception caught.'
82
- puts 'Message: %s' % e.message
83
- puts 'Code: %d' % e.code if e.code
84
- puts 'Trigger: %s' % e.trigger if e.trigger
84
+ puts "Message: %s" % e.message
85
85
  puts 'Errors:'
86
- if e.errors
87
- e.errors.each_with_index do |error, index|
88
- puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
89
- error.each_pair do |field, value|
90
- if field != :xsi_type
91
- puts ' %s: %s' % [field, value]
92
- end
93
- end
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]
94
90
  end
95
91
  end
96
92
  end