|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2018 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This example adds a Smart Shopping campaign with an ad group and ad group ad. |
| 18 | +
|
| 19 | +The LoadFromStorage method is pulling credentials and properties from a |
| 20 | +"googleads.yaml" file. By default, it looks for this file in your home |
| 21 | +directory. For more information, see the "Caching authentication information" |
| 22 | +section of our README. |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +import uuid |
| 27 | + |
| 28 | +# Import appropriate modules from the client library. |
| 29 | +from googleads import adwords |
| 30 | + |
| 31 | +# If set to true, a default partition will be created. |
| 32 | +# Set it to false if you plan to run the add_product_partition_tree.py |
| 33 | +# example right after this example. |
| 34 | +CREATE_DEFAULT_PARTITION = False |
| 35 | +MERCHANT_ID = 'INSERT_MERCHANT_ID_HERE' |
| 36 | + |
| 37 | + |
| 38 | +def main(client, merchant_id, create_default_partition): |
| 39 | + budget_id = CreateBudget(client) |
| 40 | + campaign_id = CreateSmartCampaign(client, budget_id, merchant_id) |
| 41 | + ad_group_id = CreateSmartShoppingAdGroup(client, campaign_id) |
| 42 | + CreateSmartShoppingAd(client, ad_group_id) |
| 43 | + |
| 44 | + if create_default_partition: |
| 45 | + CreateDefaultPartition(client, ad_group_id) |
| 46 | + |
| 47 | + |
| 48 | +def CreateBudget(client): |
| 49 | + """Adds a new budget. |
| 50 | +
|
| 51 | + Args: |
| 52 | + client: an AdWordsClient instance. |
| 53 | + Returns: |
| 54 | + A budget ID. |
| 55 | + """ |
| 56 | + budget_service = client.GetService('BudgetService', version='v201809') |
| 57 | + |
| 58 | + budget = { |
| 59 | + 'name': 'Interplanetary budget #%s' % uuid.uuid4(), |
| 60 | + 'amount': { |
| 61 | + 'microAmount': '50000000' |
| 62 | + }, |
| 63 | + 'deliveryMethod': 'STANDARD', |
| 64 | + # Non-shared budgets are required for Smart Shopping campaigns. |
| 65 | + 'isExplicitlyShared': False |
| 66 | + } |
| 67 | + |
| 68 | + budget_operations = [{ |
| 69 | + 'operator': 'ADD', |
| 70 | + 'operand': budget |
| 71 | + }] |
| 72 | + |
| 73 | + # Add the budget. |
| 74 | + budget_id = budget_service.mutate(budget_operations)['value'][0][ |
| 75 | + 'budgetId'] |
| 76 | + |
| 77 | + return budget_id |
| 78 | + |
| 79 | + |
| 80 | +def CreateSmartCampaign(client, budget_id, merchant_id): |
| 81 | + """Adds a new Smart Shopping campaign. |
| 82 | +
|
| 83 | + Args: |
| 84 | + client: an AdWordsClient instance. |
| 85 | + budget_id: the str ID of the budget to be associated with the Shopping |
| 86 | + campaign. |
| 87 | + merchant_id: the str ID of the merchant account to be associated with the |
| 88 | + Shopping campaign. |
| 89 | + Returns: |
| 90 | + A campaign ID. |
| 91 | + """ |
| 92 | + campaign_service = client.GetService('CampaignService', version='v201809') |
| 93 | + # Create campaign with required and optional settings. |
| 94 | + campaign = { |
| 95 | + 'name': 'Shopping campaign #%s' % uuid.uuid4(), |
| 96 | + # The advertisingChannelType is what makes this a Shopping campaign. |
| 97 | + 'advertisingChannelType': 'SHOPPING', |
| 98 | + # Sets the advertisingChannelSubType to SHOPPING_GOAL_OPTIMIZED_ADS to |
| 99 | + # make this a Smart Shopping campaign. |
| 100 | + 'advertisingChannelSubType': 'SHOPPING_GOAL_OPTIMIZED_ADS', |
| 101 | + # Recommendation: Set the campaign to PAUSED when creating it to stop the |
| 102 | + # ads from immediately serving. Set to ENABLED once you've added targeting |
| 103 | + # and the ads are ready to serve. |
| 104 | + 'status': 'PAUSED', |
| 105 | + # Set portfolio budget (required). |
| 106 | + 'budget': {'budgetId': budget_id}, |
| 107 | + # Set a bidding strategy. Only MAXIMIZE_CONVERSION_VALUE is supported. |
| 108 | + 'biddingStrategyConfiguration': { |
| 109 | + 'biddingStrategyType': 'MAXIMIZE_CONVERSION_VALUE' |
| 110 | + }, |
| 111 | + 'settings': [{ |
| 112 | + # All Shopping campaigns need a ShoppingSetting. |
| 113 | + 'xsi_type': 'ShoppingSetting', |
| 114 | + 'salesCountry': 'US', |
| 115 | + 'merchantId': merchant_id |
| 116 | + }] |
| 117 | + } |
| 118 | + |
| 119 | + campaign_operations = [{ |
| 120 | + 'operator': 'ADD', |
| 121 | + 'operand': campaign |
| 122 | + }] |
| 123 | + |
| 124 | + result = campaign_service.mutate(campaign_operations)['value'][0] |
| 125 | + |
| 126 | + print ('Smart Shopping campaign with name "%s" and ID "%s" was added.' |
| 127 | + % (result['name'], result['id'])) |
| 128 | + |
| 129 | + return result['id'] |
| 130 | + |
| 131 | + |
| 132 | +def CreateSmartShoppingAdGroup(client, campaign_id): |
| 133 | + """Adds a new Smart Shopping ad group. |
| 134 | +
|
| 135 | + Args: |
| 136 | + client: an AdWordsClient instance. |
| 137 | + campaign_id: the str ID of a Smart Shopping campaign. |
| 138 | + Returns: |
| 139 | + An ad group ID. |
| 140 | + """ |
| 141 | + ad_group_service = client.GetService('AdGroupService', version='v201809') |
| 142 | + # Create the ad group. |
| 143 | + ad_group = { |
| 144 | + 'campaignId': campaign_id, |
| 145 | + 'name': 'Smart Shopping ad group #%s' % uuid.uuid4(), |
| 146 | + # Set the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS. |
| 147 | + 'adGroupType': 'SHOPPING_GOAL_OPTIMIZED_ADS' |
| 148 | + } |
| 149 | + |
| 150 | + adgroup_operations = { |
| 151 | + 'operator': 'ADD', |
| 152 | + 'operand': ad_group |
| 153 | + } |
| 154 | + |
| 155 | + # Make the mutate request to add the AdGroup to the Smart Shopping campaign. |
| 156 | + ad_group = ad_group_service.mutate(adgroup_operations)['value'][0] |
| 157 | + ad_group_id = ad_group['id'] |
| 158 | + |
| 159 | + print ('AdGroup with name "%s" and ID "%s" was added.' |
| 160 | + % (ad_group['name'], ad_group_id)) |
| 161 | + |
| 162 | + return ad_group_id |
| 163 | + |
| 164 | + |
| 165 | +def CreateSmartShoppingAd(client, ad_group_id): |
| 166 | + """Adds a new Smart Shopping ad. |
| 167 | +
|
| 168 | + Args: |
| 169 | + client: an AdWordsClient instance. |
| 170 | + ad_group_id: an integer ID for an ad group. |
| 171 | + """ |
| 172 | + ad_group_ad_service = client.GetService('AdGroupAdService', version='v201809') |
| 173 | + # Create an AdGroup Ad. |
| 174 | + adgroup_ad = { |
| 175 | + 'adGroupId': ad_group_id, |
| 176 | + # Create a Smart Shopping ad (Goal-optimized Shopping ad). |
| 177 | + 'ad': { |
| 178 | + 'xsi_type': 'GoalOptimizedShoppingAd' |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + ad_operation = { |
| 183 | + 'operator': 'ADD', |
| 184 | + 'operand': adgroup_ad |
| 185 | + } |
| 186 | + |
| 187 | + # Make the mutate request to add the Smart Shopping ad to the AdGroup. |
| 188 | + ad_result = ad_group_ad_service.mutate([ad_operation]) |
| 189 | + |
| 190 | + for adgroup_ad in ad_result['value']: |
| 191 | + print 'Smart Shopping ad with ID "%s" was added.' % adgroup_ad['ad']['id'] |
| 192 | + |
| 193 | + |
| 194 | +def CreateDefaultPartition(client, ad_group_id): |
| 195 | + """Creates a default partition. |
| 196 | +
|
| 197 | + Args: |
| 198 | + client: an AdWordsClient instance. |
| 199 | + ad_group_id: an integer ID for an ad group. |
| 200 | + """ |
| 201 | + ad_group_criterion_service = client.GetService('AdGroupCriterionService', |
| 202 | + version='v201809') |
| 203 | + |
| 204 | + operations = [{ |
| 205 | + 'operator': 'ADD', |
| 206 | + 'operand': { |
| 207 | + 'xsi_type': 'BiddableAdGroupCriterion', |
| 208 | + 'adGroupId': ad_group_id, |
| 209 | + # Make sure that caseValue and parentCriterionId are left unspecified. |
| 210 | + # This makes this partition as generic as possible to use as a |
| 211 | + # fallback when others don't match. |
| 212 | + 'criterion': { |
| 213 | + 'xsi_type': 'ProductPartition', |
| 214 | + 'partitionType': 'UNIT' |
| 215 | + } |
| 216 | + } |
| 217 | + }] |
| 218 | + |
| 219 | + ad_group_criterion = ad_group_criterion_service.mutate(operations)['value'][0] |
| 220 | + |
| 221 | + print ('Ad group criterion with ID "%d" in ad group with ID "%d" was added.' |
| 222 | + % (ad_group_criterion['criterion']['id'], |
| 223 | + ad_group_criterion['adGroupId'])) |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == '__main__': |
| 227 | + # Initialize client object. |
| 228 | + adwords_client = adwords.AdWordsClient.LoadFromStorage() |
| 229 | + |
| 230 | + main(adwords_client, MERCHANT_ID, CREATE_DEFAULT_PARTITION) |
0 commit comments