|
| 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 illustrates how to create campaigns. |
| 22 | +# |
| 23 | +# Tags: CampaignService.mutate, BudgetService.mutate |
| 24 | + |
| 25 | +require 'adwords_api' |
| 26 | +require 'date' |
| 27 | + |
| 28 | +def add_campaigns() |
| 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 | + budget_srv = adwords.service(:BudgetService, API_VERSION) |
| 38 | + campaign_srv = adwords.service(:CampaignService, API_VERSION) |
| 39 | + |
| 40 | + # Create a budget, which can be shared by multiple campaigns. |
| 41 | + budget = { |
| 42 | + :name => 'Interplanetary budget #%d' % (Time.new.to_f * 1000).to_i, |
| 43 | + :amount => {:micro_amount => 50000000}, |
| 44 | + :delivery_method => 'STANDARD', |
| 45 | + :period => 'DAILY' |
| 46 | + } |
| 47 | + budget_operation = {:operator => 'ADD', :operand => budget} |
| 48 | + |
| 49 | + # Add budget. |
| 50 | + return_budget = budget_srv.mutate([budget_operation]) |
| 51 | + budget_id = return_budget[:value].first[:budget_id] |
| 52 | + |
| 53 | + # Create campaigns. |
| 54 | + campaigns = [ |
| 55 | + { |
| 56 | + :name => "Interplanetary Cruise #%d" % (Time.new.to_f * 1000).to_i, |
| 57 | + :status => 'PAUSED', |
| 58 | + :bidding_strategy_configuration => { |
| 59 | + :bidding_strategy_type => 'MANUAL_CPC', |
| 60 | + # Optional: provide BiddingScheme. |
| 61 | + :bidding_scheme => { |
| 62 | + :xsi_type => 'ManualCpcBiddingScheme', |
| 63 | + :enhanced_cpc_enabled => false |
| 64 | + } |
| 65 | + }, |
| 66 | + # Budget (required) - note only the budget ID is required. |
| 67 | + :budget => {:budget_id => budget_id}, |
| 68 | + :advertising_channel_type => 'SEARCH', |
| 69 | + # Optional fields: |
| 70 | + :start_date => |
| 71 | + DateTime.parse((Date.today + 1).to_s).strftime('%Y%m%d'), |
| 72 | + :ad_serving_optimization_status => 'ROTATE', |
| 73 | + :network_setting => { |
| 74 | + :target_google_search => true, |
| 75 | + :target_search_network => true, |
| 76 | + :target_content_network => true |
| 77 | + }, |
| 78 | + :settings => [ |
| 79 | + { |
| 80 | + :xsi_type => 'GeoTargetTypeSetting', |
| 81 | + :positive_geo_target_type => 'DONT_CARE', |
| 82 | + :negative_geo_target_type => 'DONT_CARE' |
| 83 | + }, |
| 84 | + { |
| 85 | + :xsi_type => 'KeywordMatchSetting', |
| 86 | + :opt_in => true |
| 87 | + } |
| 88 | + ], |
| 89 | + :frequency_cap => { |
| 90 | + :impressions => '5', |
| 91 | + :time_unit => 'DAY', |
| 92 | + :level => 'ADGROUP' |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + :name => "Interplanetary Cruise banner #%d" % (Time.new.to_f * 1000).to_i, |
| 97 | + :status => 'PAUSED', |
| 98 | + :bidding_strategy_configuration => { |
| 99 | + :bidding_strategy_type => 'MANUAL_CPC' |
| 100 | + }, |
| 101 | + :budget => {:budget_id => budget_id}, |
| 102 | + :settings => [ |
| 103 | + { |
| 104 | + :xsi_type => 'KeywordMatchSetting', |
| 105 | + :opt_in => true |
| 106 | + } |
| 107 | + ], |
| 108 | + :advertising_channel_type => 'DISPLAY' |
| 109 | + } |
| 110 | + ] |
| 111 | + |
| 112 | + # Prepare for adding campaign. |
| 113 | + operations = campaigns.map do |campaign| |
| 114 | + {:operator => 'ADD', :operand => campaign} |
| 115 | + end |
| 116 | + |
| 117 | + # Add campaign. |
| 118 | + response = campaign_srv.mutate(operations) |
| 119 | + if response and response[:value] |
| 120 | + response[:value].each do |campaign| |
| 121 | + puts "Campaign with name '%s' and ID %d was added." % |
| 122 | + [campaign[:name], campaign[:id]] |
| 123 | + end |
| 124 | + else |
| 125 | + raise new StandardError, 'No campaigns were added.' |
| 126 | + end |
| 127 | +end |
| 128 | + |
| 129 | +if __FILE__ == $0 |
| 130 | + API_VERSION = :v201402 |
| 131 | + |
| 132 | + begin |
| 133 | + add_campaigns() |
| 134 | + |
| 135 | + # Authorization error. |
| 136 | + rescue AdsCommon::Errors::OAuth2VerificationRequired => e |
| 137 | + puts "Authorization credentials are not valid. Edit adwords_api.yml for " + |
| 138 | + "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " + |
| 139 | + "to retrieve and store OAuth2 tokens." |
| 140 | + puts "See this wiki page for more details:\n\n " + |
| 141 | + 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2' |
| 142 | + |
| 143 | + # HTTP errors. |
| 144 | + rescue AdsCommon::Errors::HttpError => e |
| 145 | + puts "HTTP Error: %s" % e |
| 146 | + |
| 147 | + # API errors. |
| 148 | + rescue AdwordsApi::Errors::ApiException => e |
| 149 | + puts "Message: %s" % e.message |
| 150 | + puts 'Errors:' |
| 151 | + e.errors.each_with_index do |error, index| |
| 152 | + puts "\tError [%d]:" % (index + 1) |
| 153 | + error.each do |field, value| |
| 154 | + puts "\t\t%s: %s" % [field, value] |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments