/
add_shopping_product_ad.py
executable file
·299 lines (242 loc) · 13.3 KB
/
add_shopping_product_ad.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# Copyright 2018 Google LLC
#
# 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
#
# https://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 creates a standard shopping product ad.
In the process of creating a standard shopping campaign and a shopping product
ad group are also created.
Prerequisite: You need to have access to a Merchant Center account. You can find
instructions to create a Merchant Center account here:
https://support.google.com/merchants/answer/188924.
This account must be linked to your Google Ads account.
"""
import argparse
import sys
import uuid
import google.ads.google_ads.client
def main(client, customer_id, merchant_center_account_id,
create_default_listing_group):
# Creates a budget to be used by the campaign that will be created below.
budget_resource_name = add_campaign_budget(client, customer_id)
# Create a standard shopping campaign.
campaign_resource_name = add_standard_shopping_campaign(
client, customer_id, budget_resource_name, merchant_center_account_id)
# Create a shoppng product ad group.
ad_group_resource_name = add_shopping_product_ad_group(client, customer_id,
campaign_resource_name)
# Create a shopping product ad group ad.
add_shopping_product_ad_group_ad(client, customer_id,
ad_group_resource_name)
if (create_default_listing_group):
# Creates an ad group criterion containing a listing group.
# This will be the listing group tree for 'All products' and will
# contain a single biddable unit node.
add_default_shopping_listing_group(client, customer_id,
ad_group_resource_name)
def add_campaign_budget(client, customer_id):
"""Creates a new campaign budget in the specified client account."""
campaign_budget_service = client.get_service('CampaignBudgetService',
version='v3')
# Create a budget, which can be shared by multiple campaigns.
campaign_budget_operation = client.get_type('CampaignBudgetOperation',
version='v3')
campaign_budget = campaign_budget_operation.create
campaign_budget.name.value = 'Interplanetary Budget %s' % uuid.uuid4()
campaign_budget.delivery_method = client.get_type(
'BudgetDeliveryMethodEnum').STANDARD
campaign_budget.amount_micros.value = 500000
# Add budget.
try:
campaign_budget_response = (
campaign_budget_service.mutate_campaign_budgets(
customer_id, [campaign_budget_operation]))
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
sys.exit(1)
budget_resource_name = campaign_budget_response.results[0].resource_name
print('Added a budget with resource name: %s' % budget_resource_name)
return budget_resource_name
def add_shopping_product_ad_group_ad(client, customer_id,
ad_group_resource_name):
"""Creates a new shopping product ad group ad in the specified ad group."""
ad_group_ad_service = client.get_service('AdGroupAdService', version='v3')
# Creates a new ad group ad and sets the product ad to it.
ad_group_ad_operation = client.get_type('AdGroupAdOperation', version='v3')
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.ad_group.value = ad_group_resource_name
ad_group_ad.status = client.get_type('AdGroupAdStatusEnum',
version='v3').PAUSED
ad_group_ad.ad.shopping_product_ad.CopyFrom(client.get_type(
'ShoppingProductAdInfo'))
# Add the ad group ad.
try:
ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
customer_id, [ad_group_ad_operation])
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
sys.exit(1)
ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name
print('Created shopping product ad group ad %s.' %
ad_group_ad_resource_name)
return ad_group_resource_name
def add_shopping_product_ad_group(client, customer_id, campaign_resource_name):
"""Creates a new shopping product ad group in the specified campaign."""
ad_group_service = client.get_service('AdGroupService', version='v3')
# Create ad group.
ad_group_operation = client.get_type('AdGroupOperation', version='v3')
ad_group = ad_group_operation.create
ad_group.name.value = 'Earth to Mars cruise %s' % uuid.uuid4()
ad_group.status = client.get_type('AdGroupStatusEnum', version='v3').ENABLED
ad_group.campaign.value = campaign_resource_name
# Sets the ad group type to SHOPPING_PRODUCT_ADS. This is the only value
# possible for ad groups that contain shopping product ads.
ad_group.type = client.get_type('AdGroupTypeEnum',
version='v3').SHOPPING_PRODUCT_ADS
ad_group.cpc_bid_micros.value = 10000000
# Add the ad group.
try:
ad_group_response = ad_group_service.mutate_ad_groups(
customer_id, [ad_group_operation])
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
sys.exit(1)
ad_group_resource_name = ad_group_response.results[0].resource_name
print('Added a product shopping ad group with resource name "%s".'
% ad_group_resource_name)
return ad_group_resource_name
def add_standard_shopping_campaign(client, customer_id, budget_resource_name,
merchant_center_account_id):
"""Creates a new standard shopping campaign in the specified client account.
"""
campaign_service = client.get_service('CampaignService', version='v3')
# Create standard shoppping campaign.
campaign_operation = client.get_type('CampaignOperation', version='v3')
campaign = campaign_operation.create
campaign.name.value = 'Interplanetary Cruise Campaign %s' % uuid.uuid4()
# Configures settings related to shopping campaigns including advertising
# channel type and shopping setting.
campaign.advertising_channel_type = client.get_type(
'AdvertisingChannelTypeEnum').SHOPPING
campaign.shopping_setting.merchant_id.value = merchant_center_account_id
# Sets the sales country of products to include in the campaign.
campaign.shopping_setting.sales_country.value = "US"
# Sets the priority of the campaign. Higher numbers take priority over lower
# numbers. For standard shopping campaigns, allowed values are between 0 and
# 2, inclusive.
campaign.shopping_setting.campaign_priority.value = 0
# Enables local inventory ads for this campaign.
campaign.shopping_setting.enable_local.value = True
# Recommendation: Set the campaign to PAUSED when creating it to prevent the
# ads from immediately serving. Set to ENABLED once you've added targeting
# and the ads are ready to serve.
campaign.status = client.get_type('CampaignStatusEnum', version='v3').PAUSED
# Sets the bidding strategy to Manual CPC (with eCPC enabled)
# Recommendation: Use one of the automated bidding strategies for Shopping
# campaigns to help you optimize your advertising spend. More information
# can be found here: https://support.google.com/google-ads/answer/6309029
campaign.manual_cpc.enhanced_cpc_enabled.value = True
# Sets the budget.
campaign.campaign_budget.value = budget_resource_name
# Add the campaign.
try:
campaign_response = campaign_service.mutate_campaigns(
customer_id, [campaign_operation])
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
sys.exit(1)
campaign_resource_name = campaign_response.results[0].resource_name
print('Added a standard shopping campaign with resource name "%s".'
% campaign_resource_name)
return campaign_resource_name
def add_default_shopping_listing_group(client, customer_id,
ad_group_resource_name):
"""Creates a new default shopping listing group for the specified ad group.
A listing group is the Google Ads API representation of a "product group"
described in the Google Ads user interface. The listing group will be added
to the ad group using an "ad group criterion". The criterion will contain
the bid for a given listing group.
"""
ad_group_criterion_service = client.get_service('AdGroupCriterionService',
version='v3')
# Creates a new ad group criterion. This will contain the "default" listing
# group (All products).
ad_group_criterion_operation = client.get_type('AdGroupCriterionOperation',
version='v3')
ad_group_criterion = ad_group_criterion_operation.create
ad_group_criterion.ad_group.value = ad_group_resource_name
ad_group_criterion.status = client.get_type(
'AdGroupCriterionStatusEnum').ENABLED
ad_group_criterion.listing_group.type = client.get_type(
'ListingGroupTypeEnum').UNIT
# Set the bid for products in this listing group unit.
ad_group_criterion.cpc_bid_micros.value = 500000
try:
ad_group_criterion_response = (
ad_group_criterion_service.mutate_ad_group_criteria(
customer_id, [ad_group_criterion_operation]))
except google.ads.google_ads.errors.GoogleAdsException as ex:
print('Request with ID "%s" failed with status "%s" and includes the '
'following errors:' % (ex.request_id, ex.error.code().name))
for error in ex.failure.errors:
print('\tError with message "%s".' % error.message)
if error.location:
for field_path_element in error.location.field_path_elements:
print('\t\tOn field: %s' % field_path_element.field_name)
sys.exit(1)
print('Added an ad group criterion containing a listing group with \
resource name: %s.' %
ad_group_criterion_response.results[0].resource_name)
if __name__ == '__main__':
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
.load_from_storage())
parser = argparse.ArgumentParser(
description=('Adds a standard shopping campaign, a shopping product ad '
'group and a shopping product ad to the specified '
'merchant account.'))
# The following argument(s) should be provided to run the example.
parser.add_argument('-c', '--customer_id', type=str,
required=True, help='The Google Ads customer ID.')
parser.add_argument('-m', '--merchant_center_account_id', type=int,
required=True, help='The merchant center account ID.')
parser.add_argument('-d', '--create_default_listing_group',
action='store_true', default=False,
help='Create a default listing group.')
args = parser.parse_args()
main(google_ads_client, args.customer_id, args.merchant_center_account_id,
args.create_default_listing_group)