|
| 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 code example adds a campaign label to a list of campaigns. |
| 16 | +
|
| 17 | +This example assumes that a label has already been prepared. |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import sys |
| 22 | + |
| 23 | +from google.ads.google_ads.client import GoogleAdsClient |
| 24 | +from google.ads.google_ads.errors import GoogleAdsException |
| 25 | + |
| 26 | + |
| 27 | +def main(client, customer_id, label_id, campaign_ids): |
| 28 | + """This code example adds a campaign label to a list of campaigns. |
| 29 | +
|
| 30 | + Args: |
| 31 | + client: An initialized GoogleAdsClient instance. |
| 32 | + customer_id: A client customer ID str. |
| 33 | + label_id: The ID of the label to attach to campaigns. |
| 34 | + campaign_ids: A list of campaign IDs to which the label will be added. |
| 35 | + """ |
| 36 | + |
| 37 | + # Get an instance of CampaignLabelService client. |
| 38 | + campaign_label_service = client.get_service( |
| 39 | + 'CampaignLabelService', version='v3') |
| 40 | + campaign_service = client.get_service('CampaignService', version='v3') |
| 41 | + label_service = client.get_service('LabelService', version='v3') |
| 42 | + |
| 43 | + # Build the resource name of the label to be added across the campaigns. |
| 44 | + label_resource_name = label_service.label_path(customer_id, label_id) |
| 45 | + |
| 46 | + operations = [] |
| 47 | + |
| 48 | + for campaign_id in campaign_ids: |
| 49 | + campaign_resource_name = campaign_service.campaign_path(customer_id, |
| 50 | + campaign_id) |
| 51 | + campaign_label_operation = client.get_type( |
| 52 | + 'CampaignLabelOperation', version='v3') |
| 53 | + |
| 54 | + campaign_label = campaign_label_operation.create |
| 55 | + campaign_label.campaign.value = campaign_resource_name |
| 56 | + campaign_label.label.value = label_resource_name |
| 57 | + operations.append(campaign_label_operation) |
| 58 | + |
| 59 | + try: |
| 60 | + response = campaign_label_service.mutate_campaign_labels( |
| 61 | + customer_id, operations) |
| 62 | + print(f'Added {len(response.results)} campaign labels:') |
| 63 | + for result in response.results: |
| 64 | + print(result.resource_name) |
| 65 | + except GoogleAdsException as error: |
| 66 | + print('Request with ID "{}" failed with status "{}" and includes the ' |
| 67 | + 'following errors:'.format( |
| 68 | + error.request_id, error.error.code().name)) |
| 69 | + for error in error.failure.errors: |
| 70 | + print('\tError with message "{}".'.format(error.message)) |
| 71 | + if error.location: |
| 72 | + for field_path_element in error.location.field_path_elements: |
| 73 | + print('\t\tOn field: {}'.format( |
| 74 | + field_path_element.field_name)) |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 80 | + # home directory if none is specified. |
| 81 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 82 | + |
| 83 | + parser = argparse.ArgumentParser( |
| 84 | + description='This code example adds a campaign label to a list of ' |
| 85 | + 'campaigns.') |
| 86 | + # The following argument(s) should be provided to run the example. |
| 87 | + parser.add_argument('-c', '--customer_id', type=str, |
| 88 | + required=True, help='The Google Ads customer ID.') |
| 89 | + parser.add_argument('-l', '--label_id', type=str, required=True, |
| 90 | + help='The ID of the label to attach to campaigns.') |
| 91 | + parser.add_argument('-i', '--campaign_ids', nargs='+', type=str, |
| 92 | + required=True, |
| 93 | + help='The campaign IDs to receive the label.') |
| 94 | + args = parser.parse_args() |
| 95 | + main(google_ads_client, args.customer_id, args.label_id, args.campaign_ids) |
0 commit comments