|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Encoding: utf-8 |
| 3 | +# |
| 4 | +# Copyright 2018 Google LLC |
| 5 | +# |
| 6 | +# License:: Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | +# implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +# This example adds a Smart Shopping campaign with an ad group and ad group ad. |
| 20 | + |
| 21 | +require 'adwords_api' |
| 22 | +require 'date' |
| 23 | + |
| 24 | +def add_smart_shopping_campaign(merchant_id, make_default_partition) |
| 25 | + # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml |
| 26 | + # when called without parameters. |
| 27 | + adwords = AdwordsApi::Api.new |
| 28 | + |
| 29 | + # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in |
| 30 | + # the configuration file or provide your own logger: |
| 31 | + # adwords.logger = Logger.new('adwords_xml.log') |
| 32 | + |
| 33 | + budget_id = create_budget(adwords) |
| 34 | + campaign_id = create_smart_campaign(adwords, budget_id, merchant_id) |
| 35 | + ad_group_id = create_smart_shopping_ad_group(adwords, campaign_id) |
| 36 | + create_smart_shopping_ad(adwords, ad_group_id) |
| 37 | + |
| 38 | + create_default_partition(adwords, ad_group_id) if make_default_partition |
| 39 | +end |
| 40 | + |
| 41 | +def create_budget(adwords) |
| 42 | + budget_srv = adwords.service(:BudgetService, API_VERSION) |
| 43 | + |
| 44 | + budget = { |
| 45 | + :name => "Interplanetary budget #%d" % (Time.new.to_f * 1000).to_i, |
| 46 | + :amount => { |
| 47 | + :micro_amount => 50_000_000 |
| 48 | + }, |
| 49 | + :delivery_method => 'STANDARD', |
| 50 | + # Non-shared budgets are required for Smart Shopping campaigns. |
| 51 | + :is_explicitly_shared => false |
| 52 | + } |
| 53 | + |
| 54 | + budget_operations = [{ |
| 55 | + :operator => 'ADD', |
| 56 | + :operand => budget |
| 57 | + }] |
| 58 | + |
| 59 | + return budget_srv.mutate(budget_operations)[:value].first[:budget_id] |
| 60 | +end |
| 61 | + |
| 62 | +# [START createCampaign] MOE:strip_line |
| 63 | +def create_smart_campaign(adwords, budget_id, merchant_id) |
| 64 | + campaign_srv = adwords.service(:CampaignService, API_VERSION) |
| 65 | + |
| 66 | + campaign = { |
| 67 | + :name => "Smart Shopping campaign #%d" % (Time.new.to_f * 1000).to_i, |
| 68 | + # The advertisingChannelType is what makes this a Shopping campaign. |
| 69 | + :advertising_channel_type => 'SHOPPING', |
| 70 | + # Sets the advertisingChannelSubType to SHOPPING_GOAL_OPTIMIZED_ADS to |
| 71 | + # make this a Smart Shopping campaign. |
| 72 | + :advertising_channel_sub_type => 'SHOPPING_GOAL_OPTIMIZED_ADS', |
| 73 | + # Recommendation: Set the campaign to PAUSED when creating it to stop the |
| 74 | + # ads from immediately serving. Set to ENABLED once you've added targeting |
| 75 | + # and the ads are ready to serve. |
| 76 | + :status => 'PAUSED', |
| 77 | + # Set portfolio budget (required). |
| 78 | + :budget => { |
| 79 | + :budget_id => budget_id |
| 80 | + }, |
| 81 | + # Set a bidding strategy. Only MAXIMIZE_CONVERSION_VALUE is supported. |
| 82 | + :bidding_strategy_configuration => { |
| 83 | + :bidding_strategy_type => 'MAXIMIZE_CONVERSION_VALUE' |
| 84 | + }, |
| 85 | + # All Shopping campaigns need a ShoppingSetting. |
| 86 | + :settings => [{ |
| 87 | + :xsi_type => 'ShoppingSetting', |
| 88 | + :sales_country => 'US', |
| 89 | + :merchant_id => merchant_id |
| 90 | + }] |
| 91 | + } |
| 92 | + |
| 93 | + campaign_operations = [{ |
| 94 | + :operator => 'ADD', |
| 95 | + :operand => campaign |
| 96 | + }] |
| 97 | + |
| 98 | + result = campaign_srv.mutate(campaign_operations)[:value].first |
| 99 | + |
| 100 | + puts 'Smart Shopping campaign with name "%s" and ID %d was added.' % |
| 101 | + [result[:name], result[:id]] |
| 102 | + |
| 103 | + return result[:id] |
| 104 | +end |
| 105 | +# [END createCampaign] MOE:strip_line |
| 106 | + |
| 107 | +# [START createAdGroup] MOE:strip_line |
| 108 | +def create_smart_shopping_ad_group(adwords, campaign_id) |
| 109 | + ad_group_srv = adwords.service(:AdGroupService, API_VERSION) |
| 110 | + |
| 111 | + ad_group = { |
| 112 | + :campaign_id => campaign_id, |
| 113 | + :name => "Smart Shopping ad group #%d" % (Time.new.to_f * 1000).to_i, |
| 114 | + # Set the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS |
| 115 | + :ad_group_type => 'SHOPPING_GOAL_OPTIMIZED_ADS' |
| 116 | + } |
| 117 | + |
| 118 | + ad_group_operations = [{ |
| 119 | + :operator => 'ADD', |
| 120 | + :operand => ad_group |
| 121 | + }] |
| 122 | + |
| 123 | + result = ad_group_srv.mutate(ad_group_operations)[:value].first |
| 124 | + |
| 125 | + puts 'Smart Shopping ad group with name "%s" and ID %d was added.' % |
| 126 | + [result[:name], result[:id]] |
| 127 | + |
| 128 | + return result[:id] |
| 129 | +end |
| 130 | +# [END createAdGroup] MOE:strip_line |
| 131 | + |
| 132 | +# [START createAdGroupAd] MOE:strip_line |
| 133 | +def create_smart_shopping_ad(adwords, ad_group_id) |
| 134 | + ad_group_ad_srv = adwords.service(:AdGroupAdService, API_VERSION) |
| 135 | + |
| 136 | + ad_group_ad = { |
| 137 | + :ad_group_id => ad_group_id, |
| 138 | + # Create a Smart Shopping ad (Goal-optimized Shopping ad). |
| 139 | + :ad => { |
| 140 | + :xsi_type => 'GoalOptimizedShoppingAd' |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + ad_operations = [{ |
| 145 | + :operator => 'ADD', |
| 146 | + :operand => ad_group_ad |
| 147 | + }] |
| 148 | + |
| 149 | + result = ad_group_ad_srv.mutate(ad_operations)[:value].first |
| 150 | + |
| 151 | + puts 'Smart Shopping ad with ID "%s" was added.' % result[:ad][:id] |
| 152 | +end |
| 153 | +# [END createAdGroupAd] MOE:strip_line |
| 154 | + |
| 155 | +def create_default_partition(adwords, ad_group_id) |
| 156 | + ad_group_criterion_srv = |
| 157 | + adwords.service(:AdGroupCriterionService, API_VERSION) |
| 158 | + |
| 159 | + criterion = { |
| 160 | + :xsi_type => 'BiddableAdGroupCriterion', |
| 161 | + :ad_group_id => ad_group_id, |
| 162 | + # Make sure that the caseValue and parentCriterionId are left unspecified. |
| 163 | + # This makes this partition as generic as poissible to use as a fallback |
| 164 | + # when others don't match. |
| 165 | + :criterion => { |
| 166 | + :xsi_type => 'ProductPartition', |
| 167 | + :partition_type => 'UNIT' |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + criterion_operations = [{ |
| 172 | + :operator => 'ADD', |
| 173 | + :operand => criterion |
| 174 | + }] |
| 175 | + |
| 176 | + result = ad_group_criterion_srv.mutate(criterion_operations)[:value].first |
| 177 | + |
| 178 | + puts 'Ad group criterion with ID %" in ad group with ID %d was added.' % |
| 179 | + [result[:criterion][:id], result[:ad_group_id]] |
| 180 | +end |
| 181 | + |
| 182 | +if __FILE__ == $0 |
| 183 | + API_VERSION = :v201809 |
| 184 | + |
| 185 | + begin |
| 186 | + merchant_id = 'INSTERT_MERCHANT_ID_HERE'.to_i |
| 187 | + |
| 188 | + # If set to true, a default partition will be created. If running the |
| 189 | + # add_product_partition_tree.rb example right after this example, make |
| 190 | + # sure this stays set to false. |
| 191 | + make_default_partition = false |
| 192 | + |
| 193 | + add_smart_shopping_campaign(merchant_id, make_default_partition) |
| 194 | + |
| 195 | + # Authorization error. |
| 196 | + rescue AdsCommon::Errors::OAuth2VerificationRequired => e |
| 197 | + puts "Authorization credentials are not valid. Edit adwords_api.yml for " + |
| 198 | + "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " + |
| 199 | + "to retrieve and store OAuth2 tokens." |
| 200 | + puts "See this wiki page for more details:\n\n " + |
| 201 | + 'https://github.com/googleads/google-api-ads-ruby/wiki/OAuth2' |
| 202 | + |
| 203 | + # HTTP errors. |
| 204 | + rescue AdsCommon::Errors::HttpError => e |
| 205 | + puts "HTTP Error: %s" % e |
| 206 | + |
| 207 | + # API errors. |
| 208 | + rescue AdwordsApi::Errors::ApiException => e |
| 209 | + puts "Message: %s" % e.message |
| 210 | + puts 'Errors:' |
| 211 | + e.errors.each_with_index do |error, index| |
| 212 | + puts "\tError [%d]:" % (index + 1) |
| 213 | + error.each do |field, value| |
| 214 | + puts "\t\t%s: %s" % [field, value] |
| 215 | + end |
| 216 | + end |
| 217 | + end |
| 218 | +end |
0 commit comments