/
add_ad_group_criteria.rb
executable file
·99 lines (88 loc) · 3.02 KB
/
add_ad_group_criteria.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/ruby
#
# Author:: api.sgomes@gmail.com (Sérgio Gomes)
#
# Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example illustrates how to add a criterion to a given ad group. To create
# an ad group, run add_ad_group.rb.
#
# Tags: AdGroupCriterionService.mutate
gem 'google-adwords-api'
require 'adwords_api'
API_VERSION = :v201003
def add_ad_group_criteria()
# AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
# when called without parameters.
adwords = AdwordsApi::Api.new
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
# the configuration file or provide your own logger:
# adwords.logger = Logger.new('adwords_xml.log')
ad_group_criterion_srv =
adwords.service(:AdGroupCriterionService, API_VERSION)
ad_group_id = 'INSERT_AD_GROUP_ID_HERE'.to_i
# Create placement.
plc_operation = {
:operator => 'ADD',
:operand => {
:xsi_type => 'BiddableAdGroupCriterion',
:ad_group_id => ad_group_id,
:criterion => {
:xsi_type => 'Placement',
:url => 'http://mars.google.com'
}
}
}
# Add criteria.
response = ad_group_criterion_srv.mutate([plc_operation])
if response and response[:value]
ad_group_criteria = response[:value]
puts "Added #{ad_group_criteria.length} criteria to ad group " +
"#{ad_group_id}."
ad_group_criteria.each do |ad_group_criterion|
puts " Criterion id is #{ad_group_criterion[:criterion][:id]} and " +
"type is #{ad_group_criterion[:criterion][:xsi_type]}."
end
else
puts "No criteria were added."
end
end
if __FILE__ == $0
begin
add_ad_group_criteria()
# Connection error. Likely transitory.
rescue Errno::ECONNRESET, SOAP::HTTPStreamError, SocketError => e
puts 'Connection Error: %s' % e
puts 'Source: %s' % e.backtrace.first
# API Error.
rescue AdwordsApi::Errors::ApiException => e
puts 'API Exception caught.'
puts 'Message: %s' % e.message
puts 'Code: %d' % e.code if e.code
puts 'Trigger: %s' % e.trigger if e.trigger
puts 'Errors:'
if e.errors
e.errors.each_with_index do |error, index|
puts ' %d. Error type is %s. Fields:' % [index + 1, error[:xsi_type]]
error.each_pair do |field, value|
if field != :xsi_type
puts ' %s: %s' % [field, value]
end
end
end
end
end
end