Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adf33d9

Browse files
authored
add add_campaign_draft.py code example (googleads#251)
* code example for update expanded text ads * fixed argument wording * wording tweaks, use f-string * add punctuation * quote around resource name, and end sentence with a period * filename change * add description field * removing empty line * add add_campaign_draft example * address comments for add_campaign_draft.py Co-authored-by: Fei Xiang <[email protected]>
1 parent b8525ad commit adf33d9

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""This example adds a campaign draft for a campaign.
16+
17+
Make sure you specify a campaign that has a non-shared budget.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
from uuid import uuid4
24+
25+
from google.ads.google_ads.client import GoogleAdsClient
26+
from google.ads.google_ads.errors import GoogleAdsException
27+
28+
29+
def main(client, customer_id, base_campaign_id):
30+
campaign_service = client.get_service('CampaignService', version='v3')
31+
campaign_draft_service = client.get_service('CampaignDraftService',
32+
version='v3')
33+
34+
# Creates a campaign draft operation.
35+
campaign_draft_operation = client.get_type('CampaignDraftOperation')
36+
campaign_draft = campaign_draft_operation.create
37+
38+
# Creates a campaign draft.
39+
campaign_draft.base_campaign.value = campaign_service.campaign_path(
40+
customer_id, base_campaign_id)
41+
campaign_draft.name.value = f'Campaign Draft #{uuid4()}'
42+
43+
# Issues a mutate request to add the campaign draft.
44+
try:
45+
campaign_draft_response = (
46+
campaign_draft_service.mutate_campaign_drafts(
47+
customer_id, [campaign_draft_operation]))
48+
except GoogleAdsException as ex:
49+
print(f'Request with ID "{ex.request_id}" failed with status '
50+
f'"{ex.error.code().name}" and includes the following errors:')
51+
for error in ex.failure.errors:
52+
print(f'\tError with message "{error.message}".')
53+
if error.location:
54+
for field_path_element in error.location.field_path_elements:
55+
print(f'\t\tOn field: {field_path_element.field_name}')
56+
sys.exit(1)
57+
58+
print('Created campaign draft: '
59+
f'"{campaign_draft_response.results[0].resource_name}".')
60+
61+
62+
if __name__ == '__main__':
63+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
64+
# home directory if none is specified.
65+
google_ads_client = GoogleAdsClient.load_from_storage()
66+
67+
parser = argparse.ArgumentParser(
68+
description=('Adds a campaign draft for the specified base campaign ID, '
69+
'for the given customer ID.'))
70+
# The following argument(s) should be provided to run the example.
71+
parser.add_argument('-c', '--customer_id', type=str,
72+
required=True, help='The Google Ads customer ID.')
73+
parser.add_argument('-i', '--base_campaign_id', type=str,
74+
required=True, help='The base campaign ID.')
75+
args = parser.parse_args()
76+
77+
main(google_ads_client, args.customer_id, args.base_campaign_id)

0 commit comments

Comments
 (0)