|
| 1 | +# Copyright 2019 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 gets Hotel ads performance statistics for the 50 Hotel ad |
| 15 | +groups with the most impressions over the last 7 days. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | + |
| 20 | +import argparse |
| 21 | +import six |
| 22 | +import sys |
| 23 | + |
| 24 | +import google.ads.google_ads.client |
| 25 | + |
| 26 | + |
| 27 | +_DEFAULT_PAGE_SIZE = 50 |
| 28 | + |
| 29 | + |
| 30 | +def main(client, customer_id, page_size): |
| 31 | + ga_service = client.get_service('GoogleAdsService') |
| 32 | + |
| 33 | + query = ('SELECT campaign.id, campaign.advertising_channel_type, ' |
| 34 | + 'ad_group.id, ad_group.status, metrics.impressions, ' |
| 35 | + 'metrics.hotel_average_lead_value_micros, ' |
| 36 | + 'segments.hotel_check_in_day_of_week, ' |
| 37 | + 'segments.hotel_length_of_stay ' |
| 38 | + 'FROM hotel_performance_view ' |
| 39 | + 'WHERE segments.date DURING LAST_7_DAYS ' |
| 40 | + 'AND campaign.advertising_channel_type = \'HOTEL\' ' |
| 41 | + 'AND ad_group.status = \'ENABLED\' ' |
| 42 | + 'ORDER BY metrics.impressions DESC ' |
| 43 | + 'LIMIT 50') |
| 44 | + |
| 45 | + response = ga_service.search(customer_id, query, page_size=page_size) |
| 46 | + |
| 47 | + try: |
| 48 | + for row in response: |
| 49 | + campaign = row.campaign |
| 50 | + ad_group = row.ad_group |
| 51 | + hotel_check_in_day_of_week = row.hotel_check_in_day_of_week |
| 52 | + hotel_length_of_stay = row.hotel_length_of_stay |
| 53 | + metrics = row.metrics |
| 54 | + |
| 55 | + print('Ad group ID "%s" in campaign ID "%s" ' % (ad_group.id.value, |
| 56 | + campaign.id.value)) |
| 57 | + print('with hotel check-in on "%s" and "%s" day(s) stay ' % ( |
| 58 | + hotel_check_in_day_of_week, hotel_length_of_stay.value)) |
| 59 | + print('had %d impression(s) and %d average lead value (in micros) ' |
| 60 | + 'during the last 7 days.\n' % (metrics.impressions.value, |
| 61 | + metrics.hotel_average_lead_value_micros.value)) |
| 62 | + |
| 63 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 64 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 65 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 66 | + for error in ex.failure.errors: |
| 67 | + print('\tError with message "%s".' % error.message) |
| 68 | + if error.location: |
| 69 | + for field_path_element in error.location.field_path_elements: |
| 70 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 76 | + # home directory if none is specified. |
| 77 | + google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
| 78 | + .load_from_storage()) |
| 79 | + |
| 80 | + parser = argparse.ArgumentParser( |
| 81 | + description=('Retrieves Hotel-ads performance statistics.')) |
| 82 | + # The following argument(s) should be provided to run the example. |
| 83 | + parser.add_argument('-c', '--customer_id', type=six.text_type, |
| 84 | + required=True, help='The Google Ads customer ID.') |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE) |
0 commit comments