|
28 | 28 |
|
29 | 29 |
|
30 | 30 | def resource_name_for_resource_type(resource_type, row): |
31 | | - """Return the resource name for the resource type. |
| 31 | + """Return the resource name for the resource type. |
32 | 32 |
|
33 | 33 | Each returned row contains all possible changed fields. This function |
34 | 34 | returns the resource name of the changed field based on the |
35 | 35 | resource type. The changed field's parent is also populated but is not used. |
36 | | - |
| 36 | +
|
37 | 37 | Args: |
38 | 38 | resource_type: the string equivalent of the resource type |
39 | 39 | row: a single row returned from the service |
40 | 40 |
|
41 | 41 | Returns: |
42 | 42 | The resource name of the field that changed. |
43 | 43 | """ |
44 | | - resource_name = '' # default for UNSPECIFIED or UNKNOWN |
45 | | - if resource_type == 'AD_GROUP': |
46 | | - resource_name = row.change_status.ad_group.value |
47 | | - elif resource_type == 'AD_GROUP_AD': |
48 | | - resource_name = row.change_status.ad_group_ad.value |
49 | | - elif resource_type == 'AD_GROUP_CRITERION': |
50 | | - resource_name = row.change_status.ad_group_criterion.value |
51 | | - elif resource_type == 'CAMPAIGN': |
52 | | - resource_name = row.change_status.campaign.value |
53 | | - elif resource_type == 'CAMPAIGN_CRITERION': |
54 | | - resource_name = row.change_status.campaign_criterion.value |
55 | | - return resource_name |
| 44 | + resource_name = "" # default for UNSPECIFIED or UNKNOWN |
| 45 | + if resource_type == "AD_GROUP": |
| 46 | + resource_name = row.change_status.ad_group |
| 47 | + elif resource_type == "AD_GROUP_AD": |
| 48 | + resource_name = row.change_status.ad_group_ad |
| 49 | + elif resource_type == "AD_GROUP_CRITERION": |
| 50 | + resource_name = row.change_status.ad_group_criterion |
| 51 | + elif resource_type == "CAMPAIGN": |
| 52 | + resource_name = row.change_status.campaign |
| 53 | + elif resource_type == "CAMPAIGN_CRITERION": |
| 54 | + resource_name = row.change_status.campaign_criterion |
| 55 | + return resource_name |
56 | 56 |
|
57 | 57 |
|
58 | 58 | def main(client, customer_id): |
59 | | - ads_service = client.get_service('GoogleAdsService', version='v4') |
60 | | - query = ('SELECT change_status.resource_name, ' |
61 | | - 'change_status.last_change_date_time, ' |
62 | | - 'change_status.resource_type, ' |
63 | | - 'change_status.campaign, ' |
64 | | - 'change_status.ad_group, ' |
65 | | - 'change_status.resource_status, ' |
66 | | - 'change_status.ad_group_ad, ' |
67 | | - 'change_status.ad_group_criterion, ' |
68 | | - 'change_status.campaign_criterion ' |
69 | | - 'FROM change_status ' |
70 | | - 'WHERE change_status.last_change_date_time DURING LAST_7_DAYS ' |
71 | | - 'ORDER BY change_status.last_change_date_time') |
72 | | - |
73 | | - response = ads_service.search(customer_id, query=query, |
74 | | - page_size=ADS_PAGE_SIZE) |
75 | | - |
76 | | - resource_type_enum = (client.get_type( |
77 | | - 'ChangeStatusResourceTypeEnum', version='v4').ChangeStatusResourceType) |
78 | | - change_status_operation_enum = (client.get_type( |
79 | | - 'ChangeStatusOperationEnum', version='v4').ChangeStatusOperation) |
80 | | - |
81 | | - try: |
82 | | - for row in response: |
83 | | - resource_type = (resource_type_enum.Name(row.change_status |
84 | | - .resource_type)) |
85 | | - resource_status = (change_status_operation_enum |
86 | | - .Name(row.change_status.resource_status)) |
87 | | - print ('On "%s", change status "%s" shows a resource type of "%s" ' |
88 | | - 'with resource name "%s" was "%s".' |
89 | | - % (row.change_status.last_change_date_time.value, |
90 | | - row.change_status.resource_name, |
91 | | - resource_type, |
92 | | - resource_name_for_resource_type(resource_type, row), |
93 | | - resource_status)) |
94 | | - except google.ads.google_ads.errors.GoogleAdsException as ex: |
95 | | - print('Request with ID "%s" failed with status "%s" and includes the ' |
96 | | - 'following errors:' % (ex.request_id, ex.error.code().name)) |
97 | | - for error in ex.failure.errors: |
98 | | - print('\tError with message "%s".' % error.message) |
99 | | - if error.location: |
100 | | - for field_path_element in error.location.field_path_elements: |
101 | | - print('\t\tOn field: %s' % field_path_element.field_name) |
102 | | - sys.exit(1) |
103 | | - |
104 | | - |
105 | | -if __name__ == '__main__': |
106 | | - # GoogleAdsClient will read a google-ads.yaml configuration file in the |
107 | | - # home directory if none is specified. |
108 | | - google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
109 | | - .load_from_storage()) |
110 | | - |
111 | | - parser = argparse.ArgumentParser( |
112 | | - description=('Displays account changes that occurred in the last 7 days.')) |
113 | | - # The following argument(s) should be provided to run the example. |
114 | | - parser.add_argument('-c', '--customer_id', type=str, |
115 | | - required=True, help='The Google Ads customer ID.') |
116 | | - args = parser.parse_args() |
117 | | - |
118 | | - main(google_ads_client, args.customer_id) |
| 59 | + ads_service = client.get_service("GoogleAdsService", version="v5") |
| 60 | + query = ( |
| 61 | + "SELECT change_status.resource_name, " |
| 62 | + "change_status.last_change_date_time, " |
| 63 | + "change_status.resource_type, " |
| 64 | + "change_status.campaign, " |
| 65 | + "change_status.ad_group, " |
| 66 | + "change_status.resource_status, " |
| 67 | + "change_status.ad_group_ad, " |
| 68 | + "change_status.ad_group_criterion, " |
| 69 | + "change_status.campaign_criterion " |
| 70 | + "FROM change_status " |
| 71 | + "WHERE change_status.last_change_date_time DURING LAST_7_DAYS " |
| 72 | + "ORDER BY change_status.last_change_date_time" |
| 73 | + ) |
| 74 | + |
| 75 | + response = ads_service.search( |
| 76 | + customer_id, query=query, page_size=ADS_PAGE_SIZE |
| 77 | + ) |
| 78 | + |
| 79 | + resource_type_enum = client.get_type( |
| 80 | + "ChangeStatusResourceTypeEnum", version="v5" |
| 81 | + ).ChangeStatusResourceType |
| 82 | + change_status_operation_enum = client.get_type( |
| 83 | + "ChangeStatusOperationEnum", version="v5" |
| 84 | + ).ChangeStatusOperation |
| 85 | + |
| 86 | + try: |
| 87 | + for row in response: |
| 88 | + resource_type = resource_type_enum.Name( |
| 89 | + row.change_status.resource_type |
| 90 | + ) |
| 91 | + resource_status = change_status_operation_enum.Name( |
| 92 | + row.change_status.resource_status |
| 93 | + ) |
| 94 | + print( |
| 95 | + 'On "%s", change status "%s" shows a resource type of "%s" ' |
| 96 | + 'with resource name "%s" was "%s".' |
| 97 | + % ( |
| 98 | + row.change_status.last_change_date_time.value, |
| 99 | + row.change_status.resource_name, |
| 100 | + resource_type, |
| 101 | + resource_name_for_resource_type(resource_type, row), |
| 102 | + resource_status, |
| 103 | + ) |
| 104 | + ) |
| 105 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 106 | + print( |
| 107 | + 'Request with ID "%s" failed with status "%s" and includes the ' |
| 108 | + "following errors:" % (ex.request_id, ex.error.code().name) |
| 109 | + ) |
| 110 | + for error in ex.failure.errors: |
| 111 | + print('\tError with message "%s".' % error.message) |
| 112 | + if error.location: |
| 113 | + for field_path_element in error.location.field_path_elements: |
| 114 | + print("\t\tOn field: %s" % field_path_element.field_name) |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + # GoogleAdsClient will read a google-ads.yaml configuration file in the |
| 120 | + # home directory if none is specified. |
| 121 | + google_ads_client = ( |
| 122 | + google.ads.google_ads.client.GoogleAdsClient.load_from_storage() |
| 123 | + ) |
| 124 | + |
| 125 | + parser = argparse.ArgumentParser( |
| 126 | + description=( |
| 127 | + "Displays account changes that occurred in the last 7 days." |
| 128 | + ) |
| 129 | + ) |
| 130 | + # The following argument(s) should be provided to run the example. |
| 131 | + parser.add_argument( |
| 132 | + "-c", |
| 133 | + "--customer_id", |
| 134 | + type=str, |
| 135 | + required=True, |
| 136 | + help="The Google Ads customer ID.", |
| 137 | + ) |
| 138 | + args = parser.parse_args() |
| 139 | + |
| 140 | + main(google_ads_client, args.customer_id) |
0 commit comments