From 0c428951d1bfe769da9871edc361ee0679c94a04 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Wed, 6 Sep 2023 10:44:15 -0500 Subject: [PATCH] Code examples cleanup --- .../get_account_information.py | 88 --------- .../get_pending_invitations.py | 95 --------- examples/basic_operations/add_keywords.py | 114 ----------- .../add_responsive_search_ad.py | 125 ------------ examples/basic_operations/get_ad_groups.py | 100 ---------- .../basic_operations/get_expanded_text_ads.py | 96 ---------- examples/basic_operations/get_keywords.py | 139 -------------- examples/basic_operations/remove_ad.py | 78 -------- examples/basic_operations/remove_ad_group.py | 72 ------- examples/basic_operations/remove_keyword.py | 85 -------- examples/basic_operations/update_keyword.py | 91 --------- .../billing/get_account_budget_proposals.py | 104 ---------- examples/billing/get_account_budgets.py | 140 -------------- examples/billing/get_billing_setup.py | 100 ---------- examples/billing/remove_billing_setup.py | 89 --------- .../add_campaign_bid_modifier.py | 129 ------------- .../campaign_management/add_campaign_draft.py | 91 --------- .../get_campaigns_by_label.py | 118 ------------ examples/misc/get_all_image_assets.py | 95 --------- examples/misc/get_all_videos_and_images.py | 84 -------- .../remarketing/add_remarketing_action.py | 166 ---------------- .../reporting/get_hotel_ads_performance.py | 108 ----------- examples/reporting/get_keyword_stats.py | 107 ----------- .../get_campaign_targeting_criteria.py | 101 ---------- ...arch_for_language_and_carrier_constants.py | 181 ------------------ 25 files changed, 2696 deletions(-) delete mode 100755 examples/account_management/get_account_information.py delete mode 100755 examples/account_management/get_pending_invitations.py delete mode 100755 examples/basic_operations/add_keywords.py delete mode 100755 examples/basic_operations/add_responsive_search_ad.py delete mode 100755 examples/basic_operations/get_ad_groups.py delete mode 100755 examples/basic_operations/get_expanded_text_ads.py delete mode 100755 examples/basic_operations/get_keywords.py delete mode 100755 examples/basic_operations/remove_ad.py delete mode 100755 examples/basic_operations/remove_ad_group.py delete mode 100755 examples/basic_operations/remove_keyword.py delete mode 100755 examples/basic_operations/update_keyword.py delete mode 100755 examples/billing/get_account_budget_proposals.py delete mode 100755 examples/billing/get_account_budgets.py delete mode 100755 examples/billing/get_billing_setup.py delete mode 100755 examples/billing/remove_billing_setup.py delete mode 100755 examples/campaign_management/add_campaign_bid_modifier.py delete mode 100755 examples/campaign_management/add_campaign_draft.py delete mode 100755 examples/campaign_management/get_campaigns_by_label.py delete mode 100755 examples/misc/get_all_image_assets.py delete mode 100755 examples/misc/get_all_videos_and_images.py delete mode 100755 examples/remarketing/add_remarketing_action.py delete mode 100755 examples/reporting/get_hotel_ads_performance.py delete mode 100755 examples/reporting/get_keyword_stats.py delete mode 100755 examples/targeting/get_campaign_targeting_criteria.py delete mode 100644 examples/targeting/search_for_language_and_carrier_constants.py diff --git a/examples/account_management/get_account_information.py b/examples/account_management/get_account_information.py deleted file mode 100755 index 497d6e689..000000000 --- a/examples/account_management/get_account_information.py +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example lists information about an advertising account. - -For example, its name, currency, time zone, etc. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id): - ga_service = client.get_service("GoogleAdsService") - query = """ - SELECT - customer.id, - customer.descriptive_name, - customer.currency_code, - customer.time_zone, - customer.tracking_url_template, - customer.auto_tagging_enabled - FROM customer - LIMIT 1""" - - request = client.get_type("SearchGoogleAdsRequest") - request.customer_id = customer_id - request.query = query - response = ga_service.search(request=request) - customer = list(response)[0].customer - - print(f"Customer ID: {customer.id}") - print(f"\tDescriptive name: {customer.descriptive_name}") - print(f"\tCurrency code: {customer.currency_code}") - print(f"\tTime zone: {customer.time_zone}") - print(f"\tTracking URL template: {customer.tracking_url_template}") - print(f"\tAuto tagging enabled: {customer.auto_tagging_enabled}") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Displays basic information about the specified " - "customer's advertising account." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/account_management/get_pending_invitations.py b/examples/account_management/get_pending_invitations.py deleted file mode 100755 index d11e598a4..000000000 --- a/examples/account_management/get_pending_invitations.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This code example retrieves pending invitations for a customer account. - -To create a pending invitation, see the invite_user_with_access_role.py -example. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id): - """The main method that creates all necessary entities for the example. - - Args: - client: An initialized GoogleAdsClient instance. - customer_id: The client customer ID str. - """ - googleads_service = client.get_service("GoogleAdsService") - # [START get_pending_invitations] - query = """ - SELECT - customer_user_access_invitation.invitation_id, - customer_user_access_invitation.email_address, - customer_user_access_invitation.access_role, - customer_user_access_invitation.creation_date_time - FROM customer_user_access_invitation - WHERE customer_user_access_invitation.invitation_status = PENDING""" - - stream = googleads_service.search_stream( - customer_id=customer_id, query=query - ) - for batch in stream: - for row in batch.results: - invite = row.customer_user_access_invitation - print( - "A pending invitation with " - f"invitation ID: '{invite.invitation_id}', " - f"email address: {invite.email_address}, " - f"access role: {invite.access_role}, and " - f"created on: {invite.creation_date_time} was found." - ) - - -# [END get_pending_invitations] - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=("Retrieves pending invitations for a customer account.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/add_keywords.py b/examples/basic_operations/add_keywords.py deleted file mode 100755 index 3d22e72e9..000000000 --- a/examples/basic_operations/add_keywords.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example demonstrates how to add a keyword to an ad group.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id, keyword_text): - ad_group_service = client.get_service("AdGroupService") - ad_group_criterion_service = client.get_service("AdGroupCriterionService") - - # Create keyword. - ad_group_criterion_operation = client.get_type("AdGroupCriterionOperation") - ad_group_criterion = ad_group_criterion_operation.create - ad_group_criterion.ad_group = ad_group_service.ad_group_path( - customer_id, ad_group_id - ) - ad_group_criterion.status = client.enums.AdGroupCriterionStatusEnum.ENABLED - ad_group_criterion.keyword.text = keyword_text - ad_group_criterion.keyword.match_type = ( - client.enums.KeywordMatchTypeEnum.EXACT - ) - - # Optional field - # All fields can be referenced from the protos directly. - # The protos are located in subdirectories under: - # https://github.com/googleapis/googleapis/tree/master/google/ads/googleads - # ad_group_criterion.negative = True - - # Optional repeated field - # ad_group_criterion.final_urls.append('https://www.example.com') - - # Add keyword - ad_group_criterion_response = ad_group_criterion_service.mutate_ad_group_criteria( - customer_id=customer_id, operations=[ad_group_criterion_operation], - ) - - print( - "Created keyword " - f"{ad_group_criterion_response.results[0].resource_name}." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Adds a keyword to the provided ad group, for the " - "specified customer." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - parser.add_argument( - "-k", - "--keyword_text", - type=str, - required=False, - default="mars cruise", - help=( - "The keyword to be added to the ad group. Note " - "that you will receive an error response if you " - "attempt to create a duplicate keyword." - ), - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.ad_group_id, - args.keyword_text, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/add_responsive_search_ad.py b/examples/basic_operations/add_responsive_search_ad.py deleted file mode 100755 index a37b8efa5..000000000 --- a/examples/basic_operations/add_responsive_search_ad.py +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example adds a responsive search ad to a given ad group. - -To get ad groups, run get_ad_groups.py. -""" - - -import argparse -import sys -from uuid import uuid4 - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id): - ad_group_ad_service = client.get_service("AdGroupAdService") - ad_group_service = client.get_service("AdGroupService") - - # Create the ad group ad. - ad_group_ad_operation = client.get_type("AdGroupAdOperation") - ad_group_ad = ad_group_ad_operation.create - ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED - ad_group_ad.ad_group = ad_group_service.ad_group_path( - customer_id, ad_group_id - ) - - # Set responsive search ad info. - ad_group_ad.ad.final_urls.append("http://www.example.com") - - # Set a pinning to always choose this asset for HEADLINE_1. Pinning is - # optional; if no pinning is set, then headlines and descriptions will be - # rotated and the ones that perform best will be used more often. - served_asset_enum = client.enums.ServedAssetFieldTypeEnum.HEADLINE_1 - pinned_headline = create_ad_text_asset( - client, f"Cruise to Mars #{str(uuid4())[:8]}", served_asset_enum - ) - - ad_group_ad.ad.responsive_search_ad.headlines.extend( - [ - pinned_headline, - create_ad_text_asset(client, "Best Space Cruise Line"), - create_ad_text_asset(client, "Experience the Stars"), - ] - ) - ad_group_ad.ad.responsive_search_ad.descriptions.extend( - [ - create_ad_text_asset(client, "Buy your tickets now"), - create_ad_text_asset(client, "Visit the Red Planet"), - ] - ) - ad_group_ad.ad.responsive_search_ad.path1 = "all-inclusive" - ad_group_ad.ad.responsive_search_ad.path2 = "deals" - - # Send a request to the server to add a responsive search ad. - ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads( - customer_id=customer_id, operations=[ad_group_ad_operation] - ) - - for result in ad_group_ad_response.results: - print( - f"Created responsive search ad with resource name " - f'"{result.resource_name}".' - ) - - -def create_ad_text_asset(client, text, pinned_field=None): - """Create an AdTextAsset.""" - ad_text_asset = client.get_type("AdTextAsset") - ad_text_asset.text = text - if pinned_field: - ad_text_asset.pinned_field = pinned_field - return ad_text_asset - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Adds an expanded text ad to the specified ad group ID, " - "for the given customer ID." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.ad_group_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/get_ad_groups.py b/examples/basic_operations/get_ad_groups.py deleted file mode 100755 index d3996be02..000000000 --- a/examples/basic_operations/get_ad_groups.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to retrieve ad groups.""" - - -import argparse -import sys -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -_DEFAULT_PAGE_SIZE = 1000 - - -def main(client, customer_id, page_size, campaign_id=None): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - campaign.id, - ad_group.id, - ad_group.name - FROM ad_group""" - - if campaign_id: - query += f" WHERE campaign.id = {campaign_id}" - - search_request = client.get_type("SearchGoogleAdsRequest") - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = _DEFAULT_PAGE_SIZE - - results = ga_service.search(request=search_request) - - for row in results: - print( - f"Ad group with ID {row.ad_group.id} and name " - f'"{row.ad_group.name}" was found in campaign with ' - f"ID {row.campaign.id}." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="List ad groups for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-i", - "--campaign_id", - type=str, - required=False, - help=( - "The campaign ID. Specify this to list ad groups " - "solely for this campaign ID." - ), - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - _DEFAULT_PAGE_SIZE, - campaign_id=args.campaign_id, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/get_expanded_text_ads.py b/examples/basic_operations/get_expanded_text_ads.py deleted file mode 100755 index b4ede4fb2..000000000 --- a/examples/basic_operations/get_expanded_text_ads.py +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to retrieve expanded text ads.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id=None): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - ad_group.id, - ad_group_ad.ad.id, - ad_group_ad.ad.expanded_text_ad.headline_part1, - ad_group_ad.ad.expanded_text_ad.headline_part2, - ad_group_ad.status - FROM ad_group_ad - WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD""" - - if ad_group_id: - query += f" AND ad_group.id = {ad_group_id}" - - stream = ga_service.search_stream(customer_id=customer_id, query=query) - - for batch in stream: - for row in batch.results: - ad = row.ad_group_ad.ad - - if ad.expanded_text_ad: - expanded_text_ad_info = ad.expanded_text_ad - - print( - f"Expanded text ad with ID {ad.id}, status " - f'"{row.ad_group_ad.status.name}", and headline ' - f'"{expanded_text_ad_info.headline_part1}" - ' - f'"{expanded_text_ad_info.headline_part2}" was ' - f"found in ad group with ID {row.ad_group.id}." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="List ad groups for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", - "--ad_group_id", - type=str, - required=False, - help="The ad group ID. ", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, ad_group_id=args.ad_group_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/get_keywords.py b/examples/basic_operations/get_keywords.py deleted file mode 100755 index 182ce8c6c..000000000 --- a/examples/basic_operations/get_keywords.py +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example retrieves keywords for a customer for a specific ad group. - -It also demonstrates how to use the omit_unselected_resource_names option in -GAQL to reduce payload size. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -_DEFAULT_PAGE_SIZE = 1000 - - -def main( - client, customer_id, page_size, ad_group_id, omit_unselected_resource_names -): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - ad_group.id, - ad_group_criterion.type, - ad_group_criterion.criterion_id, - ad_group_criterion.keyword.text, - ad_group_criterion.keyword.match_type - FROM ad_group_criterion - WHERE ad_group_criterion.type = KEYWORD""" - - if ad_group_id: - query += f" AND ad_group.id = {ad_group_id}" - - # Adds omit_unselected_resource_names=true to the PARAMETERS clause of the - # Google Ads Query Language (GAQL) query, which excludes the resource names - # of all resources that aren't explicitly requested in the SELECT clause. - # Enabling this option reduces payload size, but if you plan to use a - # returned object in subsequent mutate operations, make sure you explicitly - # request its "resource_name" field in the SELECT clause. - # - # Read more about PARAMETERS: - # https://developers.google.com/google-ads/api/docs/query/structure#parameters - if omit_unselected_resource_names: - query += " PARAMETERS omit_unselected_resource_names=true" - - search_request = client.get_type("SearchGoogleAdsRequest") - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = page_size - - results = ga_service.search(request=search_request) - - for row in results: - ad_group = row.ad_group - ad_group_criterion = row.ad_group_criterion - keyword = row.ad_group_criterion.keyword - - if omit_unselected_resource_names: - resource_name_log_statement = "" - else: - resource_name_log_statement = ( - f" and resource name '{ad_group.resource_name}'" - ) - - print( - f'Keyword with text "{keyword.text}", match type ' - f"{keyword.match_type}, criteria type " - f"{ad_group_criterion.type_}, and ID " - f"{ad_group_criterion.criterion_id} was found in ad group " - f"with ID {ad_group.id}{resource_name_log_statement}." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Retrieves keywords for the specified customer, or " - "optionally for a specific ad group." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, help="The ad group ID." - ) - parser.add_argument( - "-o", - "--omit-unselected-resource-names", - type=bool, - default=False, - help="Whether or not the response should omit resource names.", - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - _DEFAULT_PAGE_SIZE, - args.ad_group_id, - args.omit_unselected_resource_names, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/remove_ad.py b/examples/basic_operations/remove_ad.py deleted file mode 100755 index d2826cc45..000000000 --- a/examples/basic_operations/remove_ad.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example removes an existing ad.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id, ad_id): - ad_group_ad_service = client.get_service("AdGroupAdService") - ad_group_ad_operation = client.get_type("AdGroupAdOperation") - - resource_name = ad_group_ad_service.ad_group_ad_path( - customer_id, ad_group_id, ad_id - ) - ad_group_ad_operation.remove = resource_name - - ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads( - customer_id=customer_id, operations=[ad_group_ad_operation] - ) - - print( - f"Removed ad group ad {ad_group_ad_response.results[0].resource_name}." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - parser = argparse.ArgumentParser( - description=("Removes an ad from the specified customer's ad group.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - parser.add_argument( - "-i", "--ad_id", type=str, required=True, help="The ad ID." - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.ad_group_id, args.ad_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/remove_ad_group.py b/examples/basic_operations/remove_ad_group.py deleted file mode 100755 index 8643afe92..000000000 --- a/examples/basic_operations/remove_ad_group.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example removes an existing ad group.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id): - ad_group_service = client.get_service("AdGroupService") - ad_group_operation = client.get_type("AdGroupOperation") - - resource_name = ad_group_service.ad_group_path(customer_id, ad_group_id) - ad_group_operation.remove = resource_name - - ad_group_response = ad_group_service.mutate_ad_groups( - customer_id=customer_id, operations=[ad_group_operation] - ) - - print(f"Removed ad group {ad_group_response.results[0].resource_name}.") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=("Removes an ad group for the specified customer.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.ad_group_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/remove_keyword.py b/examples/basic_operations/remove_keyword.py deleted file mode 100755 index e6ac93ce7..000000000 --- a/examples/basic_operations/remove_keyword.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example removes an existing keyword from an ad group.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id, criterion_id): - agc_service = client.get_service("AdGroupCriterionService") - agc_operation = client.get_type("AdGroupCriterionOperation") - - resource_name = agc_service.ad_group_criterion_path( - customer_id, ad_group_id, criterion_id - ) - agc_operation.remove = resource_name - - agc_response = agc_service.mutate_ad_group_criteria( - customer_id=customer_id, operations=[agc_operation] - ) - - print(f"Removed keyword {agc_response.results[0].resource_name}.") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - parser = argparse.ArgumentParser( - description=("Removes given campaign for the specified customer.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - parser.add_argument( - "-k", - "--criterion_id", - type=str, - required=True, - help="The criterion ID, or keyword ID.", - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.ad_group_id, - args.criterion_id, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/basic_operations/update_keyword.py b/examples/basic_operations/update_keyword.py deleted file mode 100755 index d7194664a..000000000 --- a/examples/basic_operations/update_keyword.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example updates a keyword for the specified ad group.""" - - -import argparse -import sys - -from google.api_core import protobuf_helpers -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, ad_group_id, criterion_id): - agc_service = client.get_service("AdGroupCriterionService") - ad_group_criterion_operation = client.get_type("AdGroupCriterionOperation") - - ad_group_criterion = ad_group_criterion_operation.update - ad_group_criterion.resource_name = agc_service.ad_group_criterion_path( - customer_id, ad_group_id, criterion_id - ) - ad_group_criterion.status = client.enums.AdGroupCriterionStatusEnum.ENABLED - ad_group_criterion.final_urls.append("https://www.example.com") - client.copy_from( - ad_group_criterion_operation.update_mask, - protobuf_helpers.field_mask(None, ad_group_criterion._pb), - ) - - agc_response = agc_service.mutate_ad_group_criteria( - customer_id=customer_id, operations=[ad_group_criterion_operation] - ) - print(f"Updated keyword {agc_response.results[0].resource_name}.") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - parser = argparse.ArgumentParser( - description=("Updates a keyword for the specified ad group.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-a", "--ad_group_id", type=str, required=True, help="The ad group ID." - ) - parser.add_argument( - "-k", - "--criterion_id", - type=str, - required=True, - help="The criterion ID, or keyword ID.", - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.ad_group_id, - args.criterion_id, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/billing/get_account_budget_proposals.py b/examples/billing/get_account_budget_proposals.py deleted file mode 100755 index 7a88f1731..000000000 --- a/examples/billing/get_account_budget_proposals.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to get all account budget proposals. - -To add an account budget proposal, run add_account_budget_proposal.py -""" - - -import argparse -import sys - - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -_DEFAULT_PAGE_SIZE = 1000 - - -def main(client, customer_id, page_size): - ga_service = client.get_service("GoogleAdsService") - - query = ( - "SELECT account_budget_proposal.id, " - "account_budget_proposal.account_budget," - "account_budget_proposal.billing_setup," - "account_budget_proposal.status," - "account_budget_proposal.proposed_name," - "account_budget_proposal.proposed_notes," - "account_budget_proposal.proposed_purchase_order_number," - "account_budget_proposal.proposal_type," - "account_budget_proposal.approval_date_time," - "account_budget_proposal.creation_date_time " - "FROM account_budget_proposal" - ) - - search_request = client.get_type("SearchGoogleAdsRequest") - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = page_size - - results = ga_service.search(request=search_request) - - for row in results: - budget_proposal = row.account_budget_proposal - - print( - 'Account budget proposal with ID "{budget_proposal.id}", ' - 'status "{budget_proposal.status.name}", ' - 'account_budget "{budget_proposal.account_budget}", ' - 'billing_setup "{budget_proposal.billing_setup}", ' - 'proposed_name "{budget_proposal.proposed_name}", ' - 'proposed_notes "{budget_proposal.proposed_notes}", ' - "proposed_po_number " - '"{budget_proposal.proposed_purchase_order_number}", ' - 'proposal_type "{budget_proposal.proposal_type.name}", ' - 'approval_date_time "{budget_proposal.approval_date_time}", ' - 'creation_date_time "{budget_proposal.creation_date_time}"' - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="Lists all account budget proposals." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, _DEFAULT_PAGE_SIZE) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/billing/get_account_budgets.py b/examples/billing/get_account_budgets.py deleted file mode 100755 index b99755368..000000000 --- a/examples/billing/get_account_budgets.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This illustrates how to get all account budgets for a Google Ads customer.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - account_budget.status, - account_budget.billing_setup, - account_budget.approved_spending_limit_micros, - account_budget.approved_spending_limit_type, - account_budget.proposed_spending_limit_micros, - account_budget.proposed_spending_limit_type, - account_budget.adjusted_spending_limit_micros, - account_budget.adjusted_spending_limit_type, - account_budget.approved_start_date_time, - account_budget.proposed_start_date_time, - account_budget.approved_end_date_time, - account_budget.approved_end_time_type, - account_budget.proposed_end_date_time, - account_budget.proposed_end_time_type - FROM account_budget""" - - stream = ga_service.search_stream(customer_id=customer_id, query=query) - - for batch in stream: - for row in batch.results: - budget = row.account_budget - - # Here and in the statements below, the variable is set to the - # name of the Enum as a default if the numeric value for the - # monetary or date fields is not present. - approved_spending_limit = ( - micros_to_currency(budget.approved_spending_limit_micros) - or budget.approved_spending_limit_type.name - ) - - proposed_spending_limit = ( - micros_to_currency(budget.proposed_spending_limit_micros) - or budget.proposed_spending_limit_type.name - ) - - adjusted_spending_limit = ( - micros_to_currency(budget.adjusted_spending_limit_micros) - or budget.adjusted_spending_limit_type.name - ) - - approved_end_date_time = ( - budget.approved_end_date_time - or budget.approved_end_time_type.name - ) - - proposed_end_date_time = ( - budget.proposed_end_date_time - or budget.proposed_end_time_type.name - ) - - amount_served = ( - micros_to_currency(budget.amount_served_micros) or 0.0 - ) - - total_adjustments = ( - micros_to_currency(budget.total_adjustments_micros) or 0.0 - ) - - print( - f'Account budget "{budget.resource_name}", ' - f'with status "{budget.status.name}", ' - f'billing setup "{budget.billing_setup}", ' - f"amount served {amount_served:.2f}, " - f"total adjustments {total_adjustments:.2f}, " - f'approved spending limit "{approved_spending_limit}" ' - f'(proposed "{proposed_spending_limit}" -- ' - f'adjusted "{adjusted_spending_limit}"), approved ' - f'start time "{budget.approved_start_date_time}" ' - f'(proposed "{budget.proposed_start_date_time}"), ' - f'approved end time "{approved_end_date_time}" ' - f'(proposed "{proposed_end_date_time}").' - ) - - -def micros_to_currency(micros): - return micros / 1000000.0 if micros is not None else None - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Lists all account budgets for given Google Ads customer ID." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/billing/get_billing_setup.py b/examples/billing/get_billing_setup.py deleted file mode 100755 index ca7748911..000000000 --- a/examples/billing/get_billing_setup.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Gets all billing setup objects available for the specified customer ID.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -# [START get_billing_setup] -def main(client, customer_id): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - billing_setup.id, - billing_setup.status, - billing_setup.payments_account, - billing_setup.payments_account_info.payments_account_id, - billing_setup.payments_account_info.payments_account_name, - billing_setup.payments_account_info.payments_profile_id, - billing_setup.payments_account_info.payments_profile_name, - billing_setup.payments_account_info.secondary_payments_profile_id - FROM billing_setup""" - - stream = ga_service.search_stream(customer_id=customer_id, query=query) - - print("Found the following billing setup results:") - for batch in stream: - for row in batch.results: - billing_setup = row.billing_setup - pai = billing_setup.payments_account_info - - if pai.secondary_payments_profile_id: - secondary_payments_profile_id = ( - pai.secondary_payments_profile_id - ) - else: - secondary_payments_profile_id = "None" - - print( - f"Billing setup with ID {billing_setup.id}, " - f'status "{billing_setup.status.name}", ' - f'payments_account "{billing_setup.payments_account}" ' - f"payments_account_id {pai.payments_account_id}, " - f'payments_account_name "{pai.payments_account_name}", ' - f"payments_profile_id {pai.payments_profile_id}, " - f'payments_profile_name "{pai.payments_profile_name}", ' - "secondary_payments_profile_id " - f"{secondary_payments_profile_id}." - ) - # [END get_billing_setup] - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="Lists all billing setup objects for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/billing/remove_billing_setup.py b/examples/billing/remove_billing_setup.py deleted file mode 100755 index 565186e1c..000000000 --- a/examples/billing/remove_billing_setup.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example removes a billing setup with the specified ID. - -To get available billing setups, run get_billing_setups.py. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -# [START remove_billing_setup] -def main(client, customer_id, billing_setup_id): - billing_setup_service = client.get_service("BillingSetupService") - - # Create billing setup operation. - billing_setup_operation = client.get_type("BillingSetupOperation") - billing_setup_operation.remove = billing_setup_service.billing_setup_path( - customer_id, billing_setup_id - ) - - # Remove the billing setup. - billing_setup_response = billing_setup_service.mutate_billing_setup( - customer_id=customer_id, operation=billing_setup_operation - ) - print( - "Removed billing setup " - f'"{billing_setup_response.results[0].resource_name}"' - ) - # [END remove_billing_setup] - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Removes billing setup for specified customer and billing " - "setup ID." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-b", - "--billing_setup_id", - type=str, - required=True, - help="The billing setup ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.billing_setup_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/campaign_management/add_campaign_bid_modifier.py b/examples/campaign_management/add_campaign_bid_modifier.py deleted file mode 100755 index 46c911219..000000000 --- a/examples/campaign_management/add_campaign_bid_modifier.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Demonstrates how to add a campaign-level bid modifier for call interactions. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, campaign_id, bid_modifier_value): - campaign_service = client.get_service("CampaignService") - campaign_bm_service = client.get_service("CampaignBidModifierService") - - # Create campaign bid modifier for call interactions with the specified - # campaign ID and bid modifier value. - campaign_bid_modifier_operation = client.get_type( - "CampaignBidModifierOperation" - ) - campaign_bid_modifier = campaign_bid_modifier_operation.create - - # Set the campaign. - campaign_bid_modifier.campaign = campaign_service.campaign_path( - customer_id, campaign_id - ) - - # Set the bid modifier. - campaign_bid_modifier.bid_modifier = bid_modifier_value - - # Sets the interaction type. - campaign_bid_modifier.interaction_type.type_ = ( - client.enums.InteractionTypeEnum.CALLS - ) - - # [START mutable_resource] - # Add the campaign bid modifier. Here we pass the optional parameter - # response_content_type=MUTABLE_RESOURCE so that the response contains - # the mutated object and not just its resource name. - request = client.get_type("MutateCampaignBidModifiersRequest") - request.customer_id = customer_id - request.operations = [campaign_bid_modifier_operation] - request.response_content_type = ( - client.enums.ResponseContentTypeEnum.MUTABLE_RESOURCE - ) - - campaign_bm_response = campaign_bm_service.mutate_campaign_bid_modifiers( - request=request - ) - - # The resource returned in the response can be accessed directly in the - # results list. Its fields can be read directly, and it can also be mutated - # further and used in subsequent requests, without needing to make - # additional Get or Search requests. - mutable_resource = campaign_bm_response.results[0].campaign_bid_modifier - print( - "Created campaign bid modifier with resource_name " - f"'{mutable_resource.resource_name}', criterion ID " - f"'{mutable_resource.criterion_id}', and bid modifier value " - f"'{mutable_resource.bid_modifier}', under the campaign with " - f"resource_name '{mutable_resource.campaign}', " - ) - # [END mutable_resource] - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Adds a bid modifier to the specified campaign ID, for " - "the given customer ID." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-i", "--campaign_id", type=str, required=True, help="The campaign ID." - ) - parser.add_argument( - "-b", - "--bid_modifier_value", - type=float, - required=False, - default=1.5, - help="The bid modifier value.", - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.campaign_id, - args.bid_modifier_value, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/campaign_management/add_campaign_draft.py b/examples/campaign_management/add_campaign_draft.py deleted file mode 100755 index b84cd96a0..000000000 --- a/examples/campaign_management/add_campaign_draft.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example adds a campaign draft for a campaign. - -Make sure you specify a campaign that has a non-shared budget. -""" - - -import argparse -import sys -from uuid import uuid4 - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, base_campaign_id): - campaign_service = client.get_service("CampaignService") - campaign_draft_service = client.get_service("CampaignDraftService") - - # Creates a campaign draft operation. - campaign_draft_operation = client.get_type("CampaignDraftOperation") - campaign_draft = campaign_draft_operation.create - - # Creates a campaign draft. - campaign_draft.base_campaign = campaign_service.campaign_path( - customer_id, base_campaign_id - ) - campaign_draft.name = f"Campaign Draft #{uuid4()}" - - # Issues a mutate request to add the campaign draft. - campaign_draft_response = campaign_draft_service.mutate_campaign_drafts( - customer_id=customer_id, operations=[campaign_draft_operation] - ) - print( - "Created campaign draft: " - f'"{campaign_draft_response.results[0].resource_name}".' - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="Adds a campaign draft for the specified base campaign " - "ID for the given customer ID." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-i", - "--base_campaign_id", - type=str, - required=True, - help="The base campaign ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.base_campaign_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/campaign_management/get_campaigns_by_label.py b/examples/campaign_management/get_campaigns_by_label.py deleted file mode 100755 index 0cfe06f83..000000000 --- a/examples/campaign_management/get_campaigns_by_label.py +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env python -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to get all campaigns with a specific label ID. - -To add campaigns, run add_campaigns.py. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -_DEFAULT_PAGE_SIZE = 1000 - - -# [START get_campaigns_by_label] -def main(client, customer_id, label_id, page_size): - """Demonstrates how to retrieve all campaigns by a given label ID. - - Args: - client: An initialized GoogleAdsClient instance. - customer_id: A client customer ID str. - label_id: A label ID to use when searching for campaigns. - page_size: An int of the number of results to include in each page of - results. - """ - ga_service = client.get_service("GoogleAdsService") - - # Creates a query that will retrieve all campaign labels with the - # specified label ID. - query = f""" - SELECT - campaign.id, - campaign.name, - label.id, - label.name - FROM campaign_label - WHERE label.id = "{label_id}" - ORDER BY campaign.id""" - - # Retrieves a google.api_core.page_iterator.GRPCIterator instance - # initialized with the specified request parameters. - request = client.get_type("SearchGoogleAdsRequest") - request.customer_id = customer_id - request.query = query - request.page_size = page_size - - iterator = ga_service.search(request=request) - - # Iterates over all rows in all pages and prints the requested field - # values for the campaigns and labels in each row. The results include - # the campaign and label objects because these were included in the - # search criteria. - for row in iterator: - print( - f'Campaign found with ID "{row.campaign.id}", name ' - f'"{row.campaign.name}", and label "{row.label.name}".' - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="Lists all campaigns for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-l", - "--label_id", - type=str, - required=True, - help="A label ID associated with a campaign.", - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.label_id, - _DEFAULT_PAGE_SIZE, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/misc/get_all_image_assets.py b/examples/misc/get_all_image_assets.py deleted file mode 100755 index c9234daa8..000000000 --- a/examples/misc/get_all_image_assets.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This code example gets all image assets.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - -_DEFAULT_PAGE_SIZE = 1000 - - -def main(client, customer_id, page_size): - """Main method, to run this code example as a standalone application.""" - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - asset.name, - asset.image_asset.file_size, - asset.image_asset.full_size.width_pixels, - asset.image_asset.full_size.height_pixels, - asset.image_asset.full_size.url - FROM asset - WHERE asset.type = IMAGE""" - - search_request = client.get_type("SearchGoogleAdsRequest") - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = page_size - results = ga_service.search(search_request) - - results = ga_service.search(request=search_request) - - count = 0 - for row in results: - asset = row.asset - image_asset = asset.image_asset - count += 1 - print( - f'Image with name "{asset.name}" found:\n' - f"\tfile size {image_asset.file_size} bytes\n" - f"\twidth {image_asset.full_size.width_pixels}px\n" - f"\theight {image_asset.full_size.height_pixels}px\n" - f'\turl "{image_asset.full_size.url}"' - ) - - print(f"Total of {count} image(s) found.") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="List all image assets for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, _DEFAULT_PAGE_SIZE) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/misc/get_all_videos_and_images.py b/examples/misc/get_all_videos_and_images.py deleted file mode 100755 index c5b64a749..000000000 --- a/examples/misc/get_all_videos_and_images.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This code example gets information about all video and image files.""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - -_DEFAULT_PAGE_SIZE = 1000 - - -def main(client, customer_id, page_size): - """Main method, to run this code example as a standalone application.""" - ga_service = client.get_service("GoogleAdsService") - - # Creates a query that will retrieve all video and image files. - query = """ - SELECT media_file.id, media_file.name, media_file.type - FROM media_file - ORDER BY media_file.id""" - - # Issues a search request by specifying page size. - search_request = client.get_type("SearchGoogleAdsRequest") - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = page_size - results = ga_service.search(request=search_request) - - # Iterates over all rows and prints the information about each media file. - for row in results: - media_file = row.media_file - print( - f"Media file with ID {media_file.id}, " - f'name "{media_file.name}", ' - f"type {media_file.type_.name} was found." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="List all videos and images for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, _DEFAULT_PAGE_SIZE) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/remarketing/add_remarketing_action.py b/examples/remarketing/add_remarketing_action.py deleted file mode 100755 index f5da19d36..000000000 --- a/examples/remarketing/add_remarketing_action.py +++ /dev/null @@ -1,166 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example demonstrates usage of remarketing actions. - -A new remarketing action will be created for the specified customer, and its -associated tag snippets will be retrieved. -""" - - -import argparse -import sys -from uuid import uuid4 - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, page_size): - remarketing_action_resource_name = add_remarketing_action( - client, customer_id - ) - - print(f'Created remarketing action "{remarketing_action_resource_name}".') - - queried_remarketing_action = query_remarketing_action( - client, customer_id, remarketing_action_resource_name, page_size - ) - - print_remarketing_action_attributes(queried_remarketing_action) - - -# [START add_remarketing_action] -def add_remarketing_action(client, customer_id): - remarketing_action_service = client.get_service("RemarketingActionService") - remarketing_action_operation = client.get_type("RemarketingActionOperation") - - remarketing_action = remarketing_action_operation.create - remarketing_action.name = f"Remarketing action #{uuid4()}" - - try: - remarketing_action_response = remarketing_action_service.mutate_remarketing_actions( - customer_id=customer_id, operations=[remarketing_action_operation], - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) - - return remarketing_action_response.results[0].resource_name - # [END add_remarketing_action] - - -def query_remarketing_action(client, customer_id, resource_name, page_size): - """Retrieves the previously created remarketing action with tag snippets. - - Args: - client: the Google Ads client - customer_id: the Google Ads customer ID - resource_name: the resource name of the remarketing action to query - page_size: the number of rows to return per page - - Returns: - the found remarketing action - """ - # [START add_remarketing_action_1] - query = f""" - SELECT - remarketing_action.id, - remarketing_action.name, - remarketing_action.tag_snippets - FROM remarketing_action - WHERE remarketing_action.resource_name = '{resource_name}'""" - # [END add_remarketing_action_1] - - googleads_service_client = client.get_service("GoogleAdsService") - search_request = client.get_type("SearchGoogleAdsRequest") - - search_request.customer_id = customer_id - search_request.query = query - search_request.page_size = page_size - - results = googleads_service_client.search(search_request) - - try: - return list(results)[0].remarketing_action - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) - - -def print_remarketing_action_attributes(remarketing_action): - print( - f"Remarketing action has ID {remarketing_action.id} and name " - f'"{remarketing_action.name}". \nIt has the following ' - "generated tag snippets:\n" - ) - - for tag_snippet in remarketing_action.tag_snippets: - tracking_code_type = tag_snippet.type_.name - tracking_code_page_format = tag_snippet.page_format.name - - print("=" * 80) - print( - f'Tag snippet with code type "{tracking_code_type}", and code ' - f'page format "{tracking_code_page_format}" has the following:\n' - ) - print("-" * 80) - print(f"Global site tag: \n\n{tag_snippet.global_site_tag}") - print("-" * 80) - print(f"Event snippet: \n\n{tag_snippet.event_snippet}") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description="Adds a remarketing action for specified customer." - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - # The following argument(s) are optional. - parser.add_argument( - "-p", - "--page_size", - type=int, - default=1000, - help="Number of pages to be returned in the response.", - ) - args = parser.parse_args() - - main(googleads_client, args.customer_id, args.page_size) diff --git a/examples/reporting/get_hotel_ads_performance.py b/examples/reporting/get_hotel_ads_performance.py deleted file mode 100755 index 0f691165d..000000000 --- a/examples/reporting/get_hotel_ads_performance.py +++ /dev/null @@ -1,108 +0,0 @@ -#!/usr/bin/env python -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Demonstrates how to request Hotel ad performance statistics. - -This example gets Hotel ads performance statistics for the 50 Hotel ad -groups with the most impressions over the last 7 days. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - campaign.id, - campaign.advertising_channel_type, - ad_group.id, - ad_group.status, - metrics.impressions, - metrics.hotel_average_lead_value_micros, - segments.hotel_check_in_day_of_week, - segments.hotel_length_of_stay - FROM hotel_performance_view - WHERE segments.date DURING LAST_7_DAYS - AND campaign.advertising_channel_type = 'HOTEL' - AND ad_group.status = 'ENABLED' - ORDER BY metrics.impressions DESC - LIMIT 50""" - - search_request = client.get_type("SearchGoogleAdsStreamRequest") - search_request.customer_id = customer_id - search_request.query = query - - stream = ga_service.search_stream(search_request) - - for batch in stream: - for row in batch.results: - campaign = row.campaign - ad_group = row.ad_group - hotel_check_in_day_of_week = row.segments.hotel_check_in_day_of_week - hotel_length_of_stay = row.segments.hotel_length_of_stay - metrics = row.metrics - - print( - f'Ad group ID "{ad_group.id}" ' - f'in campaign ID "{campaign.id}" ' - ) - print( - f'with hotel check-in on "{hotel_check_in_day_of_week}" ' - f'and "{hotel_length_of_stay}" day(s) stay ' - ) - print( - f"had {metrics.impressions:d} impression(s) and " - f"{metrics.hotel_average_lead_value_micros:d} average " - "lead value (in micros) during the last 7 days.\n" - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=("Retrieves Hotel-ads performance statistics.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/reporting/get_keyword_stats.py b/examples/reporting/get_keyword_stats.py deleted file mode 100755 index fb89a3350..000000000 --- a/examples/reporting/get_keyword_stats.py +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to get campaign criteria. - -Retrieves negative keywords in a campaign. -""" - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -# [START get_keyword_stats] -def main(client, customer_id): - ga_service = client.get_service("GoogleAdsService") - - query = """ - SELECT - campaign.id, - campaign.name, - ad_group.id, - ad_group.name, - ad_group_criterion.criterion_id, - ad_group_criterion.keyword.text, - ad_group_criterion.keyword.match_type, - metrics.impressions, - metrics.clicks, - metrics.cost_micros - FROM keyword_view WHERE segments.date DURING LAST_7_DAYS - AND campaign.advertising_channel_type = 'SEARCH' - AND ad_group.status = 'ENABLED' - AND ad_group_criterion.status IN ('ENABLED', 'PAUSED') - ORDER BY metrics.impressions DESC - LIMIT 50""" - - # Issues a search request using streaming. - search_request = client.get_type("SearchGoogleAdsStreamRequest") - search_request.customer_id = customer_id - search_request.query = query - stream = ga_service.search_stream(search_request) - for batch in stream: - for row in batch.results: - campaign = row.campaign - ad_group = row.ad_group - criterion = row.ad_group_criterion - metrics = row.metrics - print( - f'Keyword text "{criterion.keyword.text}" with ' - f'match type "{criterion.keyword.match_type.name}" ' - f"and ID {criterion.criterion_id} in " - f'ad group "{ad_group.name}" ' - f'with ID "{ad_group.id}" ' - f'in campaign "{campaign.name}" ' - f"with ID {campaign.id} " - f"had {metrics.impressions} impression(s), " - f"{metrics.clicks} click(s), and " - f"{metrics.cost_micros} cost (in micros) during " - "the last 7 days." - ) - # [END get_keyword_stats] - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=("Retrieves a campaign's negative keywords.") - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/targeting/get_campaign_targeting_criteria.py b/examples/targeting/get_campaign_targeting_criteria.py deleted file mode 100755 index 4cf6d52a1..000000000 --- a/examples/targeting/get_campaign_targeting_criteria.py +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env python -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""This example illustrates how to get campaign criteria, or negative keywords. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, campaign_id): - ga_service = client.get_service("GoogleAdsService") - - query = f""" - SELECT - campaign.id, - campaign_criterion.campaign, - campaign_criterion.criterion_id, - campaign_criterion.negative, - campaign_criterion.type, - campaign_criterion.keyword.text, - campaign_criterion.keyword.match_type - FROM campaign_criterion - WHERE campaign.id = {campaign_id}""" - - search_request = client.get_type("SearchGoogleAdsStreamRequest") - search_request.customer_id = customer_id - search_request.query = query - - stream = ga_service.search_stream(request=search_request) - - for batch in stream: - for row in batch.results: - criterion = row.campaign_criterion - print( - f'Campaign criterion with ID "{criterion.criterion_id}" ' - "was retrieved:" - ) - - if criterion.type_.name == "KEYWORD": - print( - f'\t{" " if criterion.negative else "Negative "} ' - f'Keyword with text "{criterion.keyword.text}" and ' - f"match type {criterion.keyword.match_type}." - ) - else: - print(f"Not a keyword: {criterion.type_.name}") - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "List campaign criteria, or negative keywords, for a " - "given campaign." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-i", "--campaign_id", type=str, required=True, help="The campaign ID." - ) - args = parser.parse_args() - - try: - main(googleads_client, args.customer_id, args.campaign_id) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1) diff --git a/examples/targeting/search_for_language_and_carrier_constants.py b/examples/targeting/search_for_language_and_carrier_constants.py deleted file mode 100644 index ee0377e31..000000000 --- a/examples/targeting/search_for_language_and_carrier_constants.py +++ /dev/null @@ -1,181 +0,0 @@ -#!/usr/bin/env python -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Demonstrates how to search for language and mobile carrier constants. - -Specifically, this example illustrates how to: -1. Search for language constants where the name includes a given string. -2. Search for all the available mobile carrier constants with a given country - code. -""" - - -import argparse -import sys - -from google.ads.googleads.client import GoogleAdsClient -from google.ads.googleads.errors import GoogleAdsException - - -def main(client, customer_id, language_name, carrier_country_code): - """Demonstrates how to search for language and mobile carrier constants. - - Args: - client: An initialized Google Ads API client. - customer_id: The Google Ads customer ID. - language_name: String included in the language name to search for. - carrier_country_code: String code of the country where the mobile - carriers are located. - """ - search_for_language_constants(client, customer_id, language_name) - search_for_carrier_constants(client, customer_id, carrier_country_code) - - -def search_for_language_constants(client, customer_id, language_name): - """Searches for language constants where the name includes a given string. - - Args: - client: An initialized Google Ads API client. - customer_id: The Google Ads customer ID. - language_name: String included in the language name to search for. - """ - # Get the GoogleAdsService client. - googleads_service = client.get_service("GoogleAdsService") - - # Create a query that retrieves the language constants where the name - # includes a given string. - query = f""" - SELECT - language_constant.id, - language_constant.code, - language_constant.name, - language_constant.targetable - FROM language_constant - WHERE language_constant.name LIKE '%{language_name}%'""" - - # Issue a search request and process the stream response to print the - # requested field values for the carrier constant in each row. - stream = googleads_service.search_stream( - customer_id=customer_id, query=query - ) - - for batch in stream: - for row in batch.results: - print( - f"Language with ID {row.language_constant.id}, " - f"code '{row.language_constant.code}', " - f"name '{row.language_constant.name}', " - f"and targetable '{row.language_constant.targetable}' " - "was found." - ) - - -def search_for_carrier_constants(client, customer_id, carrier_country_code): - """Searches for mobile carrier constants with a given country code. - - Args: - client: An initialized Google Ads API client. - customer_id: The Google Ads customer ID. - carrier_country_code: String code of the country where the mobile - carriers are located. - """ - # Get the GoogleAdsService client. - googleads_service = client.get_service("GoogleAdsService") - - # Create a query that retrieves the targetable carrier constants by country - # code. - query = f""" - SELECT - carrier_constant.id, - carrier_constant.name, - carrier_constant.country_code - FROM carrier_constant - WHERE carrier_constant.country_code = '{carrier_country_code}'""" - - # Issue a search request and process the stream response to print the - # requested field values for the carrier constant in each row. - stream = googleads_service.search_stream( - customer_id=customer_id, query=query - ) - - for batch in stream: - for row in batch.results: - print( - f"Carrier with ID {row.carrier_constant.id}, " - f"name '{row.carrier_constant.name}', " - f"and country code '{row.carrier_constant.country_code}' " - "was found." - ) - - -if __name__ == "__main__": - # GoogleAdsClient will read the google-ads.yaml configuration file in the - # home directory if none is specified. - googleads_client = GoogleAdsClient.load_from_storage(version="v14") - - parser = argparse.ArgumentParser( - description=( - "Demonstrates how to search for language and mobile carrier " - "constants." - ) - ) - # The following argument(s) should be provided to run the example. - parser.add_argument( - "-c", - "--customer_id", - type=str, - required=True, - help="The Google Ads customer ID.", - ) - parser.add_argument( - "-l", - "--language_name", - type=str, - required=False, - default="eng", - help="Optional, string included in the language name to search for.", - ) - parser.add_argument( - "-p", - "--carrier_country_code", - type=str, - required=False, - default="US", - help=( - "Optional, string code of the country where the mobile carriers " - "are located, e.g. 'US', 'ES', etc. " - "A list of country codes can be referenced here: " - "https://developers.google.com/google-ads/api/reference/data/geotargets" - ), - ) - args = parser.parse_args() - - try: - main( - googleads_client, - args.customer_id, - args.language_name, - args.carrier_country_code, - ) - except GoogleAdsException as ex: - print( - f'Request with ID "{ex.request_id}" failed with status ' - f'"{ex.error.code().name}" and includes the following errors:' - ) - for error in ex.failure.errors: - print(f'\tError with message "{error.message}".') - if error.location: - for field_path_element in error.location.field_path_elements: - print(f"\t\tOn field: {field_path_element.field_name}") - sys.exit(1)