|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2018 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This example gets the changes in the account made in the last 7 days.""" |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | + |
| 21 | +import argparse |
| 22 | +import sys |
| 23 | + |
| 24 | +import six |
| 25 | + |
| 26 | +import google.ads.google_ads.client |
| 27 | + |
| 28 | + |
| 29 | +ADS_PAGE_SIZE = 1000 |
| 30 | + |
| 31 | + |
| 32 | +def resource_name_for_resource_type(resource_type, row): |
| 33 | + """Return the resource name for the resource type. |
| 34 | +
|
| 35 | + Each returned row contains all possible changed fields. This function |
| 36 | + returns the resource name of the changed field based on the |
| 37 | + resource type. The changed field's parent is also populated but is not used. |
| 38 | + |
| 39 | + Args: |
| 40 | + resource_type: the string equivalent of the resource type |
| 41 | + row: a single row returned from the service |
| 42 | +
|
| 43 | + Returns: |
| 44 | + The resource name of the field that changed. |
| 45 | + """ |
| 46 | + resource_name = '' # default for UNSPECIFIED or UNKNOWN |
| 47 | + if resource_type == 'AD_GROUP': |
| 48 | + resource_name = row.change_status.ad_group.value |
| 49 | + elif resource_type == 'AD_GROUP_AD': |
| 50 | + resource_name = row.change_status.ad_group_ad.value |
| 51 | + elif resource_type == 'AD_GROUP_CRITERION': |
| 52 | + resource_name = row.change_status.ad_group_criterion.value |
| 53 | + elif resource_type == 'CAMPAIGN': |
| 54 | + resource_name = row.change_status.campaign.value |
| 55 | + elif resource_type == 'CAMPAIGN_CRITERION': |
| 56 | + resource_name = row.change_status.campaign_criterion.value |
| 57 | + return resource_name |
| 58 | + |
| 59 | + |
| 60 | +def main(client, customer_id): |
| 61 | + ads_service = client.get_service('GoogleAdsService') |
| 62 | + query = ('SELECT change_status.resource_name, ' |
| 63 | + 'change_status.last_change_date_time, ' |
| 64 | + 'change_status.resource_type, ' |
| 65 | + 'change_status.campaign, ' |
| 66 | + 'change_status.ad_group, ' |
| 67 | + 'change_status.resource_status, ' |
| 68 | + 'change_status.ad_group_ad, ' |
| 69 | + 'change_status.ad_group_criterion, ' |
| 70 | + 'change_status.campaign_criterion ' |
| 71 | + 'FROM change_status ' |
| 72 | + 'WHERE change_status.last_change_date_time DURING LAST_7_DAYS ' |
| 73 | + 'ORDER BY change_status.last_change_date_time') |
| 74 | + |
| 75 | + response = ads_service.search(customer_id, query=query, |
| 76 | + page_size=ADS_PAGE_SIZE) |
| 77 | + |
| 78 | + resource_type_enum = (client.get_type('ChangeStatusResourceTypeEnum') |
| 79 | + .ChangeStatusResourceType) |
| 80 | + change_status_operation_enum = (client.get_type('ChangeStatusOperationEnum') |
| 81 | + .ChangeStatusOperation) |
| 82 | + |
| 83 | + try: |
| 84 | + for row in response: |
| 85 | + resource_type = (resource_type_enum.Name(row.change_status |
| 86 | + .resource_type)) |
| 87 | + resource_status = (change_status_operation_enum |
| 88 | + .Name(row.change_status.resource_status)) |
| 89 | + print ('On "%s", change status "%s" shows a resource type of "%s" ' |
| 90 | + 'with resource name "%s" was "%s".' |
| 91 | + % (row.change_status.last_change_date_time.value, |
| 92 | + row.change_status.resource_name, |
| 93 | + resource_type, |
| 94 | + resource_name_for_resource_type(resource_type, row), |
| 95 | + resource_status)) |
| 96 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 97 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 98 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 99 | + for error in ex.failure.errors: |
| 100 | + print('\tError with message "%s".' % error.message) |
| 101 | + if error.location: |
| 102 | + for field_path_element in error.location.field_path_elements: |
| 103 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + # GoogleAdsClient will read a google-ads.yaml configuration file in the |
| 109 | + # home directory if none is specified. |
| 110 | + google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
| 111 | + .load_from_storage()) |
| 112 | + |
| 113 | + parser = argparse.ArgumentParser( |
| 114 | + description=('Displays account changes that occurred in the last 7 days.')) |
| 115 | + # The following argument(s) should be provided to run the example. |
| 116 | + parser.add_argument('-c', '--customer_id', type=six.text_type, |
| 117 | + required=True, help='The Google Ads customer ID.') |
| 118 | + args = parser.parse_args() |
| 119 | + |
| 120 | + main(google_ads_client, args.customer_id) |
0 commit comments