/
add_campaigns.py
executable file
·141 lines (119 loc) · 5.03 KB
/
add_campaigns.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
#!/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 illustrates how to add a campaign.
To get campaigns, run get_campaigns.py.
"""
import argparse
import datetime
import sys
import uuid
import google.ads.google_ads.client
_DATE_FORMAT = "%Y%m%d"
def main(client, customer_id):
campaign_budget_service = client.get_service(
"CampaignBudgetService", version="v6"
)
campaign_service = client.get_service("CampaignService", version="v6")
# [START add_campaigns_1]
# Create a budget, which can be shared by multiple campaigns.
campaign_budget_operation = client.get_type(
"CampaignBudgetOperation", version="v6"
)
campaign_budget = campaign_budget_operation.create
campaign_budget.name = "Interplanetary Budget %s" % uuid.uuid4()
campaign_budget.delivery_method = client.get_type(
"BudgetDeliveryMethodEnum"
).STANDARD
campaign_budget.amount_micros = 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)
# [END add_campaigns_1]
# [START add_campaigns]
# Create campaign.
campaign_operation = client.get_type("CampaignOperation", version="v6")
campaign = campaign_operation.create
campaign.name = "Interplanetary Cruise %s" % uuid.uuid4()
campaign.advertising_channel_type = client.get_type(
"AdvertisingChannelTypeEnum"
).SEARCH
# 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="v6").PAUSED
# Set the bidding strategy and budget.
campaign.manual_cpc.enhanced_cpc_enabled = True
campaign.campaign_budget = campaign_budget_response.results[0].resource_name
# Set the campaign network options.
campaign.network_settings.target_google_search = True
campaign.network_settings.target_search_network = True
campaign.network_settings.target_content_network = False
campaign.network_settings.target_partner_search_network = False
# [END add_campaigns]
# Optional: Set the start date.
start_time = datetime.date.today() + datetime.timedelta(days=1)
campaign.start_date = datetime.date.strftime(start_time, _DATE_FORMAT)
# Optional: Set the end date.
end_time = start_time + datetime.timedelta(weeks=4)
campaign.end_date = datetime.date.strftime(end_time, _DATE_FORMAT)
# 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)
print("Created campaign %s." % campaign_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 campaign for specified customer."
)
# 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()
main(google_ads_client, args.customer_id)