-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathadd_app_campaign.py
executable file
·327 lines (282 loc) · 12.7 KB
/
add_app_campaign.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python
# Copyright 2020 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 adds an App campaign.
For guidance regarding App Campaigns, see:
https://developers.google.com/google-ads/api/docs/app-campaigns/overview
To get campaigns, run basic_operations/get_campaigns.py.
To upload image assets for this campaign, run misc/upload_image_asset.py.
"""
import argparse
from datetime import datetime, timedelta
import sys
from uuid import uuid4
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def main(client, customer_id):
"""Main function for running this example."""
# Creates the budget for the campaign.
budget_resource_name = create_budget(client, customer_id)
# Creates the campaign.
campaign_resource_name = create_campaign(
client, customer_id, budget_resource_name
)
# Sets campaign targeting.
set_campaign_targeting_criteria(client, customer_id, campaign_resource_name)
# Creates an Ad Group.
ad_group_resource_name = create_ad_group(
client, customer_id, campaign_resource_name
)
# Creates an App Ad.
create_app_ad(client, customer_id, ad_group_resource_name)
def create_budget(client, customer_id):
"""Creates a budget under the given customer ID.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
Returns:
A resource_name str for the newly created Budget.
"""
# Retrieves the campaign budget service.
campaign_budget_service = client.get_service("CampaignBudgetService")
# Retrieves a new campaign budget operation object.
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
# Creates a campaign budget.
campaign_budget = campaign_budget_operation.create
campaign_budget.name = f"Interplanetary Cruise #{uuid4()}"
campaign_budget.amount_micros = 50000000
campaign_budget.delivery_method = (
client.enums.BudgetDeliveryMethodEnum.STANDARD
)
# An App campaign cannot use a shared campaign budget.
# explicitly_shared must be set to false.
campaign_budget.explicitly_shared = False
# Submits the campaign budget operation to add the campaign budget.
response = campaign_budget_service.mutate_campaign_budgets(
customer_id=customer_id, operations=[campaign_budget_operation]
)
resource_name = response.results[0].resource_name
print(f'Created campaign budget with resource_name: "{resource_name}"')
return resource_name
def create_campaign(client, customer_id, budget_resource_name):
"""Creates an app campaign under the given customer ID.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
budget_resource_name: the budget to associate with the campaign
Returns:
A resource_name str for the newly created app campaign.
"""
campaign_service = client.get_service("CampaignService")
campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
campaign.name = f"Interplanetary Cruise App #{uuid4()}"
campaign.campaign_budget = budget_resource_name
# 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.enums.CampaignStatusEnum.PAUSED
# All App campaigns have an advertising_channel_type of
# MULTI_CHANNEL to reflect the fact that ads from these campaigns are
# eligible to appear on multiple channels.
campaign.advertising_channel_type = (
client.enums.AdvertisingChannelTypeEnum.MULTI_CHANNEL
)
campaign.advertising_channel_sub_type = (
client.enums.AdvertisingChannelSubTypeEnum.APP_CAMPAIGN
)
# Sets the target CPA to $1 / app install.
#
# campaign_bidding_strategy is a 'oneof' message so setting target_cpa
# is mutually exclusive with other bidding strategies such as
# manual_cpc, commission, maximize_conversions, etc.
# See https://developers.google.com/google-ads/api/reference/rpc
# under current version / resources / Campaign
campaign.target_cpa.target_cpa_micros = 1000000
# Sets the App Campaign Settings.
campaign.app_campaign_setting.app_id = "com.google.android.apps.adwords"
campaign.app_campaign_setting.app_store = (
client.enums.AppCampaignAppStoreEnum.GOOGLE_APP_STORE
)
# Optimize this campaign for getting new users for your app.
campaign.app_campaign_setting.bidding_strategy_goal_type = (
client.enums.AppCampaignBiddingStrategyGoalTypeEnum.OPTIMIZE_INSTALLS_TARGET_INSTALL_COST
)
# Optional fields
campaign.start_date = (datetime.now() + timedelta(1)).strftime("%Y%m%d")
campaign.end_date = (datetime.now() + timedelta(365)).strftime("%Y%m%d")
# Optional: If you select the
# OPTIMIZE_IN_APP_CONVERSIONS_TARGET_INSTALL_COST goal type, then also
# specify your in-app conversion types so the Google Ads API can focus
# your campaign on people who are most likely to complete the
# corresponding in-app actions.
#
# campaign.selective_optimization.conversion_actions.extend(
# ["INSERT_CONVERSION_ACTION_RESOURCE_NAME_HERE"]
# )
# Submits the campaign operation and print the results.
campaign_response = campaign_service.mutate_campaigns(
customer_id=customer_id, operations=[campaign_operation]
)
resource_name = campaign_response.results[0].resource_name
print(f'Created App campaign with resource name: "{resource_name}".')
return resource_name
def set_campaign_targeting_criteria(
client, customer_id, campaign_resource_name
):
"""Sets campaign targeting criteria for a given campaign.
Both location and language targeting are illustrated.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
campaign_resource_name: the campaign to apply targeting to
"""
campaign_criterion_service = client.get_service("CampaignCriterionService")
geo_target_constant_service = client.get_service("GeoTargetConstantService")
googleads_service = client.get_service("GoogleAdsService")
campaign_criterion_operations = []
# Creates the location campaign criteria.
# Besides using location_id, you can also search by location names from
# GeoTargetConstantService.suggest_geo_target_constants() and directly
# apply GeoTargetConstant.resource_name here. An example can be found
# in targeting/get_geo_target_constant_by_names.py.
for location_id in ["21137", "2484"]: # California # Mexico
campaign_criterion_operation = client.get_type(
"CampaignCriterionOperation"
)
campaign_criterion = campaign_criterion_operation.create
campaign_criterion.campaign = campaign_resource_name
campaign_criterion.location.geo_target_constant = (
geo_target_constant_service.geo_target_constant_path(location_id)
)
campaign_criterion_operations.append(campaign_criterion_operation)
# Creates the language campaign criteria.
for language_id in ["1000", "1003"]: # English # Spanish
campaign_criterion_operation = client.get_type(
"CampaignCriterionOperation"
)
campaign_criterion = campaign_criterion_operation.create
campaign_criterion.campaign = campaign_resource_name
campaign_criterion.language.language_constant = (
googleads_service.language_constant_path(language_id)
)
campaign_criterion_operations.append(campaign_criterion_operation)
# Submits the criteria operations.
for row in campaign_criterion_service.mutate_campaign_criteria(
customer_id=customer_id, operations=campaign_criterion_operations
).results:
print(
"Created Campaign Criteria with resource name: "
f'"{row.resource_name}".'
)
def create_ad_group(client, customer_id, campaign_resource_name):
"""Creates an ad group for a given campaign.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
campaign_resource_name: the campaign to be modified
Returns:
A resource_name str for the newly created ad group.
"""
ad_group_service = client.get_service("AdGroupService")
# Creates the ad group.
# Note that the ad group type must not be set.
# Since the advertising_channel_sub_type is APP_CAMPAIGN,
# 1- you cannot override bid settings at the ad group level.
# 2- you cannot add ad group criteria.
ad_group_operation = client.get_type("AdGroupOperation")
ad_group = ad_group_operation.create
ad_group.name = f"Earth to Mars cruises {uuid4()}"
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
ad_group.campaign = campaign_resource_name
ad_group_response = ad_group_service.mutate_ad_groups(
customer_id=customer_id, operations=[ad_group_operation]
)
ad_group_resource_name = ad_group_response.results[0].resource_name
print(f'Ad Group created with resource name: "{ad_group_resource_name}".')
return ad_group_resource_name
def create_app_ad(client, customer_id, ad_group_resource_name):
"""Creates an App ad for a given ad group.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
ad_group_resource_name: the ad group where the ad will be added.
"""
# Creates the ad group ad.
ad_group_ad_service = client.get_service("AdGroupAdService")
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
ad_group_ad.ad_group = ad_group_resource_name
# ad_data is a 'oneof' message so setting app_ad
# is mutually exclusive with ad data fields such as
# text_ad, gmail_ad, etc.
ad_group_ad.ad.app_ad.headlines.extend(
[
create_ad_text_asset(client, "A cool puzzle game"),
create_ad_text_asset(client, "Remove connected blocks"),
]
)
ad_group_ad.ad.app_ad.descriptions.extend(
[
create_ad_text_asset(client, "3 difficulty levels"),
create_ad_text_asset(client, "4 colorful fun skins"),
]
)
# Optional: You can set up to 20 image assets for your campaign.
# ad_group_ad.ad.app_ad.images.extend(
# [INSERT_AD_IMAGE_RESOURCE_NAME(s)_HERE])
ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
customer_id=customer_id, operations=[ad_group_ad_operation]
)
ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name
print(
"Ad Group App Ad created with resource name:"
f'"{ad_group_ad_resource_name}".'
)
def create_ad_text_asset(client, text):
ad_text_asset = client.get_type("AdTextAsset")
ad_text_asset.text = text
return ad_text_asset
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=(
"Adds a App Ad campaign under the specified " "customer ID."
)
)
# 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.",
)
args = parser.parse_args()
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
try:
main(googleads_client, args.customer_id)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)