|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""This example illustrates how to create a new customer under a given |
| 15 | +manager account. |
| 16 | +
|
| 17 | +Note: this example must be run using the credentials of a Google Ads manager |
| 18 | +account. By default, the new account will only be accessible via the manager |
| 19 | +account. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import absolute_import |
| 23 | + |
| 24 | +import argparse |
| 25 | +import six |
| 26 | +import sys |
| 27 | +from datetime import datetime |
| 28 | + |
| 29 | +import google.ads.google_ads.client |
| 30 | + |
| 31 | + |
| 32 | +def main(client, manager_customer_id): |
| 33 | + customer_service = client.get_service('CustomerService') |
| 34 | + customer = client.get_type('Customer') |
| 35 | + today = datetime.today().strftime('%Y%m%d %H:%M:%S') |
| 36 | + customer.descriptive_name.value = ('Account created with ' |
| 37 | + 'CustomerService on %s' % today) |
| 38 | + # For a list of valid currency codes and time zones see this documentation: |
| 39 | + # https://developers.google.com/adwords/api/docs/appendix/codes-formats |
| 40 | + customer.currency_code.value = 'USD' |
| 41 | + customer.time_zone.value = 'America/New_York' |
| 42 | + # The below values are optional. For more information about URL |
| 43 | + # options see: https://support.google.com/google-ads/answer/6305348 |
| 44 | + customer.tracking_url_template.value = '{lpurl}?device={device}' |
| 45 | + customer.final_url_suffix.value = ('keyword={keyword}&matchtype={matchtype}' |
| 46 | + '&adgroupid={adgroupid}') |
| 47 | + customer.has_partners_badge.value = False |
| 48 | + |
| 49 | + try: |
| 50 | + response = customer_service.create_customer_client( |
| 51 | + manager_customer_id, customer) |
| 52 | + print(('Customer created with resource name "%s" under manager account ' |
| 53 | + 'with customer ID "%s"') % |
| 54 | + (response.resource_name, manager_customer_id)) |
| 55 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 56 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 57 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 58 | + for error in ex.failure.errors: |
| 59 | + print('\tError with message "%s".' % error.message) |
| 60 | + if error.location: |
| 61 | + for field_path_element in error.location.field_path_elements: |
| 62 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 68 | + # home directory if none is specified. |
| 69 | + google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
| 70 | + .load_from_storage()) |
| 71 | + |
| 72 | + parser = argparse.ArgumentParser( |
| 73 | + description=('Creates a new client under the given manager.')) |
| 74 | + # The following argument(s) should be provided to run the example. |
| 75 | + parser.add_argument('-m', '--manager_customer_id', type=six.text_type, |
| 76 | + required=True, help='A Google Ads customer ID for the ' |
| 77 | + 'manager account under which the new customer will ' |
| 78 | + 'be created.') |
| 79 | + args = parser.parse_args() |
| 80 | + |
| 81 | + main(google_ads_client, args.manager_customer_id) |
0 commit comments