|
| 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 | +"""Demonstrates how to find and remove shared sets, and shared set criteria.""" |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | + |
| 18 | +import argparse |
| 19 | +import six |
| 20 | +import sys |
| 21 | + |
| 22 | +import google.ads.google_ads.client |
| 23 | + |
| 24 | + |
| 25 | +_DEFAULT_PAGE_SIZE = 1000 |
| 26 | + |
| 27 | + |
| 28 | +def main(client, customer_id, page_size, campaign_id): |
| 29 | + ga_service = client.get_service('GoogleAdsService') |
| 30 | + shared_criterion_service = client.get_service('SharedCriterionService') |
| 31 | + |
| 32 | + # First, retrieve all shared sets associated with the campaign. |
| 33 | + shared_sets_query = ( |
| 34 | + 'SELECT shared_set.id, shared_set.name FROM campaign_shared_set ' |
| 35 | + 'WHERE campaign.id = %s' % campaign_id) |
| 36 | + |
| 37 | + try: |
| 38 | + shared_set_response = ga_service.search( |
| 39 | + customer_id, query=shared_sets_query, page_size=page_size) |
| 40 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 41 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 42 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 43 | + for error in ex.failure.errors: |
| 44 | + print('\tError with message "%s".' % error.message) |
| 45 | + if error.location: |
| 46 | + for field_path_element in error.location.field_path_elements: |
| 47 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + shared_set_ids = [] |
| 51 | + for row in shared_set_response: |
| 52 | + shared_set = row.shared_set |
| 53 | + shared_set_id = str(shared_set.id.value) |
| 54 | + shared_set_ids.append(shared_set_id) |
| 55 | + print('Campaign shared set ID "%s" and name "%s" was found.' |
| 56 | + % (shared_set_id, shared_set.name.value)) |
| 57 | + |
| 58 | + # Next, retrieve shared criteria for all found shared sets. |
| 59 | + shared_criteria_query = ( |
| 60 | + 'SELECT shared_criterion.type, shared_criterion.keyword.text, ' |
| 61 | + 'shared_criterion.keyword.match_type, shared_set.id ' |
| 62 | + 'FROM shared_criterion WHERE shared_set.id IN (%s)' |
| 63 | + % ', '.join(shared_set_ids)) |
| 64 | + |
| 65 | + try: |
| 66 | + shared_criteria_response = ga_service.search( |
| 67 | + customer_id, query=shared_criteria_query, page_size=page_size) |
| 68 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 69 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 70 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 71 | + for error in ex.failure.errors: |
| 72 | + print('\tError with message "%s".' % error.message) |
| 73 | + if error.location: |
| 74 | + for field_path_element in error.location.field_path_elements: |
| 75 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + # Use the enum type to determine the enum name from the value. |
| 79 | + keyword_match_type_enum = ( |
| 80 | + client.get_type('KeywordMatchTypeEnum').KeywordMatchType) |
| 81 | + |
| 82 | + criterion_ids = [] |
| 83 | + for row in shared_criteria_response: |
| 84 | + shared_criterion = row.shared_criterion |
| 85 | + shared_criterion_resource_name = shared_criterion.resource_name |
| 86 | + if (shared_criterion.type == |
| 87 | + client.get_type('CriterionTypeEnum').KEYWORD): |
| 88 | + keyword = shared_criterion.keyword |
| 89 | + print('Shared criterion with resource name "%s" for negative ' |
| 90 | + 'keyword with text "%s" and match type "%s" was found.' |
| 91 | + % (shared_criterion_resource_name, keyword.text.value, |
| 92 | + keyword_match_type_enum.Name(keyword.match_type))) |
| 93 | + criterion_ids.append(shared_criterion_resource_name) |
| 94 | + |
| 95 | + operations = [] |
| 96 | + |
| 97 | + # Finally, remove the criteria. |
| 98 | + for criteria_id in criterion_ids: |
| 99 | + shared_criterion_operation = client.get_type('SharedCriterionOperation') |
| 100 | + shared_criterion_operation.remove = criteria_id |
| 101 | + operations.append(shared_criterion_operation) |
| 102 | + |
| 103 | + try: |
| 104 | + response = shared_criterion_service.mutate_shared_criteria( |
| 105 | + customer_id, operations) |
| 106 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 107 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 108 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 109 | + for error in ex.failure.errors: |
| 110 | + print('\tError with message "%s".' % error.message) |
| 111 | + if error.location: |
| 112 | + for field_path_element in error.location.field_path_elements: |
| 113 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + for result in response.results: |
| 117 | + print('Removed shared criterion "%s".' % result.resource_name) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + # GoogleAdsClient will read a google-ads.yaml configuration file in the |
| 122 | + # home directory if none is specified. |
| 123 | + google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
| 124 | + .load_from_storage()) |
| 125 | + |
| 126 | + parser = argparse.ArgumentParser( |
| 127 | + description=('Finds shared sets, then finds and removes shared set ' |
| 128 | + 'criteria under them.')) |
| 129 | + # The following argument(s) should be provided to run the example. |
| 130 | + parser.add_argument('-c', '--customer_id', type=six.text_type, |
| 131 | + required=True, help='The AdWords customer ID.') |
| 132 | + parser.add_argument('-i', '--campaign_id', type=six.text_type, |
| 133 | + required=True, help='The campaign ID.') |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE, |
| 137 | + args.campaign_id) |
0 commit comments